-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed bluebird dependency Added funko, funko-fs and glob dependency
- Loading branch information
1 parent
0966907
commit 84709c5
Showing
3 changed files
with
56 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,65 @@ | ||
import fs from 'fs' | ||
import Promise from 'bluebird' | ||
import all from 'funko/lib/future/all' | ||
import glob from './util/glob' | ||
import map from 'funko/lib/map' | ||
import pipe from 'funko/lib/pipe' | ||
import readFile from 'funko-fs/lib/read-file' | ||
import resolved from 'funko/lib/future/resolved' | ||
import sortObject from 'sort-object-circular' | ||
|
||
Promise.promisifyAll(fs) | ||
import writeFile from 'funko-fs/lib/write-file' | ||
import { EOL } from 'os' | ||
|
||
const ERROR_ON_CHANGES = process.argv.indexOf('--error-on-changes') !== -1 | ||
|
||
function sortJson(string) { | ||
return JSON.stringify(sortObject(JSON.parse(string)), null, 2) + '\n' | ||
return JSON.stringify(sortObject(JSON.parse(string)), null, 2) | ||
.replace('\n', EOL) + EOL | ||
} | ||
|
||
function sortLines(string) { | ||
return string | ||
.split('\n') | ||
.split(EOL) | ||
.sort() | ||
.join('\n') | ||
.trim('\n') + '\n' | ||
.join(EOL) | ||
.trim(EOL) + EOL | ||
} | ||
|
||
function createFileTransformer(transformation) { | ||
return function(path) { | ||
return fs.readFileAsync(path) | ||
.then(buffer => { | ||
return readFile('utf-8', path) | ||
.chain(buffer => { | ||
console.log(`Read "${path}".`) // eslint-disable-line no-console | ||
const input = buffer.toString() | ||
const output = transformation(input) | ||
if (input === output) return false | ||
return fs.writeFileAsync(path, output).then(() => { | ||
if (input === output) return resolved(null) | ||
return writeFile('utf-8', path, output) | ||
.chain(() => { | ||
console.log(`Wrote "${path}".`) // eslint-disable-line no-console | ||
return true | ||
return resolved(path) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
Promise.all([ | ||
...[ | ||
'.babelrc', | ||
'.eslintrc.json', | ||
'package.json' | ||
].map(createFileTransformer(sortJson)), | ||
...[ | ||
'.gitignore', | ||
'.npmignore' | ||
].map(createFileTransformer(sortLines)) | ||
]) | ||
.then(changes => { | ||
console.log(`Sorted configuration files.`) // eslint-disable-line no-console | ||
if (ERROR_ON_CHANGES && changes.reduce((l, r) => l || r)) { | ||
process.exitCode = 1 | ||
function transformFiles(pattern, transformer) { | ||
return glob({ dot: true }, pattern) | ||
.chain(pipe([ | ||
map(createFileTransformer(transformer)), | ||
all | ||
])) | ||
} | ||
|
||
all([ | ||
transformFiles('{,{src,test,tools}/**/}{.babelrc,*.json}', sortJson), | ||
transformFiles('{,{src,test,tools}/**/}{.gitignore,.npmignore}', sortLines) | ||
]).fork( | ||
console.error, // eslint-disable-line no-console | ||
([ json, lines ]) => { | ||
const changed = json.concat(lines).filter(path => path !== null) | ||
if (changed.length > 0) { | ||
console.log(`Sorted configuration files, updated ${changed.length} file${changed.length > 1 ? 's': ''}.`) // eslint-disable-line no-console | ||
if (ERROR_ON_CHANGES) process.exitCode = 1 | ||
} else { | ||
console.log('Sorted configuration files, no changes.') // eslint-disable-line no-console | ||
} | ||
} | ||
}, error => { | ||
console.error(error) // eslint-disable-line no-console | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import curry from 'funko/lib/curry' | ||
import Future from 'funko/lib/future' | ||
import originalGlob from 'glob' | ||
|
||
const glob = curry(2, (options, pattern) => | ||
Future((reject, resolve) => { | ||
originalGlob(pattern, options, (error, files) => | ||
error ? reject(error) : resolve(files)) | ||
}) | ||
) | ||
|
||
export default glob |