UNPKG

1.24 kBPlain TextView Raw
1#!/usr/bin/env node
2
3var path = require('path');
4var fs = require('fs');
5var acorn = require(path.join(path.dirname(fs.realpathSync(__filename)), "../acorn.js"));
6
7var infile, parsed, options = {}, silent = false, compact = false;
8
9function help(status) {
10 console.log("usage: " + path.basename(process.argv[1]) + " infile [--ecma3|--ecma5] [--strictSemicolons]");
11 console.log(" [--trackComments] [--locations] [--compact] [--silent] [--help]");
12 process.exit(status);
13}
14
15for (var i = 2; i < process.argv.length; ++i) {
16 var arg = process.argv[i];
17 if (arg == "--ecma3") options.ecmaVersion = 3;
18 else if (arg == "--ecma5") options.ecmaVersion = 5;
19 else if (arg == "--strictSemicolons") options.strictSemicolons = true;
20 else if (arg == "--locations") options.locations = true;
21 else if (arg == "--silent") silent = true;
22 else if (arg == "--compact") compact = true;
23 else if (arg == "--help") help(0);
24 else if (arg[0] == "-") help(1);
25 else infile = arg;
26}
27
28if (!infile) help(1);
29
30try {
31 var code = fs.readFileSync(infile, "utf8");
32 parsed = acorn.parse(code, options);
33} catch(e) {
34 console.log(e.message);
35 process.exit(1);
36}
37
38if (!silent)
39 console.log(JSON.stringify(parsed, null, compact ? null : 2));