diff --git a/index.js b/index.js index e638668..b483b37 100755 --- a/index.js +++ b/index.js @@ -25,6 +25,7 @@ var defaultOpts = { verbose: false, silent: false, initial: false, + runonce: false, command: null }; @@ -83,6 +84,11 @@ var argv = require('yargs') default: defaultOpts.initial, type: 'boolean' }) + .option('runonce', { + describe: 'When set, command is initially run once and the process is terminated afterwards', + default: defaultOpts.runonce, + type: 'boolean' + }) .option('p', { alias: 'polling', describe: 'Whether to use fs.watchFile(backed by polling) instead of ' + @@ -140,7 +146,7 @@ function startWatching(opts) { var watcher = chokidar.watch(opts.patterns, chokidarOpts); var throttledRun = _.throttle(run, opts.throttle); - var debouncedRun = _.debounce(throttledRun, opts.debounce); + var debouncedRun = run; // process all requests during initial stage (runonce or initial) watcher.on('all', function(event, path) { var description = EVENT_DESCRIPTIONS[event] + ':'; @@ -172,6 +178,8 @@ function startWatching(opts) { if (!opts.silent) { console.error('Watching', '"' + list + '" ..'); } + // debounce after initial stage + debouncedRun = _.debounce(throttledRun, opts.debounce); }); } @@ -180,11 +188,12 @@ function createChokidarOpts(opts) { opts.ignore = _resolveIgnoreOpt(opts.ignore); var chokidarOpts = { + persistent: !opts.runonce, followSymlinks: opts.followSymlinks, usePolling: opts.polling, interval: opts.pollInterval, binaryInterval: opts.pollIntervalBinary, - ignoreInitial: !opts.initial + ignoreInitial: !opts.runonce && !opts.initial }; if (opts.ignore) chokidarOpts.ignored = opts.ignore;