From 82a030985891ad7efcfb6be381aeb742c1d25c81 Mon Sep 17 00:00:00 2001 From: mde Date: Sun, 19 Apr 2020 12:02:25 -0700 Subject: [PATCH] Implemented CLI --- bin/cli.js | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 + usage.txt | 15 +++++ 3 files changed, 180 insertions(+) create mode 100755 bin/cli.js create mode 100644 usage.txt diff --git a/bin/cli.js b/bin/cli.js new file mode 100755 index 00000000..b94fe2f4 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,162 @@ +#!/usr/bin/env node +/* + * EJS: Embedded JavaScript templating language + * Copyright 2112 Matthew Eernisse (mde@fleegix.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +let program = require('jake').program; +delete global.jake; // NO NOT WANT + +let ejs = require('../lib/ejs'); +let fs = require('fs'); +let args = process.argv.slice(2); +let usage = fs.readFileSync(`${__dirname}/../usage.txt`).toString(); + +const CLI_OPTS = [ + { full: 'debug', + abbr: 'd', + expectValue: false, + allowValue: false, + passThrough: true, + }, + { full: 'delimiter', + abbr: 'm', + expectValue: true, + passThrough: true, + }, + { full: 'openDelimiter', + abbr: 'o', + expectValue: true, + passThrough: true, + }, + { full: 'closeDelimiter', + abbr: 'c', + expectValue: true, + passThrough: true, + }, + { full: 'strict', + abbr: 's', + expectValue: false, + allowValue: false, + passThrough: true, + }, + { full: 'noWith', + abbr: 'n', + expectValue: false, + allowValue: false, + }, + { full: 'localsName', + abbr: 'l', + expectValue: true, + passThrough: true, + }, + { full: 'rmWhitespace', + abbr: 'w', + expectValue: false, + allowValue: false, + passThrough: true, + }, + { full: 'outputFile', + abbr: 'f', + expectValue: true, + }, + { full: 'root', + abbr: 'r', + expectValue: true, + passThrough: true, + }, + { full: 'dataFile', + abbr: 'a', + expectValue: true, + }, + { full: 'help', + abbr: 'h', + passThrough: true, + }, + { full: 'version', + abbr: 'V', + passThrough: true, + }, + // Alias lowercase v + { full: 'version', + abbr: 'v', + passThrough: true, + }, +]; + +let preempts = { + version: function () { + program.die(ejs.VERSION); + }, + help: function () { + program.die(usage); + } +}; + +function run() { + + program.availableOpts = CLI_OPTS; + program.parseArgs(args); + + let pOpts = program.opts; + let pVals = program.envVars; + let templatePath = program.taskNames[0]; + + let opts = {}; + let vals = {}; + + // Same-named 'passthrough' opts + CLI_OPTS.forEach((opt) => { + let optName = opt.full; + if (opt.passThrough && typeof pOpts[optName] != 'undefined') { + opts[optName] = pOpts[optName]; + } + }); + + // Bail out for help/version + for (let p in opts) { + if (preempts[p]) { + return preempts[p](); + } + } + + if (opts.strict) { + pOpts.noWith = true; + } + if (pOpts.noWith) { + opts._with = false; + } + + // Read the data from any data file + if (pOpts.dataFile) { + vals = JSON.parse(fs.readFileSync(pOpts.dataFile).toString()) + } + // Override / set any values passed from the command line + for (p in pVals) { + vals[p] = pVals[p]; + } + + let template = fs.readFileSync(templatePath).toString(); + let output = ejs.render(template, vals, opts); + if (pOpts.outputFile) { + fs.writeFileSync(pOpts.outputFile, output); + } + else { + console.log(output); + } +} + +run(); diff --git a/package.json b/package.json index 57c4ef5c..05b1a30a 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "version": "3.0.2", "author": "Matthew Eernisse (http://fleegix.org)", "license": "Apache-2.0", + "bin": { + "ejs": "./bin/cli.js" + }, "main": "./lib/ejs.js", "repository": { "type": "git", diff --git a/usage.txt b/usage.txt new file mode 100644 index 00000000..690d3cea --- /dev/null +++ b/usage.txt @@ -0,0 +1,15 @@ +EJS: Embedded JavaScript templating +{Usage}: ejs [options ...] templateFile [data variables ...] + +{Options}: + -f, --jakefile FILE Use FILE as the Jakefile. + -C, --directory DIRECTORY Change to DIRECTORY before running tasks. + -q, --quiet Do not log messages to standard output. + -B, --always-make Unconditionally make all targets. + -T/ls, --tasks Display the tasks (matching optional PATTERN) with descriptions, then exit. + -J, --jakelibdir JAKELIBDIR Auto-import any .jake files in JAKELIBDIR. (default is \'jakelib\') + -h, --help Display this help message. + -V/v, --version Display the Jake version. + -ar, --allow-rejection Keep running even after unhandled promise rejection + +