Browse Source

node: allow preload modules with -i

This gives us the ability to preload when using the node repl. This can
be useful for doing things like creating aliases.

Fixes: https://github.com/nodejs/node/issues/4661
PR-URL: https://github.com/nodejs/node/pull/4696
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
process-exit-stdio-flushing
Evan Lucas 9 years ago
parent
commit
ff64a4c395
  1. 1
      src/node.js
  2. 1
      test/fixtures/define-global.js
  3. 15
      test/parallel/test-preload.js

1
src/node.js

@ -140,6 +140,7 @@
} }
} else { } else {
startup.preloadModules();
// If -i or --interactive were passed, or stdin is a TTY. // If -i or --interactive were passed, or stdin is a TTY.
if (process._forceRepl || NativeModule.require('tty').isatty(0)) { if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
// REPL // REPL

1
test/fixtures/define-global.js

@ -0,0 +1 @@
global.a = 'test';

15
test/parallel/test-preload.js

@ -1,5 +1,5 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const path = require('path'); const path = require('path');
const child_process = require('child_process'); const child_process = require('child_process');
@ -21,6 +21,7 @@ var fixture = function(name) {
var fixtureA = fixture('printA.js'); var fixtureA = fixture('printA.js');
var fixtureB = fixture('printB.js'); var fixtureB = fixture('printB.js');
var fixtureC = fixture('printC.js'); var fixtureC = fixture('printC.js');
const fixtureD = fixture('define-global.js');
var fixtureThrows = fixture('throws_error4.js'); var fixtureThrows = fixture('throws_error4.js');
// test preloading a single module works // test preloading a single module works
@ -73,6 +74,18 @@ child_process.exec(nodeBinary + ' '
assert.equal(stdout, 'A\nB\nhello\n'); assert.equal(stdout, 'A\nB\nhello\n');
}); });
// test that preload works with -i
const interactive = child_process.exec(nodeBinary + ' '
+ preloadOption([fixtureD])
+ '-i',
common.mustCall(function(err, stdout, stderr) {
assert.ifError(err);
assert.strictEqual(stdout, `> 'test'\n> `);
}));
interactive.stdin.write('a\n');
interactive.stdin.write('process.exit()\n');
child_process.exec(nodeBinary + ' ' child_process.exec(nodeBinary + ' '
+ '--require ' + fixture('cluster-preload.js') + ' ' + '--require ' + fixture('cluster-preload.js') + ' '
+ fixture('cluster-preload-test.js'), + fixture('cluster-preload-test.js'),

Loading…
Cancel
Save