Skip to content

Commit

Permalink
Merge pull request #38 from marcel-ploch/master
Browse files Browse the repository at this point in the history
Support specifying which script should be run by npm-watch
  • Loading branch information
M-Zuber authored May 1, 2017
2 parents fb34eea + c18fe99 commit 74f083c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 32 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ Temporary Items
node_modules

#vs vnext files
.vs/
.vs/
#vs code files
.vscode/
.history/
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/usr/bin/env node
'use strict';
var path = require('path')

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')

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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npm-watch",
"version": "0.1.8",
"version": "0.1.9",
"description": "run scripts from package.json when files change",
"main": "index.js",
"dependencies": {
Expand Down
76 changes: 49 additions & 27 deletions watch-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@ 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 = {}

taskName = typeof taskName !== 'undefined' ? taskName.trim() : '';

if (taskName === '') {
console.info('No task specified. Will go through 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) {
Expand All @@ -36,11 +46,47 @@ module.exports = function watchPackage(pkgDir, exit) {
stdin.stderr = through()
stdin.stdout = through()

if (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
Expand Down Expand Up @@ -88,28 +134,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()
})
}

0 comments on commit 74f083c

Please sign in to comment.