Skip to content

Commit

Permalink
Enable flag for initial chokidar events (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 3, 2018
1 parent ba5a1a6 commit 415c604
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
14 changes: 11 additions & 3 deletions README.md
Expand Up @@ -35,9 +35,17 @@ Enable if you want verbose logging from `onchange` (useful for debugging). For e
onchange -v 'app/**/*.js' 'test/**/*.js' -- npm test
```

### Add (`-a`, `--add`)

To execute the command for all initially added paths:

```sh
onchange -a 'config.json' -- microservice-proxy -c {{changed}} -p 9000
```

### Initial (`-i`, `--initial`)

To execute the command on the first run (no change), include the `-i` flag: For example:
To execute the command once on load without any event:

```sh
onchange -i '**/*.js' -- npm start
Expand Down Expand Up @@ -66,12 +74,12 @@ onchange -k '**/*.js' -- npm test
Set the maximum concurrent processes to run (default is `1`):

```sh
onchange -j2 '**/*.js' -- npm test
onchange -j2 '**/*.js' -- cp -v -r '{{changed}}' 'test/{{changed}}'
```

### Delay (`-d`, `--delay`)

To set the amount of delay (in ms) between process exits:
To set the amount of delay (in ms) between process changes:

```sh
onchange -d 1000 '**/*.js' -- npm start
Expand Down
24 changes: 13 additions & 11 deletions cli.js
Expand Up @@ -6,19 +6,20 @@ var arrify = require('arrify')
// Parse argv with minimist...it's easier this way.
var argv = require('minimist')(process.argv.slice(2), {
'--': true,
boolean: ['v', 'i', 'k'],
boolean: ['v', 'i', 'k', 'a'],
string: ['e', 'c', 'killSignal'],
alias: {
jobs: ['j'],
kill: ['k'],
verbose: ['v'],
initial: ['i'],
exclude: ['e'],
cwd: ['c'],
delay: ['d'],
poll: ['p'],
outpipe: ['o'],
filter: ['f']
add: 'a',
jobs: 'j',
kill: 'k',
verbose: 'v',
initial: 'i',
exclude: 'e',
cwd: 'c',
delay: 'd',
poll: 'p',
outpipe: 'o',
filter: 'f'
},
default: {
exclude: '**/node_modules/**'
Expand All @@ -39,6 +40,7 @@ var command = args.shift()
var options = {
exclude: typeof argv.exclude === 'boolean' ? [] : arrify(argv.exclude),
verbose: argv.verbose,
add: argv.add,
initial: argv.initial,
jobs: argv.jobs,
kill: argv.kill,
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -6,6 +6,7 @@ declare namespace onchange {
export interface Options {
exclude?: string[];
cwd?: string;
add?: boolean;
initial?: boolean;
verbose?: boolean;
jobs?: number;
Expand Down
22 changes: 12 additions & 10 deletions index.js
Expand Up @@ -110,6 +110,7 @@ function onchange (match, command, rawargs, opts = {}) {
const watcher = chokidar.watch(matches, {
cwd: cwd,
ignored: opts.exclude || [],
ignoreInitial: opts.add !== true,
usePolling: opts.poll === true || typeof opts.poll === 'number',
interval: typeof opts.poll === 'number' ? opts.poll : undefined,
awaitWriteFinish: awaitWriteFinish
Expand Down Expand Up @@ -159,21 +160,22 @@ function onchange (match, command, rawargs, opts = {}) {
return dequeue()
}

watcher.on('ready', () => {
log(`watching ${matches.join(', ')}`)
// Execute initial event without any changes.
if (initial) enqueue('', '')

// Execute initial event without any changes.
if (initial) enqueue('', '')
// For any change, creation or deletion, try to run.
watcher.on('all', (event, changed) => {
if (filter.length && filter.indexOf(event) === -1) return

// For any change, creation or deletion, try to run.
watcher.on('all', (event, changed) => {
if (filter.length && filter.indexOf(event) === -1) return
return enqueue(event, changed)
})

return enqueue(event, changed)
})
// On ready, prepare triggers.
watcher.on('ready', () => {
log(`watching ${matches.join(', ')}`)

// Notify external listener of "ready" event.
ready()
return ready()
})

watcher.on('error', (error) => log(`watcher error: ${error}`))
Expand Down

0 comments on commit 415c604

Please sign in to comment.