diff --git a/README.md b/README.md index 151bf23344..86db7ab121 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,20 @@ Compile the file `script.js` and output it to stdout. $ 6to5 script.js +#### Node + +Launch a repl. + + $ 6to5-node + +Evaluate code. + + $ 6to5-node -e "class Test { }" + +Compile and run `test.js`. + + $ 6to5-node test + ### Browserify $ browserify script.js -t 6to5/browserify --outfile bundle.js @@ -166,3 +180,8 @@ Cannot subclass built-ins such as `Date`, `Array`, `DOM` etc. ## Comparison to Traceur ### Performance + +## The future + +Implement own parser and generator that preserves whitespace and automatically +works out generation rules based on code input. diff --git a/bin/6to5 b/bin/6to5 index cb0ffa4d1a..a414b72e84 100755 --- a/bin/6to5 +++ b/bin/6to5 @@ -3,9 +3,9 @@ var commander = require("commander"); var readdir = require("fs-readdir-recursive"); var mkdirp = require("mkdirp"); -var to5 = require("../lib/6to5/node"); var path = require("path"); var util = require("../lib/6to5/util"); +var to5 = require("../lib/6to5/node"); var fs = require("fs"); var _ = require("lodash"); diff --git a/bin/6to5-node b/bin/6to5-node new file mode 100755 index 0000000000..2932cc8dd3 --- /dev/null +++ b/bin/6to5-node @@ -0,0 +1,61 @@ +#!/usr/bin/env node + +var commander = require("commander"); +var path = require("path"); +var to5 = require("../lib/6to5/node"); +var util = require("../lib/6to5/util"); +var path = require("path"); +var repl = require("repl"); +var vm = require("vm"); +var _ = require("lodash"); + +commander.option("-e, --eval [script]", "evaluate script"); +commander.option("-p, --print", "evaluate script and print result"); + +var pkg = require("../package.json"); +commander.version(pkg.version); +commander.usage("[options] [ -e script | script.js ] [arguments]"); +commander.parse(process.argv); + +to5.register(); + +if (commander.eval) { + var code = to5.transform(commander.eval, { filename: "eval" }); + var result = eval(code); + if (commander.print) console.log(result); +} else { + var filenames = commander.args; + + if (filenames.length) { + _.each(filenames, function (filename) { + require(require.resolve(path.join(process.cwd(), filename))); + }); + } else { + replStart(); + } +} + +function replStart() { + repl.start({ + prompt: "> ", + input: process.stdin, + output: process.stdout, + eval: replEval + }); +} + +function replEval(code, context, filename, callback) { + var err; + var result; + + try { + code = code.slice(1, -2); // remove "(" and "\n)" + code = to5.transform(code, { filename: filename }); + + result = vm.runInContext(code, context, filename, { displayErrors: false }); + } catch (e) { + err = e; + } + + callback(err, result); +} diff --git a/lib/6to5/util.js b/lib/6to5/util.js index f73fe6db7e..2c67d6169e 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -85,10 +85,10 @@ exports.buildUidGenerator = function () { var ids = {}; return function (name) { - var i = ids[name] || 0; + var i = ids[name] || 1; var id = "_" + name; - if (i > 0) id += i; + if (i > 1) id += i; ids[name] = i + 1; return id; }; diff --git a/package.json b/package.json index 3dabfab995..3c9934ed81 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "preferGlobal": true, "main": "lib/6to5/node.js", "bin": { - "6to5": "./bin/6to5" + "6to5": "./bin/6to5", + "6to5-node": "./bin/6to5-node" }, "scripts": { "test": "mocha"