diff --git a/.gitignore b/.gitignore index efe2108..1b9e4bb 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,7 @@ Temporary Items node_modules #vs vnext files -.vs/ \ No newline at end of file +.vs/ +#vs code files +.vscode/ +.history/ \ No newline at end of file diff --git a/README.md b/README.md index b30af16..b181fba 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,42 @@ your `"scripts"`: } ``` +Possibilty to watch for different tasks + +```javascript + { + "watch": + { + "run_android": { + "patterns": [ + "app" + ], + "extensions": "ts,html,scss", + "quiet": false + }, + "run_ios": { + "patterns": [ + "app" + ], + "extensions": "ts,html,scss", + "quiet": false + } + }, + "scripts": { + "watch_android": "npm-watch run_android", + "watch_ios": "npm-watch run_ios", + "run_android": "tns run android --emulator", + "run_ios": "tns run ios --emulator" + } + } +``` + + The keys of the `"watch"` config should match the names of your `"scripts"`, and the values should be a glob pattern or array of glob patterns to watch. +Also it is now possible to obtain a second parameter to define the script which should be run for watching and not watch all possible scripts at once. + If you need to watch files with extensions other than those that `nodemon` watches [by default](https://github.com/remy/nodemon#specifying-extension-watch-list) (`.js`, `.coffee`, `.litcoffee`), you can set the value to an object with `patterns` and `extensions` keys. You can also add an `ignore` key (a list or a string) to ignore specific files. Finally, you can add a `quiet` flag to hide the script name in any output on stdout or stderr, or you can use the `inherit` flag to preserve the original's process stdout or stderr. > The `quiet` flag was changed from a `string` to a `boolean` in `0.1.5`. Backwards compatability will be kept for two patch versions. diff --git a/cli.js b/cli.js index f1b83a5..7902154 100755 --- a/cli.js +++ b/cli.js @@ -2,14 +2,17 @@ 'use strict'; var path = require('path') -var windows = process.platform === 'win32' + var windows = process.platform === 'win32' var pathVarName = (windows && !('PATH' in process.env)) ? 'Path' : 'PATH' -process.env[pathVarName] += path.delimiter + path.join(__dirname, 'node_modules', '.bin') +process.env[pathVarName] += + path.delimiter + + path.join(__dirname, 'node_modules', '.bin') -var watchPackage = require('./watch-package') + var watchPackage = require('./watch-package') -var watcher = watchPackage(process.argv[2] || process.cwd(), process.exit) + var watcher = watchPackage( + process.argv[3] || process.cwd(), process.exit, process.argv[2]) process.stdin.pipe(watcher) watcher.stdout.pipe(process.stdout) diff --git a/watch-package.js b/watch-package.js index 4915f65..36191b2 100644 --- a/watch-package.js +++ b/watch-package.js @@ -8,16 +8,24 @@ var through = require('through2') var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'; var nodemon = process.platform === 'win32' ? 'nodemon.cmd' : 'nodemon'; -module.exports = function watchPackage(pkgDir, exit) { +var pkgDir = ''; +var stdin = null; + +module.exports = function watchPackage(_pkgDir, exit, taskName) { + pkgDir = _pkgDir; var pkg = require(path.join(pkgDir, 'package.json')) var processes = {} + if (typeof taskName !== 'undefined' && taskName === '') { + console.info('No Task specified. Will go trough all possible tasks'); + } + if (typeof pkg.watch !== 'object') { die('No "watch" config in package.json') } // send 'rs' commands to the right proc - var stdin = through(function (line, _, callback) { + stdin = through(function (line, _, callback) { line = line.toString() var match = line.match(/^rs\s+(\w+)/) if (!match) { @@ -36,11 +44,47 @@ module.exports = function watchPackage(pkgDir, exit) { stdin.stderr = through() stdin.stdout = through() + if (typeof taskName !== 'undefined' && taskName !== '') { + if (!pkg.scripts[taskName]) { + die('No such script "' + taskName + '"', 2) + } + startScript(taskName, pkg, processes); + } else { + Object.keys(pkg.watch).forEach(function (script) { if (!pkg.scripts[script]) { die('No such script "' + script + '"', 2) } - var exec = [npm, 'run', '-s', script].join(' ') + startScript(script, pkg, processes); + }) + } + + return stdin + + function die(message, code) { + process.stderr.write(message) + + if (stdin) { + stdin.end() + stdin.stderr.end() + stdin.stdout.end() + } + exit(code || 1) + } +} + +function prefixer(prefix) { + return through(function (line, _, callback) { + line = line.toString() + if (!line.match('to restart at any time')) { + this.push(prefix + ' ' + line) + } + callback() + }) +} + +function startScript(script, pkg, processes) { + var exec = [npm, 'run', '-s', script].join(' ') var patterns = null var extensions = null var ignores = null @@ -88,28 +132,4 @@ module.exports = function watchPackage(pkgDir, exit) { proc.stdout.pipe(prefixer('[' + script + ']')).pipe(stdin.stdout) proc.stderr.pipe(prefixer('[' + script + ']')).pipe(stdin.stderr) } - }) - - return stdin - - function die(message, code) { - process.stderr.write(message) - - if (stdin) { - stdin.end() - stdin.stderr.end() - stdin.stdout.end() - } - exit(code || 1) - } -} - -function prefixer(prefix) { - return through(function (line, _, callback) { - line = line.toString() - if (!line.match('to restart at any time')) { - this.push(prefix + ' ' + line) - } - callback() - }) }