From 3d5c44e1821b1f3d0ca578dcd0bce37bdc1afa56 Mon Sep 17 00:00:00 2001 From: Seth Kinast Date: Fri, 20 Mar 2015 14:01:00 -0700 Subject: [PATCH 1/2] Add --watch support to dustc Watches the existing files and reruns compilation if they change. --- bin/dustc | 96 +++++++++++++++++++++++++++++++++------------------- package.json | 3 +- 2 files changed, 64 insertions(+), 35 deletions(-) diff --git a/bin/dustc b/bin/dustc index 620bd9de..aa74ec5e 100755 --- a/bin/dustc +++ b/bin/dustc @@ -1,6 +1,7 @@ #!/usr/bin/env node var cli = require('cli').enable('glob', 'version'), + watchr = require('watchr'), dust = require('../lib/server.js'), fs = cli.native.fs, path = cli.native.path, @@ -20,7 +21,8 @@ cli.parse({ split: ['s', 'create one output file per input file instead of concatenating them to --output' ], pwd: [false, 'generate template names starting from this directory', 'string'], whitespace: ['w', 'preserve whitespace in templates', ], - amd: ['a', 'register templates as AMD modules' ] + amd: ['a', 'register templates as AMD modules' ], + watch: [false, 'watch files for changes and recompile' ] }); /** @@ -36,47 +38,74 @@ if (!cli.argc) { }); } +var paths = glob(cli.args), + streams; + /** * Handle a list of paths passed as args * Each path gets globbed in case the OS doesn't support it */ -cli.args -.map(function(arg) { return cli.glob.sync(arg); }) -.reduce(function(a, b) { return a.concat(b); }, []) -.forEach(function(inputFile, index, filesToProcess) { - read(inputFile, function(err, data) { - if (err) { - cli.info('Couldn\'t open ' + inputFile + ' for reading'); - return; - } - - var outputFile = cli.options.output, - templateName = path.join(path.dirname(inputFile), - path.basename(inputFile, path.extname(inputFile))), - compiledData; - - // Use the template's path as the output path if split-files is turned on - if (cli.options.split) { - outputFile = templateName + '.js'; - } - - // Allow override of template name as long as there's only one template - if (cli.options.name && filesToProcess.length === 1) { - templateName = cli.options.name; - } +function handle() { + streams = {}; + paths.forEach(function(inputFile, index, filesToProcess) { + read(inputFile, function(err, data) { + if (err) { + cli.info('Couldn\'t open ' + inputFile + ' for reading'); + return; + } + + var outputFile = cli.options.output, + templateName = path.join(path.dirname(inputFile), + path.basename(inputFile, path.extname(inputFile))), + compiledData; + + // Use the template's path as the output path if split-files is turned on + if (cli.options.split) { + outputFile = templateName + '.js'; + } + + // Allow override of template name as long as there's only one template + if (cli.options.name && filesToProcess.length === 1) { + templateName = cli.options.name; + } + + // Optionally strip leading directories from a template name + // For example, if --pwd=tmpl, `tmpl/foo/a` becomes `foo/a` + if (cli.options.pwd) { + templateName = path.relative(cli.options.pwd, templateName); + } + + compiledData = compile(data, templateName); + output(compiledData, outputFile); + }); + }); +} - // Optionally strip leading directories from a template name - // For example, if --pwd=tmpl, `tmpl/foo/a` becomes `foo/a` - if (cli.options.pwd) { - templateName = path.relative(cli.options.pwd, templateName); +/** + * Turn on watching if --watch is enabled + * Watching runs the compilation again if any file changes or is deleted. + * However, adding new files means, for now, that you need to restart the watch. + */ +if(cli.options.watch) { + cli.info('Watching for changes... ^C to quit'); + watchr.watch({ + paths: paths, + listeners: { + change: function(method, file) { + cli.info(method + ' ' + file); + handle(); + } } - - compiledData = compile(data, templateName); - output(compiledData, outputFile); }); -}); +} else { + handle(); +} /*** helper functions ***/ +function glob(globPaths) { + return globPaths.map(function(arg) { return cli.glob.sync(arg); }) + .reduce(function(a, b) { return a.concat(b); }, []); +} function read(filename, cb) { var data = '', @@ -105,7 +134,6 @@ function compile(data, name) { return compiled; } -var streams = {}; function output(data, file) { var output_stream; try { diff --git a/package.json b/package.json index 7d5b187d..45c95655 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "linkedin" ], "dependencies": { - "cli": "~0.6.5" + "cli": "~0.6.5", + "watchr": "~2.4.13" }, "devDependencies": { "grunt": "~0.4.2", From f77eb2ea2789c7a4796e34b4887b05784198417d Mon Sep 17 00:00:00 2001 From: Seth Kinast Date: Mon, 23 Mar 2015 17:41:05 -0700 Subject: [PATCH 2/2] Use chokidar instead of watchr --- bin/dustc | 19 ++++++++++--------- package.json | 4 ++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/bin/dustc b/bin/dustc index aa74ec5e..88b2cc37 100755 --- a/bin/dustc +++ b/bin/dustc @@ -1,7 +1,7 @@ #!/usr/bin/env node var cli = require('cli').enable('glob', 'version'), - watchr = require('watchr'), + watcher = require('chokidar'), dust = require('../lib/server.js'), fs = cli.native.fs, path = cli.native.path, @@ -88,14 +88,15 @@ function handle() { */ if(cli.options.watch) { cli.info('Watching for changes... ^C to quit'); - watchr.watch({ - paths: paths, - listeners: { - change: function(method, file) { - cli.info(method + ' ' + file); - handle(); - } - } + watcher = watcher + .watch(paths, { ignoreInitial: true }) + .on('unlink', function(file) { + paths.splice(paths.indexOf(file), 1); + watcher.unwatch(file); + }) + .on('all', function(method, file) { + cli.info(method + ' ' + file); + handle(); }); } else { handle(); diff --git a/package.json b/package.json index 45c95655..8391565c 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,8 @@ "linkedin" ], "dependencies": { - "cli": "~0.6.5", - "watchr": "~2.4.13" + "chokidar": "~1.0.0-rc4", + "cli": "~0.6.5" }, "devDependencies": { "grunt": "~0.4.2",