Skip to content

Commit

Permalink
Merge pull request #578 from sethkinast/watchr
Browse files Browse the repository at this point in the history
Add --watch support to dustc
  • Loading branch information
prashn64 committed Mar 24, 2015
2 parents c2360cb + f77eb2e commit 6f15f85
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 35 deletions.
99 changes: 64 additions & 35 deletions bin/dustc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

var cli = require('cli').enable('glob', 'version'),
watcher = require('chokidar'),
dust = require('../lib/server.js'),
fs = cli.native.fs,
path = cli.native.path,
Expand All @@ -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' ]
});

/**
Expand All @@ -36,47 +38,75 @@ 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;
}

// 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);
}
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);
});
});
}

compiledData = compile(data, templateName);
output(compiledData, outputFile);
/**
* 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');
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();
}

/*** 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 = '',
Expand Down Expand Up @@ -105,7 +135,6 @@ function compile(data, name) {
return compiled;
}

var streams = {};
function output(data, file) {
var output_stream;
try {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"linkedin"
],
"dependencies": {
"chokidar": "~1.0.0-rc4",
"cli": "~0.6.5"
},
"devDependencies": {
Expand Down

0 comments on commit 6f15f85

Please sign in to comment.