108 lines
2.8 KiB
JavaScript
108 lines
2.8 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
var roadrunner = require("roadrunner");
|
|
var commander = require("commander");
|
|
var Module = require("module");
|
|
var path = require("path");
|
|
var repl = require("repl");
|
|
var to5 = require("../lib/6to5");
|
|
var util = require("../lib/6to5/util");
|
|
var vm = require("vm");
|
|
var _ = require("lodash");
|
|
|
|
commander.option("-e, --eval [script]", "Evaluate script");
|
|
commander.option("-p, --print", "Evaluate script and print result");
|
|
commander.option("-i, --ignore [regex]", "Ignore all files that match this regex when using the require hook");
|
|
commander.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js]");
|
|
commander.option("-r, --experimental", "Enable experimental support for proposed ES7 features");
|
|
commander.option("-g, --playground", "Enable playground support");
|
|
commander.option("-c, --cache", "Cache compiled files and require paths");
|
|
|
|
var pkg = require("../package.json");
|
|
commander.version(pkg.version);
|
|
commander.usage("[options] [ -e script | script.js ] [arguments]");
|
|
commander.parse(process.argv);
|
|
|
|
if (commander.cache) roadrunner.load();
|
|
|
|
//
|
|
|
|
to5.register({
|
|
experimental: commander.experimental,
|
|
extensions: commander.extensions,
|
|
playground: commander.playground,
|
|
ignore: commander.ignore,
|
|
cache: commander.cache && roadrunner.get("6to5")
|
|
});
|
|
|
|
//
|
|
|
|
var _eval = function (code, filename) {
|
|
code = to5.transform(code, {
|
|
filename: filename,
|
|
blacklist: ["useStrict"],
|
|
experimental: commander.experimental,
|
|
playground: commander.playground
|
|
}).code;
|
|
return vm.runInThisContext(code, filename);
|
|
};
|
|
|
|
if (commander.eval) {
|
|
var result = _eval(commander.eval, "eval");
|
|
if (commander.print) console.log(result);
|
|
} else {
|
|
if (commander.args.length) {
|
|
// slice all arguments up to the first filename since they're 6to5 args that we handle
|
|
var args = process.argv.slice(2);
|
|
|
|
var i = 0;
|
|
_.each(args, function (arg, i2) {
|
|
if (arg[0] !== "-") {
|
|
i = i2;
|
|
return false;
|
|
}
|
|
});
|
|
args = args.slice(i);
|
|
|
|
// make the filename absolute
|
|
var filename = args[0]
|
|
if (!util.isAbsolute(filename)) args[0] = path.join(process.cwd(), filename);
|
|
|
|
// add back on node and concat the sliced args
|
|
process.argv = ["node"].concat(args);
|
|
|
|
Module.runMain();
|
|
} else {
|
|
replStart();
|
|
}
|
|
}
|
|
|
|
if (commander.cache) roadrunner.save();
|
|
|
|
function replStart() {
|
|
repl.start({
|
|
prompt: "> ",
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
eval: replEval,
|
|
useGlobal: true
|
|
});
|
|
}
|
|
|
|
function replEval(code, context, filename, callback) {
|
|
var err;
|
|
var result;
|
|
|
|
try {
|
|
if (/^\((.*?)\n\)$/.test(code)) {
|
|
code = code.slice(1, -2); // remove "(" and "\n)"
|
|
}
|
|
|
|
result = _eval(code, filename);
|
|
} catch (e) {
|
|
err = e;
|
|
}
|
|
|
|
callback(err, result);
|
|
}
|