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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ input is taken from standard input and output to standard output.
-w, --watch watch files for changes and automatically re-render
-E, --extension <ext> specify the output file extension
-s, --silent do not output logs
-m, --main <str> Main File, Only render single main file when have dependencies
-I, --ignoreinitial Skip first rendered on watch
--name-after-file name the template after the last section of the file
path (requires --client and overriden by --name)
--doctype <str> specify the doctype on the command line (useful if it
Expand Down
64 changes: 51 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ program
.option('-w, --watch', 'watch files for changes and automatically re-render')
.option('-E, --extension <ext>', 'specify the output file extension')
.option('-s, --silent', 'do not output logs')
.option('-m, --main <str>', 'Main File, Only render when have dependencies')
.option('-I, --ignoreinitial', 'Ignore render on first watch')
.option('--ignoredependencies', 'Ignore dependencies on watch')
.option('--only <str>', 'Only render one file')
.option('--name-after-file', 'name the template after the last section of the file path (requires --client and overriden by --name)')
.option('--doctype <str>', 'specify the doctype on the command line (useful if it is not specified by the template)')

Expand Down Expand Up @@ -76,6 +80,7 @@ program.on('--help', function(){
program.parse(process.argv);

// options given, parse them
var count = 0;

if (program.obj) {
options = parseObj(program.obj);
Expand Down Expand Up @@ -104,12 +109,16 @@ function parseObj (input) {
}

[
['path', 'filename'], // --path
['debug', 'compileDebug'], // --no-debug
['client', 'client'], // --client
['pretty', 'pretty'], // --pretty
['basedir', 'basedir'], // --basedir
['doctype', 'doctype'], // --doctype
['path', 'filename'], // --path
['debug', 'compileDebug'], // --no-debug
['client', 'client'], // --client
['pretty', 'pretty'], // --pretty
['basedir', 'basedir'], // --basedir
['doctype', 'doctype'], // --doctype
['main', 'main'], // --main
['ignoreinitial', 'ignoreinitial'], // --ignoreinitial
['ignoredependencies', 'ignoredependencies'], // --ignoreinitial
['only', 'only'], // --only
].forEach(function (o) {
options[o[1]] = program[o[0]] !== undefined ? program[o[0]] : options[o[1]];
});
Expand Down Expand Up @@ -145,9 +154,19 @@ if (files.length) {
process.exit(1);
});
}
files.forEach(function (file) {
render(file);
});

if(!!program.watch && options.only){
render(options.only);
}else{
files.forEach(function (file) {
if(!!program.watch && options.only){
return render(options.only)
}else{
render(file);
}
});
}

// stdio
} else {
stdin();
Expand Down Expand Up @@ -185,9 +204,17 @@ function watchFile(path, base, rootPath) {
if (curr.mtime.getTime() === 0) return;
// istanbul ignore if
if (curr.mtime.getTime() === prev.mtime.getTime()) return;
watchList[path].forEach(function(file) {
tryRender(file, rootPath);
});

if(options.main && watchList[path].length > 1){
var mainFile = watchList[path].find( function(file){
return file === options.main;
}) || watchList[path][0];
tryRender(mainFile, rootPath);
}else{
watchList[path].forEach(function(file) {
tryRender(file, rootPath);
});
}
});
}

Expand Down Expand Up @@ -242,6 +269,7 @@ function stdin() {
*/

function renderFile(path, rootPath) {
program.ignoreinitial && count++;
var isPug = /\.(?:pug|jade)$/;
var isIgnored = /([\/\\]_)|(^_)/;

Expand All @@ -256,13 +284,23 @@ function renderFile(path, rootPath) {
var fn = options.client
? pug.compileFileClient(path, options)
: pug.compileFile(path, options);
if (program.watch && fn.dependencies) {

// Add ignoredependencies for fast watching
if (program.watch && fn.dependencies && !program.ignoredependencies) {
// watch dependencies, and recompile the base
fn.dependencies.forEach(function (dep) {
watchFile(dep, path, rootPath);
});
}

// Only render main file when ignoreinitial
if( program.ignoreinitial && program.watch && (count <= program.args.length)){
watchFile(program.main || program.args[0], null, rootPath);
if(count > 1){
return;
}
}

// --extension
var extname;
if (program.extension) extname = '.' + program.extension;
Expand Down