Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/ejs-cli');
module.exports = require('./lib/cli');
166 changes: 100 additions & 66 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
})();