Skip to content

Commit

Permalink
Implemented CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Apr 19, 2020
1 parent e7ae463 commit 82a0309
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
162 changes: 162 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -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();
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"version": "3.0.2",
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)",
"license": "Apache-2.0",
"bin": {
"ejs": "./bin/cli.js"
},
"main": "./lib/ejs.js",
"repository": {
"type": "git",
Expand Down
15 changes: 15 additions & 0 deletions usage.txt
Original file line number Diff line number Diff line change
@@ -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


0 comments on commit 82a0309

Please sign in to comment.