mirror of
https://github.com/yldio/copilot.git
synced 2024-12-25 03:10:06 +02:00
75 lines
1.8 KiB
JavaScript
Executable File
75 lines
1.8 KiB
JavaScript
Executable File
#!/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');
|
|
const asyncFilter = require('apr-filter');
|
|
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, '../');
|
|
const SCRIPTS = path.resolve(__dirname);
|
|
|
|
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 = []) => {
|
|
const filteredFiles = filter(files);
|
|
|
|
if (!filteredFiles.length) {
|
|
return;
|
|
}
|
|
|
|
return execa(
|
|
'prettier',
|
|
['--write', '--single-quote'].concat(filteredFiles),
|
|
{
|
|
stdio: 'inherit'
|
|
}
|
|
);
|
|
};
|
|
|
|
const all = async () => {
|
|
const files = await globby(['packages/**/*.js', 'scripts/*'], {
|
|
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))
|
|
.filter(
|
|
filename => /\.js$/.test(filename) || filename.indexOf(SCRIPTS) === 0
|
|
);
|
|
|
|
const existing = await asyncFilter(files, exists);
|
|
|
|
if (!existing.length) {
|
|
return;
|
|
}
|
|
|
|
return run(existing);
|
|
};
|
|
|
|
main(argv._.length ? run(argv._) : argv.staged ? staged() : all());
|