--- /dev/null
+#!/usr/bin/env node
+
+let commander = require('commander')
+let fs = require('fs')
+let jst = require('./jst')
+
+commander
+ .version('0.1.0', '-v, --version')
+ .option('-s, --signature <str>', 'function signature for --wrap [async (_require, _pathname, _root)]', 'async (_require, _pathname, _root)')
+ .option('-b, --bare-returns', 'allow return in top level code', false)
+ .option('-i, --indent <n>', 'indent [2]', '2')
+ .option('-j, --initial-indent <n>', 'initial indent level [0]', '0')
+ .option('-w, --wrap', 'wrap as jst_server CommonJS module', false)
+ .parse(process.argv)
+
+let text = fs.readFileSync(0, {encoding: 'utf-8'})
+if (commander.wrap)
+ text = `module.exports=${commander.signature}=>{${text}}`
+fs.writeSync(
+ 1,
+ jst(
+ text,
+ {
+ bare_returns: commander.bareReturns,
+ indent: parseInt(commander.indent),
+ initial_indent: parseInt(commander.initialIndent)
+ }
+ ),
+ {encoding: 'utf-8'}
+)
/*
* Copyright (C) 2018 Nick Downing <nick@ndcode.org>
* SPDX-License-Identifier: MIT
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* IN THE SOFTWARE.
*/
-let BuildCache = require('@ndcode/build_cache')
let acorn = require('./dist/acorn')
-let assert = require('assert')
let astring = require('astring')
-let disk_build = require('@ndcode/disk_build')
-let fs = require('fs')
-let path = require('path')
let transform = require('./transform')
let visitors = require('./visitors')
-let util = require('util')
-let fs_readFile = util.promisify(fs.readFile)
-let fs_writeFile = util.promisify(fs.writeFile)
-
-let build_cache = new BuildCache()
-let jst = (pathname, root, args) => /*await*/ build_cache.get(
- pathname,
- async result => {
- let arg_names = ['_require', '_pathname', '_root']
- let arg_values = [
- async require_pathname => {
- let temp = path.posix.dirname(pathname)
- while (require_pathname.charAt(0) === '/') {
- temp = root
- require_pathname = require_pathname.slice(1)
- }
- require_pathname = path.posix.resolve(temp, require_pathname)
- return jst(require_pathname, root, args)
- },
- pathname,
- root
- ]
- if (args !== undefined)
- for (let i in args)
- if (Object.prototype.hasOwnProperty.call(args, i)) {
- arg_names.push(i)
- arg_values.push(args[i])
- }
-
- let render = await disk_build(
- pathname,
- async temp_pathname => {
- let text = await fs_readFile(pathname, {encoding: 'utf-8'})
- return /*await*/ fs_writeFile(
- temp_pathname,
- astring.generate(
- transform.transform(
- visitors,
- acorn.parse(
- `module.exports = async (${arg_names.join(', ')}) => {${text}}`
- )
- ),
- {} //indent: ''}
- ),
- {encoding: 'utf-8'}
- )
- },
- true
- )
- assert(render.deps.length === 0)
- let full_pathname = require.resolve(render.pathname)
- delete require.cache[full_pathname]
- result.value = await (require(full_pathname)).apply(null, arg_values)
- }
-)
+let jst = (text, options) => {
+ options = Object.assign(
+ {bare_returns: true, indent: 2, initial_indent: 0},
+ options || {}
+ )
+ return astring.generate(
+ transform.transform(
+ visitors,
+ acorn.parse(text, {allowReturnOutsideFunction: options.bare_returns})
+ ),
+ {
+ indent: ' '.repeat(options.indent),
+ startingIndentLevel: options.initial_indent
+ }
+ )
+}
module.exports = jst