From 618f017e4659502cfa440154baee1716399effc9 Mon Sep 17 00:00:00 2001 From: levithomason Date: Wed, 20 Apr 2016 16:40:54 -0700 Subject: [PATCH 1/3] feat(include): add include pattern cli option --- cli.js | 31 +++++++++++++++++++++++++++++-- package.json | 1 + readme.mkd | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index 3a92ac6..a05ce01 100755 --- a/cli.js +++ b/cli.js @@ -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 [...directory] [--wait=] [--filter=] [--interval=] [--ignoreDotFiles] [--ignoreUnreadable]') + console.error([ + 'Usage: watch [...directory]', + '[--wait=]', + '[--filter=]', + '[--include=]', + '[--interval=]', + '[--ignoreDotFiles]', + '[--ignoreUnreadable]' + ].join(' ')) process.exit() } @@ -35,15 +45,32 @@ 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 + } + + if (minimatch(fileName, argv.include)) { + return (filterFn) ? filterFn(fileName) : true + } else { + return false + } + } +} else if (filterFn) { + watchTreeOpts.filter = filterFn +} + var wait = false var dirLen = dirs.length diff --git a/package.json b/package.json index 6257b0c..e0ef245 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "main": "./main", "dependencies": { "exec-sh": "^0.2.0", + "minimatch": "^3.0.0", "minimist": "^1.2.0" } } diff --git a/readme.mkd b/readme.mkd index ed14948..8643db5 100644 --- a/readme.mkd +++ b/readme.mkd @@ -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. From 5ab4c62a1affaa64d3badd17debefcc7b3230ab5 Mon Sep 17 00:00:00 2001 From: levithomason Date: Wed, 20 Apr 2016 16:42:08 -0700 Subject: [PATCH 2/3] fix(package.json): engines entry --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index e0ef245..a9f0a52 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,9 @@ "directories": { "lib": "lib" }, - "engines": [ - "node >=0.1.95" - ], + "engines": { + "node": ">=0.1.95" + }, "main": "./main", "dependencies": { "exec-sh": "^0.2.0", From 21986f5c2d6bd78621b416f0daf734eb8920692c Mon Sep 17 00:00:00 2001 From: levithomason Date: Tue, 26 Apr 2016 00:49:32 -0600 Subject: [PATCH 3/3] wip, scan dirs for pattern --- cli.js | 15 ++++++++++++++- foo/bar-test.js | 3 +++ foo/bar.js | 3 +++ foo/foo-sub/baz-test.js | 3 +++ foo/foo-sub/baz.js | 3 +++ 5 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 foo/bar-test.js create mode 100644 foo/bar.js create mode 100644 foo/foo-sub/baz-test.js create mode 100644 foo/foo-sub/baz.js diff --git a/cli.js b/cli.js index a05ce01..90c9a2d 100755 --- a/cli.js +++ b/cli.js @@ -61,7 +61,20 @@ if (argv.include) { return (filterFn) ? filterFn(fileName) : true } - if (minimatch(fileName, argv.include)) { + 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 diff --git a/foo/bar-test.js b/foo/bar-test.js new file mode 100644 index 0000000..a034ff1 --- /dev/null +++ b/foo/bar-test.js @@ -0,0 +1,3 @@ +/** + * Created by levithomason on 4/25/16. + */ diff --git a/foo/bar.js b/foo/bar.js new file mode 100644 index 0000000..a034ff1 --- /dev/null +++ b/foo/bar.js @@ -0,0 +1,3 @@ +/** + * Created by levithomason on 4/25/16. + */ diff --git a/foo/foo-sub/baz-test.js b/foo/foo-sub/baz-test.js new file mode 100644 index 0000000..a034ff1 --- /dev/null +++ b/foo/foo-sub/baz-test.js @@ -0,0 +1,3 @@ +/** + * Created by levithomason on 4/25/16. + */ diff --git a/foo/foo-sub/baz.js b/foo/foo-sub/baz.js new file mode 100644 index 0000000..a034ff1 --- /dev/null +++ b/foo/foo-sub/baz.js @@ -0,0 +1,3 @@ +/** + * Created by levithomason on 4/25/16. + */