Skip to content

Commit 7fc6984

Browse files
devsnekBethGriggs
authored andcommitted
repl: check for NODE_REPL_EXTERNAL_MODULE
PR-URL: #29778 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
1 parent d6e69fb commit 7fc6984

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

doc/api/cli.md

+8
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,14 @@ Path to the file used to store the persistent REPL history. The default path is
11781178
`~/.node_repl_history`, which is overridden by this variable. Setting the value
11791179
to an empty string (`''` or `' '`) disables persistent REPL history.
11801180

1181+
### `NODE_REPL_EXTERNAL_MODULE=file`
1182+
<!-- YAML
1183+
added: REPLACEME
1184+
-->
1185+
1186+
Path to a Node.js module which will be loaded in place of the built-in REPL.
1187+
Overriding this value to an empty string (`''`) will use the built-in REPL.
1188+
11811189
### `NODE_TLS_REJECT_UNAUTHORIZED=value`
11821190

11831191
If `value` equals `'0'`, certificate validation is disabled for TLS connections.

lib/internal/main/repl.js

+36-30
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,44 @@ prepareMainThreadExecution();
1919

2020
markBootstrapComplete();
2121

22-
// --input-type flag not supported in REPL
23-
if (getOptionValue('--input-type')) {
24-
// If we can't write to stderr, we'd like to make this a noop,
25-
// so use console.error.
26-
console.error('Cannot specify --input-type for REPL');
27-
process.exit(1);
28-
}
22+
if (process.env.NODE_REPL_EXTERNAL_MODULE) {
23+
require('internal/modules/cjs/loader')
24+
.Module
25+
._load(process.env.NODE_REPL_EXTERNAL_MODULE, undefined, true);
26+
} else {
27+
// --input-type flag not supported in REPL
28+
if (getOptionValue('--input-type')) {
29+
// If we can't write to stderr, we'd like to make this a noop,
30+
// so use console.error.
31+
console.error('Cannot specify --input-type for REPL');
32+
process.exit(1);
33+
}
2934

30-
console.log(`Welcome to Node.js ${process.version}.\n` +
31-
'Type ".help" for more information.');
35+
console.log(`Welcome to Node.js ${process.version}.\n` +
36+
'Type ".help" for more information.');
3237

33-
const cliRepl = require('internal/repl');
34-
cliRepl.createInternalRepl(process.env, (err, repl) => {
35-
if (err) {
36-
throw err;
37-
}
38-
repl.on('exit', () => {
39-
if (repl._flushing) {
40-
repl.pause();
41-
return repl.once('flushHistory', () => {
42-
process.exit();
43-
});
38+
const cliRepl = require('internal/repl');
39+
cliRepl.createInternalRepl(process.env, (err, repl) => {
40+
if (err) {
41+
throw err;
4442
}
45-
process.exit();
43+
repl.on('exit', () => {
44+
if (repl._flushing) {
45+
repl.pause();
46+
return repl.once('flushHistory', () => {
47+
process.exit();
48+
});
49+
}
50+
process.exit();
51+
});
4652
});
47-
});
48-
49-
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
50-
// evaluate the code in the current context.
51-
if (getOptionValue('[has_eval_string]')) {
52-
evalScript('[eval]',
53-
getOptionValue('--eval'),
54-
getOptionValue('--inspect-brk'),
55-
getOptionValue('--print'));
53+
54+
// If user passed '-e' or '--eval' along with `-i` or `--interactive`,
55+
// evaluate the code in the current context.
56+
if (getOptionValue('[has_eval_string]')) {
57+
evalScript('[eval]',
58+
getOptionValue('--eval'),
59+
getOptionValue('--inspect-brk'),
60+
getOptionValue('--print'));
61+
}
5662
}

test/fixtures/external-repl-module.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('42');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const { execSync } = require('child_process');
6+
7+
execSync(process.execPath, {
8+
encoding: 'utf8',
9+
stdio: 'inherit',
10+
env: {
11+
...process.env,
12+
NODE_REPL_EXTERNAL_MODULE: fixtures.path('external-repl-module.js'),
13+
},
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

0 commit comments

Comments
 (0)