babel/bin/acorn
impinball 31e77b975e Add option: --
Had a few bugs in the initial patch request (#98) as well...

Also, fixed the help() command to print to stderr on nonzero exit statuses given.
2014-05-15 10:32:32 +02:00

54 lines
1.7 KiB
JavaScript
Executable File

#!/usr/bin/env node
var path = require("path");
var fs = require("fs");
var acorn = require("../acorn.js");
var infile, infilecount = 0;
var options = {}, silent = false, compact = false;
var parsed;
function help(status) {
// we want to print to stderr, not stdout, on errors.
var print = (status == 0) ? console.out : console.err;
print("usage: " + path.basename(process.argv[1]) + " [--ecma3|--ecma5] [--strictSemicolons]");
print(" [--locations] [--compact] [--silent] [--help] [--] infile");
process.exit(status);
}
for (var i = 2; i < process.argv.length; ++i) {
var arg = process.argv[i];
if (arg[0] != "-") {
infile = arg;
++infilecount;
continue;
} else if (arg == "--") {
if (i < process.argv.length - 2) help(1); // we have too many remaining `infile`s
infile = process.argv[i + 1]; // we want the *next* argument, not the current one
++infilecount;
break;
}
else if (arg == "--ecma3") options.ecmaVersion = 3;
else if (arg == "--ecma5") options.ecmaVersion = 5;
else if (arg == "--strictSemicolons") options.strictSemicolons = true;
else if (arg == "--locations") options.locations = true;
else if (arg == "--silent") silent = true;
else if (arg == "--compact") compact = true;
else if (arg == "--help") help(0);
else help(1); // we already took care of all arguments without a starting dash
}
// test against counter: we want exactly 1 file. Any more or less should error.
if (infilecount !== 1) help(1);
try {
var code = fs.readFileSync(infile, "utf8");
parsed = acorn.parse(code, options);
} catch(e) {
console.log(e.message);
process.exit(1);
}
if (!silent)
console.log(JSON.stringify(parsed, null, compact ? null : 2));