| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
var fs = require('fs'); |
| 4 |
var Module = require('module'); |
| 5 |
var path = require('path'); |
| 6 |
var repl = require('repl'); |
| 7 |
var util = require('util'); |
| 8 |
var vm = require('vm'); |
| 9 |
|
| 10 |
var flowRemoveTypes = require('./index'); |
| 11 |
|
| 12 |
var usage = 'Usage: flow-node [options] [ script.js ] [arguments] \n' + |
| 13 |
|
| 14 |
'\nOptions:\n' + |
| 15 |
' -h, --help Show this message\n' + |
| 16 |
' -v, --version Prints the current version of flow-node\n' + |
| 17 |
' -e, --eval script Evaluate script\n' + |
| 18 |
' -p, --print script Evaluate script and print result\n' + |
| 19 |
' -c, --check Syntax check script without executing\n' + |
| 20 |
' -a, --all Interpret all files as flow-typed, not just those with a @flow comment\n'; |
| 21 |
|
| 22 |
// Collect arguments |
| 23 |
var evalScript; |
| 24 |
var printScript; |
| 25 |
var checkSource; |
| 26 |
var all; |
| 27 |
var source; |
| 28 |
var options = []; |
| 29 |
var i = 2; |
| 30 |
while (i < process.argv.length) { |
| 31 |
var arg = process.argv[i++]; |
| 32 |
if (arg === '-h' || arg === '--help') { |
| 33 |
process.stdout.write(usage); |
| 34 |
process.exit(0); |
| 35 |
} else if (arg === '-v' || arg === '--version') { |
| 36 |
process.stdout.write('v' + require('./package').version + '\n'); |
| 37 |
process.exit(0); |
| 38 |
} else if (arg === '-e' || arg === '--eval') { |
| 39 |
evalScript = process.argv[i++]; |
| 40 |
if (!evalScript) { |
| 41 |
process.stderr.write('flow-node: ' + arg + ' requires an argument'); |
| 42 |
return process.exit(1); |
| 43 |
} |
| 44 |
} else if (arg === '-p' || arg === '--print') { |
| 45 |
printScript = process.argv[i++]; |
| 46 |
if (!printScript) { |
| 47 |
process.stderr.write('flow-node: ' + arg + ' requires an argument'); |
| 48 |
return process.exit(1); |
| 49 |
} |
| 50 |
} else if (arg === '-c' || arg === '--check') { |
| 51 |
checkSource = true; |
| 52 |
} else if (arg === '-a' || arg === '--all') { |
| 53 |
all = true; |
| 54 |
} else if (arg[0] === '-' || arg === 'debug') { |
| 55 |
options.push(arg); |
| 56 |
} else { |
| 57 |
source = arg; |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// If node options were provided, forward to another process with the options |
| 63 |
// applied before other arguments. |
| 64 |
if (options.length > 0) { |
| 65 |
var nodePath = process.argv.shift(); |
| 66 |
var nodeArgs = options.concat(process.argv.filter(function (arg) { |
| 67 |
return options.indexOf(arg) === -1; |
| 68 |
})); |
| 69 |
|
| 70 |
const child_process = require('child_process'); |
| 71 |
const proc = child_process.spawn(nodePath, nodeArgs, { stdio: 'inherit' }); |
| 72 |
proc.on('exit', function (code, signal) { |
| 73 |
process.on('exit', function () { |
| 74 |
if (signal) { |
| 75 |
process.kill(process.pid, signal); |
| 76 |
} else { |
| 77 |
process.exit(code); |
| 78 |
} |
| 79 |
}); |
| 80 |
}); |
| 81 |
|
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
require('./register')({ all: all }); |
| 86 |
|
| 87 |
// Evaluate and possibly also print a script. |
| 88 |
if (evalScript || printScript) { |
| 89 |
global.__filename = '[eval]'; |
| 90 |
global.__dirname = process.cwd(); |
| 91 |
var evalModule = new Module(global.__filename); |
| 92 |
evalModule.filename = global.__filename; |
| 93 |
evalModule.paths = Module._nodeModulePaths(global.__dirname); |
| 94 |
global.exports = evalModule.exports; |
| 95 |
global.module = evalModule; |
| 96 |
global.require = evalModule.require.bind(evalModule); |
| 97 |
var result = vm.runInThisContext( |
| 98 |
flowRemoveTypes(evalScript || printScript, { all: true }).toString(), |
| 99 |
{ filename: global.__filename } |
| 100 |
); |
| 101 |
|
| 102 |
if (printScript) { |
| 103 |
process.stdout.write((typeof result === 'string' ? result : util.inspect(result)) + '\n'); |
| 104 |
} |
| 105 |
|
| 106 |
// Or check the source for syntax errors but do not run it. |
| 107 |
} else if (source && checkSource) { |
| 108 |
var code = fs.readFileSync(source, 'utf8'); |
| 109 |
try { |
| 110 |
flowRemoveTypes(code, { all: all }); |
| 111 |
} catch (error) { |
| 112 |
var lines = code.split(/\r\n?|\n|\u2028|\u2029/); |
| 113 |
process.stdout.write(source + ':' + error.loc.line + '\n'); |
| 114 |
process.stdout.write(lines[error.loc.line - 1] + '\n'); |
| 115 |
process.stdout.write(Array(error.loc.column + 1).join(' ') + '^\n'); |
| 116 |
process.stdout.write(error.stack + '\n'); |
| 117 |
return process.exit(1); |
| 118 |
} |
| 119 |
|
| 120 |
// Or run the script. |
| 121 |
} else if (source) { |
| 122 |
var absoluteSource = path.resolve(process.cwd(), source); |
| 123 |
process.argv = [ 'node' ].concat( |
| 124 |
absoluteSource, |
| 125 |
process.argv.slice(i) |
| 126 |
); |
| 127 |
process.execArgv.unshift(__filename); |
| 128 |
Module.runMain(); |
| 129 |
|
| 130 |
// Or begin a REPL. |
| 131 |
} else { |
| 132 |
repl.start({ |
| 133 |
prompt: '> ', |
| 134 |
input: process.stdin, |
| 135 |
output: process.stdout, |
| 136 |
useGlobal: true, |
| 137 |
eval: function (code, context, filename, callback) { |
| 138 |
var error; |
| 139 |
var result; |
| 140 |
try { |
| 141 |
var runCode = flowRemoveTypes(code, { all: true }).toString(); |
| 142 |
try { |
| 143 |
result = vm.runInThisContext(runCode, { filename: filename }); |
| 144 |
} catch (runError) { |
| 145 |
error = runError; |
| 146 |
} |
| 147 |
} catch (transformError) { |
| 148 |
error = repl.Recoverable ? new repl.Recoverable(transformError) : transformError; |
| 149 |
} |
| 150 |
callback(error, result); |
| 151 |
} |
| 152 |
}); |
| 153 |
} |