1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-15 07:40:07 +02:00
copilot/scripts/run-staged-pkg
2017-05-29 23:04:51 +01:00

68 lines
1.5 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 = []) =>
execa('lerna', args, {
stdio: 'inherit'
});
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 map(pkgs.map(({ name }) => name), run);
};
main(gather());