Skip to content

Commit

Permalink
Fix: refactor and change Node API for argument-passthrough
Browse files Browse the repository at this point in the history
- Node API does not need `--`.
- Nit picking.
  • Loading branch information
mysticatea committed Jun 17, 2016
1 parent 7a1657d commit 49c9e67
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
11 changes: 6 additions & 5 deletions src/bin/common/parse-cli-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class ArgumentSet {
this.groups = [];
this.printLabel = false;
this.printName = false;
this.rest = [];
this.silent = process.env.npm_config_loglevel === "silent";
this.singleMode = Boolean(options.singleMode);
this.packageConfig = createPackageConfig();
Expand All @@ -109,15 +110,15 @@ class ArgumentSet {
* @returns {ArgumentSet} set itself.
*/
function parseCLIArgsCore(set, args) { // eslint-disable-line complexity
const terminator = args.indexOf("--");
if (terminator !== -1) {
set.rest = args.slice(terminator);
args = args.slice(0, terminator); // eslint-disable-line no-param-reassign
}
LOOP:
for (let i = 0; i < args.length; ++i) {
const arg = args[i];

switch (arg) {
case "--":
set.rest = args.slice(1 + i);
break LOOP;

case "-c":
case "--continue-on-error":
set.continueOnError = true;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/npm-run-all/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
printName,
packageConfig,
silent,
rest
arguments: rest
}
));
},
Expand Down
2 changes: 1 addition & 1 deletion src/bin/run-p/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
printName,
packageConfig,
silent,
rest
arguments: rest
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/run-s/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function npmRunAll(args, stdout, stderr) {
printName,
packageConfig,
silent,
rest
arguments: rest
}
);
}
Expand Down
64 changes: 46 additions & 18 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const runTasksInSequencial = require("./run-tasks-in-sequencial");
// Helpers
//------------------------------------------------------------------------------

const ARGS_PATTERN = /[{]([*@]|\d+)[}]/g;

/**
* Converts a given value to an array.
*
Expand All @@ -34,6 +36,47 @@ function toArray(x) {
return Array.isArray(x) ? x : [x];
}

/**
* Replaces argument placeholders (such as `{1}`) by arguments.
*
* @param {string[]} patterns - Patterns to replace.
* @param {string[]} args - Arguments to replace.
* @returns {string[]} replaced
*/
function applyArguments(patterns, args) {
return patterns.map(pattern => pattern.replace(ARGS_PATTERN, (match, index) => {
if (index === "@") {
return shellQuote.quote(args);
}
if (index === "*") {
return shellQuote.quote([args.join(" ")]);
}

const position = parseInt(index, 10);
if (position >= 1 && position <= args.length) {
return shellQuote.quote([args[position - 1]]);
}

return match;
}));
}

/**
* Parse patterns.
* In parsing process, it replaces argument placeholders (such as `{1}`) by arguments.
*
* @param {string|string[]} patternOrPatterns - Patterns to run.
* A pattern is a npm-script name or a Glob-like pattern.
* @param {string[]} args - Arguments to replace placeholders.
* @returns {string[]} Parsed patterns.
*/
function parsePatterns(patternOrPatterns, args) {
const patterns = toArray(patternOrPatterns);
const hasArguments = Array.isArray(args) && args.length > 0;

return hasArguments ? applyArguments(patterns, args) : patterns;
}

/**
* Converts a given config object to an `--:=` style option array.
*
Expand Down Expand Up @@ -142,14 +185,15 @@ module.exports = function npmRunAll(
continueOnError = false,
printLabel = false,
printName = false,
rest = []
arguments: args = []
} = {}
) {
try {
const patterns = toArray(patternOrPatterns);
const patterns = parsePatterns(patternOrPatterns, args);
if (patterns.length === 0) {
return Promise.resolve(null);
}

if (taskList != null && Array.isArray(taskList) === false) {
throw new Error("Invalid options.taskList");
}
Expand All @@ -162,22 +206,6 @@ module.exports = function npmRunAll(
prefixOptions.push(...toOverwriteOptions(packageConfig));
}

for (let i = 0, len = patterns.length; i < len; ++i) {
patterns[i] = patterns[i].replace(/[{]([*@]|\d+)[}]/g, (match, index) => {
if (index === "@") {
return shellQuote.quote(rest.slice(1));
}
if (index === "*") {
return shellQuote.quote([rest.slice(1).join(" ")]);
}
const position = parseInt(index, 10);
if (position >= 0 && position < rest.length) {
return shellQuote.quote([rest[position]]);
}
return match;
});
}

return Promise.resolve(taskList)
.then(taskList => { // eslint-disable-line no-shadow
if (taskList != null) {
Expand Down

0 comments on commit 49c9e67

Please sign in to comment.