Skip to content

Commit

Permalink
Update: Add silent option
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jan 16, 2016
1 parent bbad6e8 commit 87aedbf
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/npm-debug.log
/test-workspace/npm-debug.log
/test-workspace/test.txt
/test.js
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Usage: npm-run-all [OPTIONS] [...tasks]
-p, --parallel [...tasks] Run a group of tasks in parallel.
-s, --sequential [...tasks] Run a group of tasks sequentially.
-v, --version Print version number.
--silent Set "silent" to the log level of npm.
```

### Run tasks sequentially
Expand Down Expand Up @@ -89,7 +90,7 @@ npm-run-all --parallel "build:* -- --watch"
We can enclose a script name or a pattern in quotes to use arguments.
When you use a pattern, arguments are forwarded to every matched task.

An example: https://gist.github.com/mysticatea/34949629c9e0a01a9e7d
An example: https://gist.github.com/mysticatea/34949629c9e0a01a9e7d<br>
See also: https://docs.npmjs.com/cli/run-script

### Glob-like pattern matching for task names
Expand Down Expand Up @@ -152,6 +153,9 @@ Run npm-scripts.
Every value is a map-like object (Pairs of variable name and value).
e.g. `{"npm-run-all": {"test": 777, "test2": 333}}`
Default is `null`.
- **options.silent** `boolean` --
The flag to set `silent` to the log level of npm.
Default is `false`.

`runAll` returns a promise that becomes *fulfilled* when all tasks are completed.
The promise will become *rejected* when any of the tasks exit with a non-zero code.
Expand Down
1 change: 1 addition & 0 deletions src/bin/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Usage: npm-run-all [OPTIONS] [...tasks]
-p, --parallel [...tasks] Run a group of tasks in parallel.
-s, --sequential [...tasks] Run a group of tasks in sequencial.
-v, --version Print version number.
--silent Set "silent" to the log level of npm.
See Also:
https://github.com/mysticatea/npm-run-all#readme
Expand Down
30 changes: 22 additions & 8 deletions src/bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function parse(args) {
queue.push({parallel: true, patterns: [], packageConfig});
break;

case "--silent":
// do nothing.
break;

default: {
const matched = OVERWRITE_OPTION.exec(arg);
if (matched) {
Expand Down Expand Up @@ -108,19 +112,29 @@ function parse(args) {
*/
export default function npmRunAll(args, stdout, stderr) {
try {
const stdin = process.stdin;
const silent = (
args.indexOf("--silent") !== -1 ||
process.env.npm_config_loglevel === "silent"
);

return parse(args).reduce(
(prev, group) => (group.patterns.length === 0) ?
prev :
prev.then(() => runAll(
group.patterns,
(prev, {patterns, parallel, packageConfig}) => {
if (patterns.length === 0) {
return prev;
}
return prev.then(() => runAll(
patterns,
{
stdout,
stderr,
stdin: process.stdin,
parallel: group.parallel,
packageConfig: group.packageConfig
stdin,
parallel,
packageConfig,
silent
}
)),
));
},
START_PROMISE
);
}
Expand Down
23 changes: 15 additions & 8 deletions src/lib/npm-run-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ function toArray(x) {
*/
function toOverwriteOptions(config) {
const options = [];
if (config == null) {
return options;
}

for (const packageName of Object.keys(config)) {
const packageConfig = config[packageName];
Expand Down Expand Up @@ -87,6 +84,9 @@ function toOverwriteOptions(config) {
* Every value is a map-like object (Pairs of variable name and value).
* e.g. `{"npm-run-all": {"test": 777}}`
* Default is `null`.
* @param {boolean} options.silent -
* The flag to set `silent` to the log level of npm.
* Default is `false`.
* @returns {Promise}
* A promise object which becomes fullfilled when all npm-scripts are completed.
*/
Expand All @@ -98,7 +98,8 @@ export default function npmRunAll(
stdout = null,
stderr = null,
taskList = null,
packageConfig = null
packageConfig = null,
silent = false
} = {}
) {
try {
Expand All @@ -115,10 +116,16 @@ export default function npmRunAll(
throw new Error(`Matched tasks not found: ${patterns.join(", ")}`);
}

return (
parallel ? runTasksInParallel(tasks, stdin, stdout, stderr, toOverwriteOptions(packageConfig)) :
/* else */ runTasksInSequencial(tasks, stdin, stdout, stderr, toOverwriteOptions(packageConfig))
);
const prefixOptions = [];
if (silent) {
prefixOptions.push("--silent");
}
if (packageConfig != null) {
prefixOptions.push(...toOverwriteOptions(packageConfig));
}

const runTasks = parallel ? runTasksInParallel : runTasksInSequencial;
return runTasks(tasks, stdin, stdout, stderr, prefixOptions);
}
catch (err) {
return Promise.reject(new Error(err.message));
Expand Down
8 changes: 4 additions & 4 deletions src/lib/run-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ function detectStreamKind(stream, std) {
* If this is `null`, cannot send.
* If this is `process.stderr`, inherits it.
* Otherwise, makes a pipe.
* @param {string[]} packageConfigOptions -
* `--:=` style options to overwrite package configs.
* @param {string[]} prefixOptions -
* An array of options which are inserted before the task name.
* @returns {Promise}
* A promise object which becomes fullfilled when the npm-script is completed.
* This promise object has an extra method: `abort()`.
* @private
*/
export default function runTask(task, stdin, stdout, stderr, packageConfigOptions) {
export default function runTask(task, stdin, stdout, stderr, prefixOptions) {
let cp = null;
const promise = whichNpm().then(npmPath => new Promise((resolve, reject) => {
const stdinKind = detectStreamKind(stdin, process.stdin);
Expand All @@ -60,7 +60,7 @@ export default function runTask(task, stdin, stdout, stderr, packageConfigOption
// Execute.
cp = spawn(
npmPath,
["run-script"].concat(packageConfigOptions, parseArgs(task)),
["run-script"].concat(prefixOptions, parseArgs(task)),
{stdio: [stdinKind, stdoutKind, stderrKind]}
);

Expand Down
8 changes: 4 additions & 4 deletions src/lib/run-tasks-in-parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ import runTask from "./run-task";
* If this is `null`, cannot send.
* If this is `process.stderr`, inherits it.
* Otherwise, makes a pipe.
* @param {string[]} packageConfigOptions -
* `--:=` style options to overwrite package configs.
* @param {string[]} prefixOptions -
* An array of options which are inserted before the task name.
* @returns {Promise}
* A promise object which becomes fullfilled when all npm-scripts are completed.
* @private
*/
export default function runTasksInParallel(tasks, stdin, stdout, stderr, packageConfigOptions) {
export default function runTasksInParallel(tasks, stdin, stdout, stderr, prefixOptions) {
// When one of tasks exited with non-zero, abort all tasks.
// And wait for all tasks exit.
let nonZeroExited = null;
const taskPromises = tasks.map(task => runTask(task, stdin, stdout, stderr, packageConfigOptions));
const taskPromises = tasks.map(task => runTask(task, stdin, stdout, stderr, prefixOptions));
const parallelPromise = Promise.all(taskPromises.map(p =>
p.then(item => {
if (nonZeroExited == null && item.code) {
Expand Down
8 changes: 4 additions & 4 deletions src/lib/run-tasks-in-sequencial.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ function rejectIfNonZeroExit(result) {
* If this is `null`, cannot send.
* If this is `process.stderr`, inherits it.
* Otherwise, makes a pipe.
* @param {string[]} packageConfigOptions -
* `--:=` style options to overwrite package configs.
* @param {string[]} prefixOptions -
* An array of options which are inserted before the task name.
* @returns {Promise}
* A promise object which becomes fullfilled when all npm-scripts are completed.
* @private
*/
export default function runTasksInSequencial(tasks, stdin, stdout, stderr, packageConfigOptions) {
export default function runTasksInSequencial(tasks, stdin, stdout, stderr, prefixOptions) {
return tasks.reduce(
(prev, task) => prev.then(result => {
rejectIfNonZeroExit(result);
return runTask(task, stdin, stdout, stderr, packageConfigOptions);
return runTask(task, stdin, stdout, stderr, prefixOptions);
}),
START_PROMISE
).then(rejectIfNonZeroExit);
Expand Down
22 changes: 22 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,26 @@ describe("[common] npm-run-all", () => {
it("command version", () => command(["test-task:issue14:posix"]));
});
}

describe("should not print log if silent option was given:", () => {
it("lib version", () => {
const stdout = new BufferStream();
const stderr = new BufferStream();
return runAll("test-task:error", {silent: true, stdout, stderr})
.then(
() => assert(false, "Should fail."),
() => assert(stdout.value === "" && stderr.value === "")
);
});

it("command version", () => {
const stdout = new BufferStream();
const stderr = new BufferStream();
return command(["--silent", "test-task:error"], stdout, stderr)
.then(
() => assert(false, "Should fail."),
() => assert(stdout.value === "" && stderr.value === "")
);
});
});
});

0 comments on commit 87aedbf

Please sign in to comment.