Skip to content

Commit

Permalink
Camelize all CLI opts internally
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Apr 24, 2020
1 parent 40daa57 commit 0f3eb48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,21 @@ function run() {
program.availableOpts = CLI_OPTS;
program.parseArgs(args);

let pOpts = program.opts;
let pVals = program.envVars;
let templatePath = program.taskNames[0];
let pVals = program.envVars;
let pOpts = {};

for (let p in program.opts) {
let name = p.replace(/-[a-z]/g, (match) => { return match[1].toUpperCase(); });
pOpts[name] = program.opts[p];
}

let opts = {};
let vals = {};

// Same-named 'passthrough' opts
CLI_OPTS.forEach((opt) => {
let optName = opt.full.replace(/-[a-z]/g, (match) => { return match[1].toUpperCase(); });
let optName = opt.full;
if (opt.passThrough && typeof pOpts[optName] != 'undefined') {
opts[optName] = pOpts[optName];
}
Expand All @@ -142,8 +147,8 @@ function run() {
}

// Read the data from any data file
if (pOpts['data-file']) {
vals = JSON.parse(fs.readFileSync(pOpts['data-file']).toString());
if (pOpts.dataFile) {
vals = JSON.parse(fs.readFileSync(pOpts.dataFile).toString());
}
// Override / set any values passed from the command line
for (let p in pVals) {
Expand All @@ -152,8 +157,8 @@ function run() {

let template = fs.readFileSync(templatePath).toString();
let output = ejs.render(template, vals, opts);
if (pOpts['output-file']) {
fs.writeFileSync(pOpts['output-file'], output);
if (pOpts.outputFile) {
fs.writeFileSync(pOpts.outputFile, output);
}
else {
process.stdout.write(output);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"bugs": "https://github.com/mde/ejs/issues",
"homepage": "https://github.com/mde/ejs",
"dependencies": {
"jake": "^10.5.2"
"jake": "^10.6.1"
},
"devDependencies": {
"browserify": "^16.5.1",
Expand Down

0 comments on commit 0f3eb48

Please sign in to comment.