Allow to output either raw or transformed AST, fix several transformation bugs with...
[jst.git] / cli.js
1 #!/usr/bin/env node
2
3 let commander = require('commander')
4 let fs = require('fs')
5 let jst = require('./jst')
6
7 commander
8   .version('0.1.0', '-v, --version')
9   .option('-s, --signature <str>', 'function signature for --wrap', 'async (_require, _pathname, _root)')
10   .option('-b, --bare-returns', 'allow return in top level code', false)
11   .option('-o, --output <ast0|ast1|astring|uglify>', 'output raw or transformed AST, or via astring/UglifyJS', 'astring')
12   .option('-i, --indent <n>', 'indent per level (ast/astring only)', '2')
13   .option('-j, --initial-indent <n>', 'initial indent (astring only)', '0')
14   .option('-c, --compress', 'compress the logic (UglifyJS only)', true)
15   .option('-m, --mangle', 'mangle symbol names (UglifyJS only)', true)
16   .option('-w, --wrap', 'wrap as jst_server CommonJS module', false)
17   .parse(process.argv)
18
19 let text = fs.readFileSync(0, {encoding: 'utf-8'})
20 if (commander.wrap)
21   text = `module.exports=${commander.signature}=>{${text}}`
22 fs.writeSync(
23   1,
24   jst(
25     text,
26     {
27       bare_returns: commander.bareReturns,
28       output: commander.output,
29       ast_options: {
30         indent: parseInt(commander.indent)
31       },
32       astring_options: {
33         indent: ' '.repeat(parseInt(commander.indent)),
34         startingIndentLevel: parseInt(commander.initialIndent)
35       },
36       uglify_options: {
37         compress: commander.compress,
38         mangle: commander.mangle,
39         output: {inline_script: false}
40       }
41     }
42   ),
43   {encoding: 'utf-8'}
44 )