| 1 | #!/usr/bin/env node
|
|---|
| 2 | /*
|
|---|
| 3 | * EJS Embedded JavaScript templates
|
|---|
| 4 | * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
|
|---|
| 5 | *
|
|---|
| 6 | * Licensed under the Apache License, Version 2.0 (the "License");
|
|---|
| 7 | * you may not use this file except in compliance with the License.
|
|---|
| 8 | * You may obtain a copy of the License at
|
|---|
| 9 | *
|
|---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 11 | *
|
|---|
| 12 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 15 | * See the License for the specific language governing permissions and
|
|---|
| 16 | * limitations under the License.
|
|---|
| 17 | *
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | let path = require('path');
|
|---|
| 21 |
|
|---|
| 22 | let program = require('jake').program;
|
|---|
| 23 | delete global.jake; // NO NOT WANT
|
|---|
| 24 | program.setTaskNames = function (n) { this.taskNames = n; };
|
|---|
| 25 |
|
|---|
| 26 | let ejs = require('../lib/ejs');
|
|---|
| 27 | let { hyphenToCamel } = require('../lib/utils');
|
|---|
| 28 | let fs = require('fs');
|
|---|
| 29 | let args = process.argv.slice(2);
|
|---|
| 30 | let usage = fs.readFileSync(`${__dirname}/../usage.txt`).toString();
|
|---|
| 31 |
|
|---|
| 32 | const CLI_OPTS = [
|
|---|
| 33 | { full: 'output-file',
|
|---|
| 34 | abbr: 'o',
|
|---|
| 35 | expectValue: true,
|
|---|
| 36 | },
|
|---|
| 37 | { full: 'data-file',
|
|---|
| 38 | abbr: 'f',
|
|---|
| 39 | expectValue: true,
|
|---|
| 40 | },
|
|---|
| 41 | { full: 'data-input',
|
|---|
| 42 | abbr: 'i',
|
|---|
| 43 | expectValue: true,
|
|---|
| 44 | },
|
|---|
| 45 | { full: 'delimiter',
|
|---|
| 46 | abbr: 'm',
|
|---|
| 47 | expectValue: true,
|
|---|
| 48 | passThrough: true,
|
|---|
| 49 | },
|
|---|
| 50 | { full: 'open-delimiter',
|
|---|
| 51 | abbr: 'p',
|
|---|
| 52 | expectValue: true,
|
|---|
| 53 | passThrough: true,
|
|---|
| 54 | },
|
|---|
| 55 | { full: 'close-delimiter',
|
|---|
| 56 | abbr: 'c',
|
|---|
| 57 | expectValue: true,
|
|---|
| 58 | passThrough: true,
|
|---|
| 59 | },
|
|---|
| 60 | { full: 'strict',
|
|---|
| 61 | abbr: 's',
|
|---|
| 62 | expectValue: false,
|
|---|
| 63 | allowValue: false,
|
|---|
| 64 | passThrough: true,
|
|---|
| 65 | },
|
|---|
| 66 | { full: 'no-with',
|
|---|
| 67 | abbr: 'n',
|
|---|
| 68 | expectValue: false,
|
|---|
| 69 | allowValue: false,
|
|---|
| 70 | },
|
|---|
| 71 | { full: 'locals-name',
|
|---|
| 72 | abbr: 'l',
|
|---|
| 73 | expectValue: true,
|
|---|
| 74 | passThrough: true,
|
|---|
| 75 | },
|
|---|
| 76 | { full: 'rm-whitespace',
|
|---|
| 77 | abbr: 'w',
|
|---|
| 78 | expectValue: false,
|
|---|
| 79 | allowValue: false,
|
|---|
| 80 | passThrough: true,
|
|---|
| 81 | },
|
|---|
| 82 | { full: 'debug',
|
|---|
| 83 | abbr: 'd',
|
|---|
| 84 | expectValue: false,
|
|---|
| 85 | allowValue: false,
|
|---|
| 86 | passThrough: true,
|
|---|
| 87 | },
|
|---|
| 88 | { full: 'help',
|
|---|
| 89 | abbr: 'h',
|
|---|
| 90 | passThrough: true,
|
|---|
| 91 | },
|
|---|
| 92 | { full: 'version',
|
|---|
| 93 | abbr: 'V',
|
|---|
| 94 | passThrough: true,
|
|---|
| 95 | },
|
|---|
| 96 | // Alias lowercase v
|
|---|
| 97 | { full: 'version',
|
|---|
| 98 | abbr: 'v',
|
|---|
| 99 | passThrough: true,
|
|---|
| 100 | },
|
|---|
| 101 | ];
|
|---|
| 102 |
|
|---|
| 103 | let preempts = {
|
|---|
| 104 | version: function () {
|
|---|
| 105 | program.die(ejs.VERSION);
|
|---|
| 106 | },
|
|---|
| 107 | help: function () {
|
|---|
| 108 | program.die(usage);
|
|---|
| 109 | }
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| 112 | let stdin = '';
|
|---|
| 113 | process.stdin.setEncoding('utf8');
|
|---|
| 114 | process.stdin.on('readable', () => {
|
|---|
| 115 | let chunk;
|
|---|
| 116 | while ((chunk = process.stdin.read()) !== null) {
|
|---|
| 117 | stdin += chunk;
|
|---|
| 118 | }
|
|---|
| 119 | });
|
|---|
| 120 |
|
|---|
| 121 | function run() {
|
|---|
| 122 |
|
|---|
| 123 | program.availableOpts = CLI_OPTS;
|
|---|
| 124 | program.parseArgs(args);
|
|---|
| 125 |
|
|---|
| 126 | let templatePath = program.taskNames[0];
|
|---|
| 127 | let pVals = program.envVars;
|
|---|
| 128 | let pOpts = {};
|
|---|
| 129 |
|
|---|
| 130 | for (let p in program.opts) {
|
|---|
| 131 | let name = hyphenToCamel(p);
|
|---|
| 132 | pOpts[name] = program.opts[p];
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | let opts = {};
|
|---|
| 136 | let vals = {};
|
|---|
| 137 |
|
|---|
| 138 | // Same-named 'passthrough' opts
|
|---|
| 139 | CLI_OPTS.forEach((opt) => {
|
|---|
| 140 | let optName = hyphenToCamel(opt.full);
|
|---|
| 141 | if (opt.passThrough && typeof pOpts[optName] != 'undefined') {
|
|---|
| 142 | opts[optName] = pOpts[optName];
|
|---|
| 143 | }
|
|---|
| 144 | });
|
|---|
| 145 |
|
|---|
| 146 | // Bail out for help/version
|
|---|
| 147 | for (let p in opts) {
|
|---|
| 148 | if (preempts[p]) {
|
|---|
| 149 | return preempts[p]();
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | // Ensure there's a template to render
|
|---|
| 154 | if (!templatePath) {
|
|---|
| 155 | throw new Error('Please provide a template path. (Run ejs -h for help)');
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | if (opts.strict) {
|
|---|
| 159 | pOpts.noWith = true;
|
|---|
| 160 | }
|
|---|
| 161 | if (pOpts.noWith) {
|
|---|
| 162 | opts._with = false;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | // Grab and parse any input data, in order of precedence:
|
|---|
| 166 | // 1. Stdin
|
|---|
| 167 | // 2. CLI arg via -i
|
|---|
| 168 | // 3. Data file via -f
|
|---|
| 169 | // Any individual vals passed at the end (e.g., foo=bar) will override
|
|---|
| 170 | // any vals previously set
|
|---|
| 171 | let input;
|
|---|
| 172 | let err = new Error('Please do not pass data multiple ways. Pick one of stdin, -f, or -i.');
|
|---|
| 173 | if (stdin) {
|
|---|
| 174 | input = stdin;
|
|---|
| 175 | }
|
|---|
| 176 | else if (pOpts.dataInput) {
|
|---|
| 177 | if (input) {
|
|---|
| 178 | throw err;
|
|---|
| 179 | }
|
|---|
| 180 | input = decodeURIComponent(pOpts.dataInput);
|
|---|
| 181 | }
|
|---|
| 182 | else if (pOpts.dataFile) {
|
|---|
| 183 | if (input) {
|
|---|
| 184 | throw err;
|
|---|
| 185 | }
|
|---|
| 186 | input = fs.readFileSync(pOpts.dataFile).toString();
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | if (input) {
|
|---|
| 190 | vals = JSON.parse(input);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | // Override / set any individual values passed from the command line
|
|---|
| 194 | for (let p in pVals) {
|
|---|
| 195 | vals[p] = pVals[p];
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | opts.filename = path.resolve(process.cwd(), templatePath);
|
|---|
| 199 | let template = fs.readFileSync(opts.filename).toString();
|
|---|
| 200 | let output = ejs.render(template, vals, opts);
|
|---|
| 201 | if (pOpts.outputFile) {
|
|---|
| 202 | fs.writeFileSync(pOpts.outputFile, output);
|
|---|
| 203 | }
|
|---|
| 204 | else {
|
|---|
| 205 | process.stdout.write(output);
|
|---|
| 206 | }
|
|---|
| 207 | process.exit();
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | // Defer execution so that stdin can be read if necessary
|
|---|
| 211 | setImmediate(run);
|
|---|