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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ input is taken from standard input and output to standard output.
```
-h, --help output usage information
-V, --version output the version number
-O, --obj <str|path> JSON/JavaScript options object or file
-O, --obj <str|path> JSON/JavaScript/YAML options object or file
-o, --out <dir> output the rendered HTML or compiled JavaScript to
<dir>
-p, --path <path> filename used to resolve includes
Expand Down Expand Up @@ -82,6 +82,8 @@ $ pug -O options.js foo.pug
# or, JSON works too
$ echo '{"doctype": "html"}' > options.json
$ pug -O options.json foo.pug
# YAML works as well
$ pug -O options.yaml foo.pug
```

## Installation
Expand Down
32 changes: 29 additions & 3 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var program = require('commander');
var mkdirp = require('mkdirp');
var chalk = require('chalk');
var pug = require('pug');
var yaml = require('js-yaml')

var basename = path.basename;
var dirname = path.dirname;
Expand All @@ -28,7 +29,7 @@ program
'pug-cli version: ' + require( './package.json').version
)
.usage('[options] [dir|file ...]')
.option('-O, --obj <str|path>', 'JSON/JavaScript options object or file')
.option('-O, --obj <str|path>', 'JSON/JavaScript/YAML options object or file')
.option('-o, --out <dir>', 'output the rendered HTML or compiled JavaScript to <dir>')
.option('-p, --path <path>', 'filename used to resolve includes')
.option('-b, --basedir <path>', 'path used as root directory to resolve absolute includes')
Expand Down Expand Up @@ -87,7 +88,9 @@ if (program.obj) {
*/
function parseObj (input) {
try {
return require(path.resolve(input));
resolved = require.resolve(input);
delete require[resolved]; // delete cache
return require(resolved);
} catch (e) {
var str;
try {
Expand All @@ -98,7 +101,11 @@ function parseObj (input) {
try {
return JSON.parse(str);
} catch (e) {
return eval('(' + str + ')');
try {
return yaml.load(str, 'utf-8');
} catch(e) {
return eval('(' + str + ')');
}
}
}
}
Expand Down Expand Up @@ -140,7 +147,26 @@ var render = program.watch ? tryRender : renderFile;

if (files.length) {
consoleLog();

if (program.watch) {
if (program.obj && fs.existsSync(program.obj)) {
fs.watchFile(program.obj, {persistent: true, interval: 200}, function() {
consoleLog(' ' + chalk.yellow(program.obj) + ' ' + chalk.gray('changed'));

// update object without losing previous data
Object.assign(options, parseObj(options));

// then update all files
for (const [path, bases] of Object.entries(watchList)) {
if (watchList.hasOwnProperty(path)) {
bases.forEach(render);
}
}
});

consoleLog(' ' + chalk.gray('watching') + ' ' + chalk.yellow(program.obj));
}

process.on('SIGINT', function() {
process.exit(1);
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"dependencies": {
"chalk": "^1.0.0",
"commander": "^2.8.1",
"pug": "^2.0.0-alpha7",
"mkdirp": "^0.5.1"
"js-yaml": "^4.1.0",
"mkdirp": "^0.5.1",
"pug": "^2.0.0-alpha7"
},
"devDependencies": {
"istanbul": "*",
Expand Down