UNPKG

npx-run

Version:

Easily run scripts using npx.

95 lines (71 loc) 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.showHelp = exports.scriptCompare = void 0; var _stringArgv = _interopRequireDefault(require("string-argv")); var _strings = require("./strings"); var _colorize = require("./colorize"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const scriptCompare = (name1, name2) => { name1 = name1 === 'default' ? '' : name1; name2 = name2 === 'default' ? '' : name2; if (name1 > name2) { return 1; } else if (name1 < name2) { return -1; } return 0; }; exports.scriptCompare = scriptCompare; const printScripts = scripts => { const scriptNames = Object.keys(scripts).sort(scriptCompare); const maxLen = Math.max(...scriptNames.map(key => key.length)); _strings.print`{underline scripts}:`; for (const scriptName of scriptNames) { const name = (0, _colorize.formattedScriptName)(scriptName); const command = (0, _stringArgv.default)(scripts[scriptName]); const preview = (0, _colorize.formattedScript)(command, scripts); const padding = maxLen - scriptName.length; _strings.print` ${name} ${' '.repeat(padding)} ${preview}`; } return scriptNames.length; }; /* eslint-disable max-len */ const showHelp = scripts => { _strings.print` npx {bold run} {dim [run-args] [node-args]} <{green script}|{blue pattern}> [srcipt-args] ... {underline Examples}: npx {bold run} npx {bold run} {green clean} {green build} npx {bold run} {green config} --env='testing' {green build} {green deploy} npx {bold run} {blue lint:*} npx {bold run} {dim --inspect-brk} {green jest} {underline default script}: When calling without any arguemtns 'npx run' will run the {bold.green default} script if it has been defined. {underline run-args}: --help Show this help. --dry-run List scripts to be run, but don't run them. {underline node-args}: Any args before the first script are sent to npx as node args. --inspect Run node with --inspect to debug scripts. --inspect-brk Run node with --inspect-brk to debug scripts `; if (!printScripts(scripts)) { _strings.print` {red no scripts discoverd} You can add scripts using: ./package.json - using npm run-scripts ./scripts module - exporting a single default object working the same as the package.json run-scripts. ./scripts/*.js - using any loadable nodejs module which is run as a script given its basename ./scripts/*.js modules will override scripts of the same name defined in the ./scripts module. Scripts defined in the ./scripts module will override scripts of the same name defined in ./package.json. `; } }; exports.showHelp = showHelp;