feat: run command, fail on non-zero exit

This commit is contained in:
2019-04-04 16:17:58 -07:00
parent 2ffd608fc5
commit b84c4556cd
5 changed files with 32 additions and 14 deletions

View File

@@ -57,7 +57,8 @@
"fast-deep-equal": "^2.0.1",
"joycon": "^2.2.4",
"md5-file": "^4.0.0",
"mkdirp": "^0.5.1"
"mkdirp": "^0.5.1",
"shell-exec": "^1.0.2"
},
"devDependencies": {
"@w33ble/npm-auto-tools": "*",

View File

@@ -8,15 +8,20 @@ function parseArgs() {
return args.reduce(
(acc, arg) => {
if (arg === '--verbose') acc.verbose = true;
if (arg === '--force') acc.force = true;
if (arg === '--use-yarn') acc.useYarn = true;
const match = /--([a-z-]+)(=(.+))?/.exec(arg);
// ignore anything besides -- flags
if (!match) return acc;
const [, flag, , value] = match;
if (flag === 'verbose') acc.verbose = true;
if (flag === 'force') acc.force = true;
if (flag === 'cmd') acc.cmd = value;
return acc;
},
{
verbose: false,
force: false,
useYarn: false,
}
);
}
@@ -24,7 +29,7 @@ function parseArgs() {
function buildConfig(args) {
// build config object, mixing in CLI overrides
const configOverrides = {};
if (args.useYarn) configOverrides.cmd = 'yarn';
if (args.cmd) configOverrides.cmd = args.cmd;
return Object.assign({}, utils.getConfig(), configOverrides);
}

View File

@@ -1,6 +1,7 @@
const path = require('path');
const md5File = require('md5-file');
const deepEqual = require('fast-deep-equal');
const shellExec = require('shell-exec');
const data = require('./data');
const utils = require('./utils');
@@ -14,8 +15,10 @@ function getFileHash(file, verbose = false) {
}
}
function execCommand(cmd) {
console.log('RUN:', cmd);
async function execCommand(cmd) {
// do nothing if cmd is missing or false
if (cmd == null || cmd === false) return { stdout: '', stderr: '', cmd, code: 0 };
return shellExec(cmd, { stdio: 'inherit' });
}
const defaultOptions = {
@@ -23,7 +26,7 @@ const defaultOptions = {
force: false,
};
module.exports = function pkgcomp(config, ident, opts = defaultOptions) {
module.exports = async function pkgcomp(config, ident, opts = defaultOptions) {
const packageHistory = data.read(config.dataDir, ident);
const firstRun = Object.keys(packageHistory).length === 0;
const doExec = !firstRun || opts.force;
@@ -49,7 +52,11 @@ module.exports = function pkgcomp(config, ident, opts = defaultOptions) {
// check if the command should be run
const runCmd = Boolean(opts.force || (doExec && !deepEqual(packageHistory, payload)));
if (runCmd) execCommand(config.cmd);
data.write(config.dataDir, ident, payload);
if (runCmd) {
const res = await execCommand(config.cmd);
if (res.code !== 0) process.exit(res.code);
data.write(config.dataDir, ident, payload);
} else {
data.write(config.dataDir, ident, payload);
}
};

View File

@@ -52,12 +52,12 @@ exports.getConfig = (overrides = {}) => {
checkFiles: ['package-lock.json', 'yarn.lock'],
rootDir: process.cwd(),
dataDir: exports.getDataDir(),
cmd: 'npm',
cmd: 'echo Modules need updating',
};
const result = joycon.loadSync({
packageKey: 'pkgcomp',
files: ['.config/pkgcomp.json', 'pkgcomp.json', 'package.json'],
files: ['.config/pkgcomp.json', '.pkgcomp.json', 'pkgcomp.json', 'package.json'],
});
return Object.assign(defaults, result.data, overrides);

View File

@@ -2320,6 +2320,11 @@ shebang-regex@^1.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=
shell-exec@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/shell-exec/-/shell-exec-1.0.2.tgz#2e9361b0fde1d73f476c4b6671fa17785f696756"
integrity sha512-jyVd+kU2X+mWKMmGhx4fpWbPsjvD53k9ivqetutVW/BQ+WIZoDoP4d8vUMGezV6saZsiNoW2f9GIhg9Dondohg==
signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"