joyent-portal/scripts/run-staged-pkg
Sérgio Ramos 9b3874ea56 chore: format, lint, and test scripts based on staged files
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.
2017-05-26 10:20:00 +01:00

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());