Skip to content

Commit 5db4c54

Browse files
jcbhmrtargos
authored andcommitted
bootstrap: print --help message using console.log
PR-URL: #51463 Fixes: #51448 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 67fcb6b commit 5db4c54

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

lib/internal/main/print_help.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ function format(
202202

203203
function print(stream) {
204204
const { options, aliases } = getCLIOptionsInfo();
205+
const console = require('internal/console/global');
205206

206207
// Use 75 % of the available width, and at least 70 characters.
207208
const width = MathMax(70, (stream.columns || 0) * 0.75);
@@ -212,21 +213,22 @@ function print(stream) {
212213
'(default if no file name is provided, ' +
213214
'interactive mode if a tty)' });
214215
options.set('--', { helpText: 'indicate the end of node options' });
215-
stream.write(
216+
let helpText = (
216217
'Usage: node [options] [ script.js ] [arguments]\n' +
217218
' node inspect [options] [ script.js | host:port ] [arguments]\n\n' +
218219
'Options:\n');
219-
stream.write(indent(format({
220+
helpText += (indent(format({
220221
options, aliases, firstColumn, secondColumn,
221222
}), 2));
222223

223-
stream.write('\nEnvironment variables:\n');
224+
helpText += ('\nEnvironment variables:\n');
224225

225-
stream.write(format({
226+
helpText += (format({
226227
options: envVars, firstColumn, secondColumn,
227228
}));
228229

229-
stream.write('\nDocumentation can be found at https://nodejs.org/\n');
230+
helpText += ('\nDocumentation can be found at https://nodejs.org/');
231+
console.log(helpText);
230232
}
231233

232234
prepareMainThreadExecution();

test/parallel/test-cli-node-print-help.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const common = require('../common');
77
// returns the proper set of cli options when invoked
88

99
const assert = require('assert');
10-
const { exec } = require('child_process');
10+
const { exec, spawn } = require('child_process');
11+
const { once } = require('events');
1112
let stdOut;
1213

1314

@@ -53,3 +54,13 @@ function testForSubstring(options) {
5354
}
5455

5556
startPrintHelpTest();
57+
58+
// Test closed stdout for `node --help`. Like `node --help | head -n5`.
59+
(async () => {
60+
const cp = spawn(process.execPath, ['--help'], {
61+
stdio: ['inherit', 'pipe', 'inherit'],
62+
});
63+
cp.stdout.destroy();
64+
const [exitCode] = await once(cp, 'exit');
65+
assert.strictEqual(exitCode, 0);
66+
})().then(common.mustCall());

0 commit comments

Comments
 (0)