Skip to content

Commit 87aedbf

Browse files
committed
Update: Add silent option
1 parent bbad6e8 commit 87aedbf

File tree

9 files changed

+78
-29
lines changed

9 files changed

+78
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/npm-debug.log
77
/test-workspace/npm-debug.log
88
/test-workspace/test.txt
9+
/test.js

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Usage: npm-run-all [OPTIONS] [...tasks]
3838
-p, --parallel [...tasks] Run a group of tasks in parallel.
3939
-s, --sequential [...tasks] Run a group of tasks sequentially.
4040
-v, --version Print version number.
41+
--silent Set "silent" to the log level of npm.
4142
```
4243

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

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

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

156160
`runAll` returns a promise that becomes *fulfilled* when all tasks are completed.
157161
The promise will become *rejected* when any of the tasks exit with a non-zero code.

src/bin/help.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Usage: npm-run-all [OPTIONS] [...tasks]
2222
-p, --parallel [...tasks] Run a group of tasks in parallel.
2323
-s, --sequential [...tasks] Run a group of tasks in sequencial.
2424
-v, --version Print version number.
25+
--silent Set "silent" to the log level of npm.
2526
2627
See Also:
2728
https://github.com/mysticatea/npm-run-all#readme

src/bin/main.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ function parse(args) {
7373
queue.push({parallel: true, patterns: [], packageConfig});
7474
break;
7575

76+
case "--silent":
77+
// do nothing.
78+
break;
79+
7680
default: {
7781
const matched = OVERWRITE_OPTION.exec(arg);
7882
if (matched) {
@@ -108,19 +112,29 @@ function parse(args) {
108112
*/
109113
export default function npmRunAll(args, stdout, stderr) {
110114
try {
115+
const stdin = process.stdin;
116+
const silent = (
117+
args.indexOf("--silent") !== -1 ||
118+
process.env.npm_config_loglevel === "silent"
119+
);
120+
111121
return parse(args).reduce(
112-
(prev, group) => (group.patterns.length === 0) ?
113-
prev :
114-
prev.then(() => runAll(
115-
group.patterns,
122+
(prev, {patterns, parallel, packageConfig}) => {
123+
if (patterns.length === 0) {
124+
return prev;
125+
}
126+
return prev.then(() => runAll(
127+
patterns,
116128
{
117129
stdout,
118130
stderr,
119-
stdin: process.stdin,
120-
parallel: group.parallel,
121-
packageConfig: group.packageConfig
131+
stdin,
132+
parallel,
133+
packageConfig,
134+
silent
122135
}
123-
)),
136+
));
137+
},
124138
START_PROMISE
125139
);
126140
}

src/lib/npm-run-all.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ function toArray(x) {
3232
*/
3333
function toOverwriteOptions(config) {
3434
const options = [];
35-
if (config == null) {
36-
return options;
37-
}
3835

3936
for (const packageName of Object.keys(config)) {
4037
const packageConfig = config[packageName];
@@ -87,6 +84,9 @@ function toOverwriteOptions(config) {
8784
* Every value is a map-like object (Pairs of variable name and value).
8885
* e.g. `{"npm-run-all": {"test": 777}}`
8986
* Default is `null`.
87+
* @param {boolean} options.silent -
88+
* The flag to set `silent` to the log level of npm.
89+
* Default is `false`.
9090
* @returns {Promise}
9191
* A promise object which becomes fullfilled when all npm-scripts are completed.
9292
*/
@@ -98,7 +98,8 @@ export default function npmRunAll(
9898
stdout = null,
9999
stderr = null,
100100
taskList = null,
101-
packageConfig = null
101+
packageConfig = null,
102+
silent = false
102103
} = {}
103104
) {
104105
try {
@@ -115,10 +116,16 @@ export default function npmRunAll(
115116
throw new Error(`Matched tasks not found: ${patterns.join(", ")}`);
116117
}
117118

118-
return (
119-
parallel ? runTasksInParallel(tasks, stdin, stdout, stderr, toOverwriteOptions(packageConfig)) :
120-
/* else */ runTasksInSequencial(tasks, stdin, stdout, stderr, toOverwriteOptions(packageConfig))
121-
);
119+
const prefixOptions = [];
120+
if (silent) {
121+
prefixOptions.push("--silent");
122+
}
123+
if (packageConfig != null) {
124+
prefixOptions.push(...toOverwriteOptions(packageConfig));
125+
}
126+
127+
const runTasks = parallel ? runTasksInParallel : runTasksInSequencial;
128+
return runTasks(tasks, stdin, stdout, stderr, prefixOptions);
122129
}
123130
catch (err) {
124131
return Promise.reject(new Error(err.message));

src/lib/run-task.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ function detectStreamKind(stream, std) {
4343
* If this is `null`, cannot send.
4444
* If this is `process.stderr`, inherits it.
4545
* Otherwise, makes a pipe.
46-
* @param {string[]} packageConfigOptions -
47-
* `--:=` style options to overwrite package configs.
46+
* @param {string[]} prefixOptions -
47+
* An array of options which are inserted before the task name.
4848
* @returns {Promise}
4949
* A promise object which becomes fullfilled when the npm-script is completed.
5050
* This promise object has an extra method: `abort()`.
5151
* @private
5252
*/
53-
export default function runTask(task, stdin, stdout, stderr, packageConfigOptions) {
53+
export default function runTask(task, stdin, stdout, stderr, prefixOptions) {
5454
let cp = null;
5555
const promise = whichNpm().then(npmPath => new Promise((resolve, reject) => {
5656
const stdinKind = detectStreamKind(stdin, process.stdin);
@@ -60,7 +60,7 @@ export default function runTask(task, stdin, stdout, stderr, packageConfigOption
6060
// Execute.
6161
cp = spawn(
6262
npmPath,
63-
["run-script"].concat(packageConfigOptions, parseArgs(task)),
63+
["run-script"].concat(prefixOptions, parseArgs(task)),
6464
{stdio: [stdinKind, stdoutKind, stderrKind]}
6565
);
6666

src/lib/run-tasks-in-parallel.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ import runTask from "./run-task";
2626
* If this is `null`, cannot send.
2727
* If this is `process.stderr`, inherits it.
2828
* Otherwise, makes a pipe.
29-
* @param {string[]} packageConfigOptions -
30-
* `--:=` style options to overwrite package configs.
29+
* @param {string[]} prefixOptions -
30+
* An array of options which are inserted before the task name.
3131
* @returns {Promise}
3232
* A promise object which becomes fullfilled when all npm-scripts are completed.
3333
* @private
3434
*/
35-
export default function runTasksInParallel(tasks, stdin, stdout, stderr, packageConfigOptions) {
35+
export default function runTasksInParallel(tasks, stdin, stdout, stderr, prefixOptions) {
3636
// When one of tasks exited with non-zero, abort all tasks.
3737
// And wait for all tasks exit.
3838
let nonZeroExited = null;
39-
const taskPromises = tasks.map(task => runTask(task, stdin, stdout, stderr, packageConfigOptions));
39+
const taskPromises = tasks.map(task => runTask(task, stdin, stdout, stderr, prefixOptions));
4040
const parallelPromise = Promise.all(taskPromises.map(p =>
4141
p.then(item => {
4242
if (nonZeroExited == null && item.code) {

src/lib/run-tasks-in-sequencial.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ function rejectIfNonZeroExit(result) {
4040
* If this is `null`, cannot send.
4141
* If this is `process.stderr`, inherits it.
4242
* Otherwise, makes a pipe.
43-
* @param {string[]} packageConfigOptions -
44-
* `--:=` style options to overwrite package configs.
43+
* @param {string[]} prefixOptions -
44+
* An array of options which are inserted before the task name.
4545
* @returns {Promise}
4646
* A promise object which becomes fullfilled when all npm-scripts are completed.
4747
* @private
4848
*/
49-
export default function runTasksInSequencial(tasks, stdin, stdout, stderr, packageConfigOptions) {
49+
export default function runTasksInSequencial(tasks, stdin, stdout, stderr, prefixOptions) {
5050
return tasks.reduce(
5151
(prev, task) => prev.then(result => {
5252
rejectIfNonZeroExit(result);
53-
return runTask(task, stdin, stdout, stderr, packageConfigOptions);
53+
return runTask(task, stdin, stdout, stderr, prefixOptions);
5454
}),
5555
START_PROMISE
5656
).then(rejectIfNonZeroExit);

test/common.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,26 @@ describe("[common] npm-run-all", () => {
104104
it("command version", () => command(["test-task:issue14:posix"]));
105105
});
106106
}
107+
108+
describe("should not print log if silent option was given:", () => {
109+
it("lib version", () => {
110+
const stdout = new BufferStream();
111+
const stderr = new BufferStream();
112+
return runAll("test-task:error", {silent: true, stdout, stderr})
113+
.then(
114+
() => assert(false, "Should fail."),
115+
() => assert(stdout.value === "" && stderr.value === "")
116+
);
117+
});
118+
119+
it("command version", () => {
120+
const stdout = new BufferStream();
121+
const stderr = new BufferStream();
122+
return command(["--silent", "test-task:error"], stdout, stderr)
123+
.then(
124+
() => assert(false, "Should fail."),
125+
() => assert(stdout.value === "" && stderr.value === "")
126+
);
127+
});
128+
});
107129
});

0 commit comments

Comments
 (0)