From: Nick Downing Date: Tue, 20 Nov 2018 00:19:14 +0000 (+1100) Subject: Remove caching facility, jst() now does bare text-to-text conversion, add CLI X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=f4ca05a679900cb4332553d6a7b00fd1061fd61c;p=jst.git Remove caching facility, jst() now does bare text-to-text conversion, add CLI --- diff --git a/cli.js b/cli.js new file mode 100755 index 0000000..07fc5df --- /dev/null +++ b/cli.js @@ -0,0 +1,30 @@ +#!/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 ', '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 ', 'indent [2]', '2') + .option('-j, --initial-indent ', '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'} +) diff --git a/jst.js b/jst.js index 2e26ba8..94f72ed 100644 --- a/jst.js +++ b/jst.js @@ -1,17 +1,17 @@ /* * Copyright (C) 2018 Nick Downing * 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 @@ -21,70 +21,26 @@ * 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 diff --git a/package.json b/package.json index d3c3bdb..525a5aa 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,9 @@ "bugs": { "email": "nick@ndcode.org" }, + "bin": { + "jst": "./cli.js" + }, "main": "jst.js", "engines": { "node": ">=0.4.0"