From 73b3c9d1ecaec8a3c116c4a2b85f5f34c3380712 Mon Sep 17 00:00:00 2001 From: Charles Dudley Date: Tue, 17 Apr 2018 17:36:04 -0500 Subject: [PATCH] added config flag to clear the buffer on file changes --- README.md | 40 ++++++++++++++++++++++++++++++++++++---- watch-package.js | 16 ++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f3b35f8..d6dc24f 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,7 @@ Possibilty to watch for different tasks ```javascript { - "watch": - { + "watch": { "run_android": { "patterns": [ "app" @@ -53,8 +52,7 @@ Possibilty to watch for different tasks } ``` - -The keys of the `"watch"` config should match the names of your `"scripts"`, and +The top level 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. @@ -120,6 +118,40 @@ EOF Tests run *perfectly*, ship it to the enterprise! +### Options + +#### `patterns` + +Array of paths to watch + +#### `extensions` + +Array of file extensions to watch + +#### `ignore` + +String or array of paths to ignore + +#### `quiet` + +Boolean to hide the script name in any output on stdout and stderr + +#### `inherit` + +Boolean to preserve the original process' stdout and stderr + +#### `legacyWatch` + +Boolean to enable [legacy watch](https://github.com/remy/nodemon#application-isnt-restarting) + +#### `delay` + +Number of milliseconds to [delay](https://github.com/remy/nodemon#delaying-restarting) before checking for new files + +#### `clearBuffer` + +Boolean to clear the buffer after detecting a new change + ## Acknowledgements This module does very little but run [`nodemon`](http://npm.im/nodemon) for you, all diff --git a/watch-package.js b/watch-package.js index 978b745..2d73eab 100644 --- a/watch-package.js +++ b/watch-package.js @@ -7,6 +7,7 @@ var through = require('through2') var npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'; var nodemon = process.platform === 'win32' ? 'nodemon.cmd' : 'nodemon'; +var clearCharacter = process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'; var pkgDir = ''; var stdin = null; @@ -94,6 +95,7 @@ function startScript(script, pkg, processes) { var inherit = null var legacyWatch = null var delay = null + var clearBuffer = null if (typeof pkg.watch[script] === 'object' && !Array.isArray(pkg.watch[script])) { patterns = pkg.watch[script].patterns @@ -103,6 +105,7 @@ function startScript(script, pkg, processes) { inherit = pkg.watch[script].inherit legacyWatch = pkg.watch[script].legacyWatch delay = pkg.watch[script].delay + clearBuffer = pkg.watch[script].clearBuffer } else { patterns = pkg.watch[script] } @@ -133,6 +136,19 @@ function startScript(script, pkg, processes) { stdio: inherit === true ? ['pipe', 'inherit', 'pipe'] : 'pipe' }) if (inherit === true) return; + + if (clearBuffer === true) { + proc.stdout.pipe( + through(function(line, _, callback) { + line = line.toString(); + if (line.match('restarting due to changes...')) { + stdin.stdout.write(clearCharacter); + } + callback() + }) + ) + } + if (quiet === true || quiet === 'true') { proc.stdout.pipe(stdin.stdout) proc.stderr.pipe(stdin.stderr)