diff --git a/README.md b/README.md index 7e4a220..6112025 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ Options: -f, --file give ejs template file path. [string] -b, --base-dir base directory that -f is relative to. [string] [default: "./"] -o, --out file to write compiled. [string] - -O, --options option variables (file path or JSON string). [string] + -O, --options option variables (file path or JSON string). [string] (actually the 'data' parameter of EJS library) + -C, --config config variables (file path or JSON string). [string] (actually the 'options' parameter of EJS library) ``` ### examples @@ -40,6 +41,16 @@ ejs-cli --base-dir src/ "*.ejs" --out dest/ # renders all of the files in src/ and outputs them to dest/ ``` +```bash +ejs-cli "myfile.ejs" --config "{\"delimiter\":\"$\"}" --options options.json +# renders the "myfile.ejs" file with inline config (set the custom delimiter "$") and an options.json data file +``` + +```bash +ejs-cli "myfile.ejs" --config config.json --options "{\"test\":\"123\"}" +# renders the "myfile.ejs" file with config.json config file and inline data +``` + ```bash cat example.ejs | ejs-cli example.ejs > example.html ``` diff --git a/index.js b/index.js index 0f38b6f..e5f3d78 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./lib/ejs-cli'); \ No newline at end of file +module.exports = require('./lib/cli'); \ No newline at end of file diff --git a/lib/cli.js b/lib/cli.js index 7a21b61..2c7f911 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -13,29 +13,33 @@ var parseOptionsFile = require(__dirname + '/parseOptionsFile'); (function () { var argv = optimist - .boolean('h') - .alias('h', 'help') - .default('h', false) - .describe('h', 'show this help.') - - .string('f') - .alias('f', 'file') - .describe('f', 'give ejs template file path.') + .boolean('h') + .alias('h', 'help') + .default('h', false) + .describe('h', 'show this help.') + + .string('f') + .alias('f', 'file') + .describe('f', 'give ejs template file path.') - .string('b') - .alias('b', 'base-dir') - .default('b', './') - .describe('b', 'base directory that -f is relative to.') + .string('b') + .alias('b', 'base-dir') + .default('b', './') + .describe('b', 'base directory that -f is relative to.') - .string('o') - .alias('o', 'out') - .describe('o', 'file to write compiled.') + .string('o') + .alias('o', 'out') + .describe('o', 'file to write compiled.') - .string('O') - .alias('O', 'options') - .describe('O', 'option variables (file path or JSON string).') + .string('O') + .alias('O', 'options') + .describe('O', 'option variables (file path or JSON string).') - .argv; + .string('C') + .alias('C', 'config') + .describe('C', 'config variables (file path or JSON string).') + + .argv; if (argv.help) { optimist.showHelp(); @@ -82,61 +86,91 @@ var parseOptionsFile = require(__dirname + '/parseOptionsFile'); var srcPaths = []; var options = {}; + var config = {}; + + async.series([ + function (next) { + // read config + if (!argv.config) { + log('config', 'none'); + return next(); + } + + // read config + if (argv.config && fs.existsSync(argv.config)) { + log('config', '"' + argv.config + '"'); + parseOptionsFile(argv.config, function (err, opts) { + config = opts; + next(err); + }); + return; + } + + log('config', argv.config); - async.series([function (next) { - // read options - if (!argv.options) { - log('opts', 'none'); + try { + config = JSON.parse(argv.config); + } catch (e) { + return next(new Error('fail to parse config JSON.\n => ' + argv.config)); + } return next(); - } - if (fs.existsSync(argv.options)) { - log('opts', '"' + argv.options + '"'); - parseOptionsFile(argv.options, function (err, opts) { - options = opts; - next(err); - }); - return; - } + }, function (next) { + // read options + if (!argv.options) { + log('options', 'none'); + return next(); + } - log('options', argv.options); - try { - options = JSON.parse(argv.options); - } catch (e) { - return next(new Error('fail to parse options JSON.\n => ' + argv.options)); - } - return next(); + if (fs.existsSync(argv.options)) { + log('opts', '"' + argv.options + '"'); + parseOptionsFile(argv.options, function (err, opts) { + options = opts; + next(err); + }); + return; + } - }, function (next) { - glob(path.join(baseDir, srcGlob), function (err, array) { - if (err) { - return next(err); + log('options', argv.options); + + try { + options = JSON.parse(argv.options); + } catch (e) { + return next(new Error('fail to parse options JSON.\n => ' + argv.options)); } - srcPaths = array; - next(); - }); - }, function (next) { - if (!srcGlob) { - fetchStdio(function (err, src) { - var result = ejs.render(src, options); - writeCompiled(result, null, next); - }); - return; - } + return next(); - async.each(srcPaths, function (srcPath, done) { - ejs.renderFile(srcPath, options, function(err, result) { + }, function (next) { + glob(path.join(baseDir, srcGlob), function (err, array) { if (err) { - console.log(err); - } else { - writeCompiled(result, srcPath, done); + return next(err); } + srcPaths = array; + next(); }); - }, next); - }], function (err) { - if (err) { - console.error(err); - } - process.exit(); - }); + }, function (next) { + if (!srcGlob) { + fetchStdio(function (err, src) { + var result = ejs.render(src, options, config); + writeCompiled(result, null, next); + }); + return; + } + + async.each(srcPaths, function (srcPath, done) { + ejs.renderFile(srcPath, options, config, function(err, result) { + if (err) { + console.log(err); + } else { + writeCompiled(result, srcPath, done); + } + }); + }, next); + }], + function (err) { + if (err) { + console.error(err); + } + process.exit(); + }); })();