Skip to content

Commit

Permalink
node: allow preload modules with -i
Browse files Browse the repository at this point in the history
This gives us the ability to preload when using the node repl. This can
be useful for doing things like creating aliases.

Fixes: #4661
PR-URL: #4696
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
evanlucas committed Jan 18, 2016
1 parent 6988d2e commit 848b04b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
}

} else {
startup.preloadModules();
// If -i or --interactive were passed, or stdin is a TTY.
if (process._forceRepl || NativeModule.require('tty').isatty(0)) {
// REPL
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/define-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.a = 'test';
15 changes: 14 additions & 1 deletion test/parallel/test-preload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const path = require('path');
const child_process = require('child_process');
Expand All @@ -21,6 +21,7 @@ var fixture = function(name) {
var fixtureA = fixture('printA.js');
var fixtureB = fixture('printB.js');
var fixtureC = fixture('printC.js');
const fixtureD = fixture('define-global.js');
var fixtureThrows = fixture('throws_error4.js');

// test preloading a single module works
Expand Down Expand Up @@ -73,6 +74,18 @@ child_process.exec(nodeBinary + ' '
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 + ' '
+ '--require ' + fixture('cluster-preload.js') + ' '
+ fixture('cluster-preload-test.js'),
Expand Down

0 comments on commit 848b04b

Please sign in to comment.