-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.js
94 lines (81 loc) · 2.75 KB
/
exec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* A lot of this entry file is based off of node's src/node.js.
*/
// grab a reference to the entry point script, then clean up the env
var entryPoint = process.env.NODE6_ENTRY_POINT;
delete process.env.NODE6_ENTRY_POINT;
var Repl = require('./repl')
var transpile = require('./').transpile
// load the custom `.js` ES6 Generator compiler
var node6 = require('./')
// overwrite process.execPath and process.argv[0] with the
// absolute path to the gnode binary
process.execPath = process.argv[0] = require('path').resolve(__dirname, 'bin', 'node6');
// remove "wrap.js" from `process.argv`
process.argv.splice(1, 1);
if (process._eval != null) {
// User passed '-e' or '--eval' arguments to Node.
evalScript('[eval]');
} else if (entryPoint) {
// replace `process.argv[1]` with the expected path value,
// and re-run Module.runMain()
process.argv.splice(1, 0, require('path').resolve(entryPoint));
node6(entryPoint)
require('module').runMain();
} else {
console.error('node6 repl currently unsupported. Sorry!\nNote -e and -p flags work though.')
process.exit(1)
// run the REPL, or run from stdin
// If -i or --interactive were passed, or stdin is a TTY.
if (process._forceRepl || require('tty').isatty(0)) {
var opts = {}
if (parseInt(process.env.NODE_NO_READLINE, 10)) {
opts.terminal = false;
}
if (parseInt(process.env.NODE_DISABLE_COLORS, 10)) {
opts.useColors = false;
}
var repl = Repl(opts);
repl.on('exit', function() {
process.exit();
});
} else {
// Read all of stdin - execute it.
process.stdin.resume();
process.stdin.setEncoding('utf8');
var code = '';
process.stdin.on('data', function(d) {
code += d;
});
process.stdin.on('end', function() {
process._eval = code;
evalScript('[stdin]');
});
}
}
// copied (almost) directly from joyent/node's src/node.js
function evalScript (name) {
var Module = require('module');
var path = require('path');
var cwd = process.cwd();
var module = new Module(name);
module.filename = path.join(cwd, name);
module.paths = Module._nodeModulePaths(cwd);
var script = process._eval;
// transpile js
script = transpile(script)
if (!Module._contextLoad) {
var body = script;
script = 'global.__filename = ' + JSON.stringify(name) + ';\n' +
'global.exports = exports;\n' +
'global.module = module;\n' +
'global.__dirname = __dirname;\n' +
'global.require = require;\n' +
'return require("vm").runInThisContext(' +
JSON.stringify(body) + ', ' +
JSON.stringify(name) + ', true);\n';
}
var result = module._compile(script, name + '-wrapper');
if (process._print_eval) console.log(result);
return result
}