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
44 changes: 42 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ var argv = require('minimist')(process.argv.slice(2))
var execshell = require('exec-sh')
var path = require('path')
var watch = require('./main.js')
var minimatch = require('minimatch')
var fs = require('fs')

if(argv._.length === 0) {
console.error('Usage: watch <command> [...directory] [--wait=<seconds>] [--filter=<file>] [--interval=<seconds>] [--ignoreDotFiles] [--ignoreUnreadable]')
console.error([
'Usage: watch <command> [...directory]',
'[--wait=<seconds>]',
'[--filter=<file>]',
'[--include=<pattern>]',
'[--interval=<seconds>]',
'[--ignoreDotFiles]',
'[--ignoreUnreadable]'
].join(' '))
process.exit()
}

Expand Down Expand Up @@ -35,15 +45,45 @@ if(argv.ignoreDotFiles || argv.d)
if(argv.ignoreUnreadable || argv.u)
watchTreeOpts.ignoreUnreadableDir = true

var filterFn
if(argv.filter || argv.f) {
try {
watchTreeOpts.filter = require(path.resolve(process.cwd(), argv.filter || argv.f))
filterFn = require(path.resolve(process.cwd(), argv.filter || argv.f))
} catch (e) {
console.error(e)
process.exit(1)
}
}

if (argv.include) {
watchTreeOpts.filter = function (fileName) {
if (fs.statSync(fileName).isDirectory()) {
return (filterFn) ? filterFn(fileName) : true
}

const isMatch = !dirs.length ? minimatch(fileName, `**/${argv.include}`) : dirs.some(dir => {
const absDir = path.resolve(dir) + '/'
var relFileName = path.resolve(fileName).replace(absDir, '')

console.log({ dir, absDir, fileName, relFileName })

return minimatch(relFileName, argv.include)
})

console.log(`\nisMatch pattern fileName`)
console.log(`${isMatch} ${argv.include} ${fileName}`)
console.log('-'.repeat(80))

if (isMatch) {
return (filterFn) ? filterFn(fileName) : true
} else {
return false
}
}
} else if (filterFn) {
watchTreeOpts.filter = filterFn
}

var wait = false

var dirLen = dirs.length
Expand Down
3 changes: 3 additions & 0 deletions foo/bar-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Created by levithomason on 4/25/16.
*/
3 changes: 3 additions & 0 deletions foo/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Created by levithomason on 4/25/16.
*/
3 changes: 3 additions & 0 deletions foo/foo-sub/baz-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Created by levithomason on 4/25/16.
*/
3 changes: 3 additions & 0 deletions foo/foo-sub/baz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* Created by levithomason on 4/25/16.
*/
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"directories": {
"lib": "lib"
},
"engines": [
"node >=0.1.95"
],
"engines": {
"node": ">=0.1.95"
},
"main": "./main",
"dependencies": {
"exec-sh": "^0.2.0",
"minimatch": "^3.0.0",
"minimist": "^1.2.0"
}
}
1 change: 1 addition & 0 deletions readme.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The options object is passed to fs.watchFile but can also be used to provide two
* `'ignoreDotFiles'` - When true this option means that when the file tree is walked it will ignore files that being with "."
* `'filter'` - You can use this option to provide a function that returns true or false for each file and directory to decide whether or not that file/directory is included in the watcher.
* `'interval' - Specifies the interval duration in milliseconds, the time period between polling for file changes.
* `'include'` - You can use this option to provide a glob pattern that is applied for each file to decide whether or not that file is included in the watcher. If both a filter and include pattern are provided, the include pattern is applied to files before the filter function. Note that the include pattern is not applied to directories.
* `'ignoreUnreadableDir'` - When true, this options means that when a file can't be read, this file is silently skipped.
* `'ignoreNotPermitted'` - When true, this options means that when a file can't be read due to permission issues, this file is silently skipped.
* `'ignoreDirectoryPattern'` - When a regex pattern is set, e.g. /node_modules/, these directories are silently skipped.
Expand Down