From ce11c9971d56d40cbea8417edb49089e6f1c0073 Mon Sep 17 00:00:00 2001 From: anthony-tron Date: Mon, 9 Aug 2021 18:24:31 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20Support=20hot=20reload=20for=20?= =?UTF-8?q?object=20files=20(JS,=20JSON)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) mode change 100644 => 100755 index.js diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 84cbe42..e67c905 --- a/index.js +++ b/index.js @@ -87,7 +87,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 { @@ -140,7 +142,26 @@ var render = program.watch ? tryRender : renderFile; if (files.length) { consoleLog(); + if (program.watch) { + if (program.obj && fs.existsSync(program.obj)) { + fs.watch(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); }); From 6ccbc22e70ef7688de01292792d1d2fa66dc3c71 Mon Sep 17 00:00:00 2001 From: anthony-tron Date: Mon, 9 Aug 2021 18:50:54 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=A8=20Support=20YAML?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 8 +++++++- package.json | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e67c905..d074320 100755 --- 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; @@ -79,6 +80,7 @@ program.parse(process.argv); if (program.obj) { options = parseObj(program.obj); + console.log(options); } /** @@ -100,7 +102,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 + ')'); + } } } } 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": "*", From 9c031154bf048690093132c109a58113cf29cbeb Mon Sep 17 00:00:00 2001 From: anthony-tron Date: Mon, 9 Aug 2021 18:54:16 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=E2=8F=AA=EF=B8=8F=20Switch=20back=20to=20f?= =?UTF-8?q?s.watchFile=20because=20it's=20less=20buggy=20even=20though=20n?= =?UTF-8?q?ode=20recommends=20to=20use=20fs.watch=20instead?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d074320..5de1f4e 100755 --- a/index.js +++ b/index.js @@ -151,7 +151,7 @@ if (files.length) { if (program.watch) { if (program.obj && fs.existsSync(program.obj)) { - fs.watch(program.obj, {persistent: true, interval: 200}, function() { + fs.watchFile(program.obj, {persistent: true, interval: 200}, function() { consoleLog(' ' + chalk.yellow(program.obj) + ' ' + chalk.gray('changed')); // update object without losing previous data From 8de6f84fbe5e91080dd8386bc533a9eb39d7892b Mon Sep 17 00:00:00 2001 From: anthony-tron Date: Mon, 9 Aug 2021 19:08:47 +0200 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9D=20Updated=20usage=20for=20YAML?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- index.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) 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 index 5de1f4e..f5195e5 100755 --- a/index.js +++ b/index.js @@ -29,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') From 6f71e1fed1a16aefdd0106d1c8095c6000019e7f Mon Sep 17 00:00:00 2001 From: anthony-tron Date: Mon, 9 Aug 2021 19:18:00 +0200 Subject: [PATCH 5/5] =?UTF-8?q?=E2=8F=AE=20=20Removed=20useless=20console.?= =?UTF-8?q?log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/index.js b/index.js index f5195e5..33e05ac 100755 --- a/index.js +++ b/index.js @@ -80,7 +80,6 @@ program.parse(process.argv); if (program.obj) { options = parseObj(program.obj); - console.log(options); } /**