From 84709c51b12b0efc5ad8c54c0bfac1249bc28e3a Mon Sep 17 00:00:00 2001 From: Mick van Gelderen Date: Fri, 18 Mar 2016 12:58:47 +0100 Subject: [PATCH] Glob configuration files Removed bluebird dependency Added funko, funko-fs and glob dependency --- package.json | 4 +- tools/sort-configuration-files.js | 72 ++++++++++++++++++------------- tools/util/glob.js | 12 ++++++ 3 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 tools/util/glob.js diff --git a/package.json b/package.json index a3d3644..d342b2f 100644 --- a/package.json +++ b/package.json @@ -11,8 +11,10 @@ "babel-core": "^6.5.2", "babel-preset-es2015-node4": "^2.0.3", "babel-register": "^6.5.2", - "bluebird": "^3.3.3", "eslint": "^2.0.0", + "funko": "^0.4.0", + "funko-fs": "^0.2.0", + "glob": "^7.0.3", "mocha": "^2.4.5", "must": "^0.13.1", "semver": "^5.1.0", diff --git a/tools/sort-configuration-files.js b/tools/sort-configuration-files.js index 5497ecd..6b058fe 100644 --- a/tools/sort-configuration-files.js +++ b/tools/sort-configuration-files.js @@ -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 -}) +) diff --git a/tools/util/glob.js b/tools/util/glob.js new file mode 100644 index 0000000..6c4a99d --- /dev/null +++ b/tools/util/glob.js @@ -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