diff --git a/README.md b/README.md index 677f9c3..51955f6 100644 --- a/README.md +++ b/README.md @@ -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 JSON/JavaScript options object or file +-O, --obj JSON/JavaScript/YAML options object or file -o, --out output the rendered HTML or compiled JavaScript to -p, --path filename used to resolve includes @@ -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 diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 84cbe42..33e05ac --- a/index.js +++ b/index.js @@ -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; @@ -28,7 +29,7 @@ program 'pug-cli version: ' + require( './package.json').version ) .usage('[options] [dir|file ...]') - .option('-O, --obj ', 'JSON/JavaScript options object or file') + .option('-O, --obj ', 'JSON/JavaScript/YAML options object or file') .option('-o, --out ', 'output the rendered HTML or compiled JavaScript to ') .option('-p, --path ', 'filename used to resolve includes') .option('-b, --basedir ', 'path used as root directory to resolve absolute includes') @@ -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 { @@ -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 + ')'); + } } } } @@ -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); }); diff --git a/package.json b/package.json index 846e96c..9c22aac 100644 --- a/package.json +++ b/package.json @@ -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": "*",