Change from pnpm to npm, add ./link.sh shortcut for npm style package linking
[jstize.git] / cli.js
1 #!/usr/bin/env node
2
3 let commander = require('commander')
4 let fs = require('fs')
5 let jstize = require('./jstize')
6
7 commander
8   .version('0.1.0', '-v, --version')
9   .option('-i, --indent <n>', 'indent per level', '2')
10   .option('-j, --initial-indent <n>', 'initial indent', '0')
11   .option('-n, --name <str>', 'message for --wrap', 'page.jst')
12   .option('-w, --wrap', 'wrap as jst_server template function')
13   .parse(process.argv)
14
15 let indent = parseInt(commander.indent)
16 let initial_indent = parseInt(commander.initialIndent)
17 let wrap = commander.wrap || false
18 let text = jstize(
19   fs.readFileSync(0, {encoding: 'utf-8'}),
20   {
21     indent: indent,
22     initial_indent: initial_indent + wrap
23   }
24 )
25 if (wrap)
26   text = `${' '.repeat(indent * initial_indent)}return async env => {
27 ${' '.repeat(indent * (initial_indent + 1))}let _out = []
28 ${text}${' '.repeat(indent * (initial_indent + 1))}env.site.serve(env, 200, Buffer.from(_out.join('')), '${commander.name}')
29 ${' '.repeat(indent * initial_indent)}}
30 `
31 fs.writeSync(1, text, {encoding: 'utf-8'})