Skip to content

Commit

Permalink
Adding possibility to define one script to run in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
marcel-ploch-coodoo committed Apr 27, 2017
1 parent fb34eea commit 9c02af1
Show file tree
Hide file tree
Showing 4 changed files with 91 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
11 changes: 7 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 47 additions & 27 deletions watch-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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()
})
}

0 comments on commit 9c02af1

Please sign in to comment.