mirror of
https://github.com/yldio/copilot.git
synced 2024-11-28 14:10:04 +02:00
9b3874ea56
For lint and test, it detects the packages that the staged files affect and only runs the corresponding `lint` and `test` scripts. For format, it will run prettier through the Added/Modified js files.
72 lines
1.6 KiB
JavaScript
Executable File
72 lines
1.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const { packages } = require('../lerna.json');
|
|
const { readFile } = require('mz/fs');
|
|
const sgf = require('staged-git-files');
|
|
const execa = require('execa');
|
|
const awaitify = require('apr-awaitify');
|
|
const main = require('apr-main');
|
|
const map = require('apr-map');
|
|
const globby = require('globby');
|
|
const path = require('path');
|
|
const uniq = require('lodash.uniq');
|
|
const argv = require('yargs').argv;
|
|
|
|
const ROOT = path.join(__dirname, '..');
|
|
const getStaged = awaitify(sgf);
|
|
|
|
const statuses = [
|
|
'Added',
|
|
'Copied',
|
|
'Deleted',
|
|
'Modified',
|
|
'Renamed',
|
|
'Unmerged'
|
|
];
|
|
|
|
const exec = (args = []) => {
|
|
const cp = execa('lerna', args);
|
|
|
|
cp.stdout.pipe(process.stdout);
|
|
cp.stderr.pipe(process.stderr);
|
|
|
|
return cp;
|
|
};
|
|
|
|
const lint = (scope) => exec(['run', 'lint', '--scope', scope]);
|
|
const test = (scope) => exec(['run', 'test', '--scope', scope]);
|
|
|
|
const run = async (scope) => {
|
|
if (argv.lint) {
|
|
await lint(scope);
|
|
}
|
|
|
|
if (argv.test) {
|
|
await test(scope);
|
|
}
|
|
};
|
|
|
|
const gather = async () => {
|
|
const locations = await globby(packages, {
|
|
cwd: ROOT
|
|
});
|
|
|
|
const staged = (await getStaged())
|
|
.filter(({ status }) => statuses.indexOf(status) >= 0)
|
|
.map(({ filename }) => path.resolve(ROOT, filename));
|
|
|
|
const folders = uniq(
|
|
locations
|
|
.map(folder => path.resolve(ROOT, folder))
|
|
.filter(folder => staged.some(i => i.indexOf(folder) >= 0))
|
|
);
|
|
|
|
const pkgs = await map(folders, async folder =>
|
|
JSON.parse(await readFile(path.join(folder, 'package.json'), 'utf-8'))
|
|
)
|
|
|
|
return await map(pkgs.map(({ name }) => name), run)
|
|
};
|
|
|
|
main(gather());
|