Skip to content

Commit 594b5d7

Browse files
ebraminioaddaleax
authored andcommitted
cmd: support dash as stdin alias
PR-URL: #13012 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
1 parent 7a1dc1f commit 594b5d7

File tree

7 files changed

+71
-29
lines changed

7 files changed

+71
-29
lines changed

doc/api/cli.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To view this documentation as a manual page in a terminal, run `man node`.
1010

1111
## Synopsis
1212

13-
`node [options] [v8 options] [script.js | -e "script"] [--] [arguments]`
13+
`node [options] [v8 options] [script.js | -e "script" | -] [--] [arguments]`
1414

1515
`node debug [script.js | -e "script" | <host>:<port>] …`
1616

@@ -345,6 +345,17 @@ added: v0.11.15
345345

346346
Specify ICU data load path. (overrides `NODE_ICU_DATA`)
347347

348+
349+
### `-`
350+
<!-- YAML
351+
added: REPLACEME
352+
-->
353+
354+
Alias for stdin, analogous to the use of - in other command line utilities,
355+
meaning that the script will be read from stdin, and the rest of the options
356+
are passed to that script.
357+
358+
348359
### `--`
349360
<!-- YAML
350361
added: v7.5.0

doc/api/synopsis.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<!--type=misc-->
44

5-
`node [options] [v8 options] [script.js | -e "script"] [arguments]`
5+
`node [options] [v8 options] [script.js | -e "script" | - ] [arguments]`
66

77
Please see the [Command Line Options][] document for information about
88
different options and ways to run scripts with Node.js.

doc/node.1

+10-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ node \- Server-side JavaScript runtime
3636
.RI [ v8\ options ]
3737
.RI [ script.js \ |
3838
.B -e
39-
.RI \&" script \&"]
39+
.RI \&" script \&"
40+
.R |
41+
.B -
42+
.R ]
4043
.B [--]
4144
.RI [ arguments ]
4245
.br
@@ -225,6 +228,12 @@ See \fBSSL_CERT_DIR\fR and \fBSSL_CERT_FILE\fR.
225228
.BR \-\-icu\-data\-dir =\fIfile\fR
226229
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)
227230

231+
.TP
232+
.BR \-\fR
233+
Alias for stdin, analogous to the use of - in other command line utilities,
234+
meaning that the script will be read from stdin, and the rest of the options
235+
are passed to that script.
236+
228237
.TP
229238
.BR \-\-\fR
230239
Indicate the end of node options. Pass the rest of the arguments to the script.

lib/internal/bootstrap_node.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
const internalModule = NativeModule.require('internal/module');
124124
internalModule.addBuiltinLibsToObject(global);
125125
evalScript('[eval]');
126-
} else if (process.argv[1]) {
126+
} else if (process.argv[1] && process.argv[1] !== '-') {
127127
// make process.argv[1] into a full path
128128
const path = NativeModule.require('path');
129129
process.argv[1] = path.resolve(process.argv[1]);

src/node.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -3592,7 +3592,7 @@ void LoadEnvironment(Environment* env) {
35923592
static void PrintHelp() {
35933593
// XXX: If you add an option here, please also add it to doc/node.1 and
35943594
// doc/api/cli.md
3595-
printf("Usage: node [options] [ -e script | script.js ] [arguments]\n"
3595+
printf("Usage: node [options] [ -e script | script.js | - ] [arguments]\n"
35963596
" node inspect script.js [arguments]\n"
35973597
"\n"
35983598
"Options:\n"
@@ -3604,6 +3604,8 @@ static void PrintHelp() {
36043604
" does not appear to be a terminal\n"
36053605
" -r, --require module to preload (option can be "
36063606
"repeated)\n"
3607+
" - script read from stdin (default; "
3608+
"interactive mode if a tty)"
36073609
#if HAVE_INSPECTOR
36083610
" --inspect[=[host:]port] activate inspector on host:port\n"
36093611
" (default: 127.0.0.1:9229)\n"
@@ -3913,6 +3915,8 @@ static void ParseArgs(int* argc,
39133915
} else if (strcmp(arg, "--expose-internals") == 0 ||
39143916
strcmp(arg, "--expose_internals") == 0) {
39153917
config_expose_internals = true;
3918+
} else if (strcmp(arg, "-") == 0) {
3919+
break;
39163920
} else if (strcmp(arg, "--") == 0) {
39173921
index += 1;
39183922
break;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const expected = '--option-to-be-seen-on-child';
6+
7+
const { spawn } = require('child_process');
8+
const child = spawn(process.execPath, ['-', expected], { stdio: 'pipe' });
9+
10+
child.stdin.end('console.log(process.argv[2])');
11+
12+
let actual = '';
13+
child.stdout.setEncoding('utf8');
14+
child.stdout.on('data', (chunk) => actual += chunk);
15+
child.stdout.on('end', common.mustCall(() => {
16+
assert.strictEqual(actual.trim(), expected);
17+
}));

test/parallel/test-stdin-script-child.js

+25-24
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22
const common = require('../common');
33
const assert = require('assert');
44

5-
const spawn = require('child_process').spawn;
6-
const child = spawn(process.execPath, [], {
7-
env: Object.assign(process.env, {
8-
NODE_DEBUG: process.argv[2]
9-
})
10-
});
11-
const wanted = `${child.pid}\n`;
12-
let found = '';
5+
const { spawn } = require('child_process');
6+
for (const args of [[], ['-']]) {
7+
const child = spawn(process.execPath, args, {
8+
env: Object.assign(process.env, {
9+
NODE_DEBUG: process.argv[2]
10+
})
11+
});
12+
const wanted = `${child.pid}\n`;
13+
let found = '';
1314

14-
child.stdout.setEncoding('utf8');
15-
child.stdout.on('data', function(c) {
16-
found += c;
17-
});
15+
child.stdout.setEncoding('utf8');
16+
child.stdout.on('data', function(c) {
17+
found += c;
18+
});
1819

19-
child.stderr.setEncoding('utf8');
20-
child.stderr.on('data', function(c) {
21-
console.error(`> ${c.trim().split(/\n/).join('\n> ')}`);
22-
});
20+
child.stderr.setEncoding('utf8');
21+
child.stderr.on('data', function(c) {
22+
console.error(`> ${c.trim().split(/\n/).join('\n> ')}`);
23+
});
2324

24-
child.on('close', common.mustCall(function(c) {
25-
assert.strictEqual(c, 0);
26-
assert.strictEqual(found, wanted);
27-
console.log('ok');
28-
}));
25+
child.on('close', common.mustCall(function(c) {
26+
assert.strictEqual(c, 0);
27+
assert.strictEqual(found, wanted);
28+
}));
2929

30-
setTimeout(function() {
31-
child.stdin.end('console.log(process.pid)');
32-
}, 1);
30+
setTimeout(function() {
31+
child.stdin.end('console.log(process.pid)');
32+
}, 1);
33+
}

0 commit comments

Comments
 (0)