2017-05-26 01:43:00 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
const { config } = require('../package.json');
|
|
|
|
const { exists } = require('mz/fs');
|
|
|
|
const sgf = require('staged-git-files');
|
|
|
|
const forceArray = require('force-array');
|
|
|
|
const awaitify = require('apr-awaitify');
|
2017-05-26 04:23:17 +03:00
|
|
|
const asyncFilter = require('apr-filter');
|
2017-05-26 01:43:00 +03:00
|
|
|
const execa = require('execa');
|
|
|
|
const globby = require('globby');
|
|
|
|
const main = require('apr-main');
|
|
|
|
const argv = require('yargs').argv;
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const getStaged = awaitify(sgf);
|
|
|
|
|
|
|
|
const ROOT = path.join(__dirname, '../');
|
2017-05-26 04:23:17 +03:00
|
|
|
const SCRIPTS = path.resolve(__dirname);
|
|
|
|
|
2017-05-26 01:43:00 +03:00
|
|
|
const optOut = forceArray(config['fmt-opt-out']).map(pkg =>
|
|
|
|
path.join(ROOT, `packages/${pkg}`)
|
|
|
|
);
|
|
|
|
|
|
|
|
const statuses = ['Added', 'Modified'];
|
|
|
|
|
|
|
|
const filter = (files = []) =>
|
|
|
|
files
|
|
|
|
.filter(file => !/node_modules/.test(file))
|
|
|
|
.map(file => path.resolve(ROOT, file))
|
|
|
|
.filter(file => !optOut.some(pkg => file.indexOf(pkg) === 0));
|
|
|
|
|
|
|
|
const run = async (files = []) => {
|
2017-05-26 14:02:19 +03:00
|
|
|
const filteredFiles = filter(files);
|
|
|
|
|
|
|
|
if (!filteredFiles.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-30 01:04:51 +03:00
|
|
|
return execa(
|
2017-05-26 01:43:00 +03:00
|
|
|
'prettier',
|
2017-05-30 01:04:51 +03:00
|
|
|
['--write', '--single-quote'].concat(filteredFiles),
|
|
|
|
{
|
|
|
|
stdio: 'inherit'
|
|
|
|
}
|
2017-05-26 01:43:00 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const all = async () => {
|
2017-05-26 04:23:17 +03:00
|
|
|
const files = await globby(['packages/**/*.js', 'scripts/*'], {
|
2017-05-26 01:43:00 +03:00
|
|
|
cwd: path.join(__dirname, '..')
|
|
|
|
});
|
|
|
|
|
|
|
|
return run(files);
|
|
|
|
};
|
|
|
|
|
|
|
|
const staged = async () => {
|
|
|
|
const files = (await getStaged())
|
|
|
|
.filter(({ status }) => statuses.indexOf(status) >= 0)
|
|
|
|
.map(({ filename }) => filename)
|
|
|
|
.map(filename => path.resolve(ROOT, filename))
|
2017-05-26 04:23:17 +03:00
|
|
|
.filter(
|
|
|
|
filename => /\.js$/.test(filename) || filename.indexOf(SCRIPTS) === 0
|
|
|
|
);
|
2017-05-26 01:43:00 +03:00
|
|
|
|
2017-05-26 04:23:17 +03:00
|
|
|
const existing = await asyncFilter(files, exists);
|
2017-05-26 01:43:00 +03:00
|
|
|
|
|
|
|
if (!existing.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-01 12:28:59 +03:00
|
|
|
console.log('existing = ', existing);
|
|
|
|
|
2017-05-26 01:43:00 +03:00
|
|
|
return run(existing);
|
|
|
|
};
|
|
|
|
|
|
|
|
main(argv._.length ? run(argv._) : argv.staged ? staged() : all());
|