Skip to content

Commit

Permalink
Glob configuration files
Browse files Browse the repository at this point in the history
Removed bluebird dependency
Added funko, funko-fs and glob dependency
  • Loading branch information
mickvangelderen committed Mar 18, 2016
1 parent 0966907 commit 84709c5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 32 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
72 changes: 41 additions & 31 deletions tools/sort-configuration-files.js
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
})
)
12 changes: 12 additions & 0 deletions tools/util/glob.js
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

0 comments on commit 84709c5

Please sign in to comment.