UNPKG

npx-run

Version:

Easily run scripts using npx.

100 lines (71 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fs = _interopRequireDefault(require("fs")); var _libnpx = _interopRequireDefault(require("libnpx")); var _strings = require("./strings"); var _help = require("./help"); var _scripts = require("./scripts"); var _parseArgs = require("./parse-args"); var _colorize = require("./colorize"); var _globals = require("./globals"); var _pkgRoot = _interopRequireDefault(require("./pkg-root")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const runCmd = async (nodeArgs, scriptArgs) => { const npxArgs = _libnpx.default.parseArgs([_globals.process.argv[0], _globals.process.argv[1], '--always-spawn', '--no-install', ...nodeArgs.map(a => `--node-arg=${a}`), ...scriptArgs]); return await (0, _libnpx.default)(npxArgs); }; const runSingle = async (dryRun, runArgs, [cmd, ...args]) => { // TODO: could run this right away even if not in dry-run if (dryRun && cmd === 'run') { // eslint-disable-next-line no-use-before-define return await run('--dry-run', ...args); } if (!dryRun) { const result = await runCmd(runArgs, [cmd, ...args]); if (result === undefined) { return -1; } return result.code; } return 0; }; const runAll = async (tasks, dryRun, scripts) => { let count = 0; for (const [runArgs, scriptName, command] of tasks) { count += 1; const preview = (0, _colorize.formattedScript)(command, scripts); _strings.print`[{green ${scriptName}}] ${preview}`; const result = await runSingle(dryRun, runArgs, command); if (result !== 0) { return [count, result]; } } return [count, 0]; }; const run = async (...args) => { const scripts = (0, _scripts.getScripts)((0, _pkgRoot.default)(), _fs.default); const { tasks, dryRun, help } = (0, _parseArgs.parseArgs)(args, scripts); if (help) { (0, _help.showHelp)(scripts); return 0; } const [numRuns, exitCode] = await runAll(tasks, dryRun, scripts); if (numRuns === 0) { _strings.print` {red scripts not found} For available scripts run: npx run --help `; return -1; } return exitCode; }; var _default = run; exports.default = _default;