Skip to content

Commit 3d1a242

Browse files
authored
Merge pull request #94 from sarangjo/manual-restart
Add manual restart option
2 parents 3529f8e + 270375d commit 3d1a242

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

lib/index.js

100644100755
Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var fs = require('fs')
77
var tsNodeVersion = require('ts-node').VERSION
88
var tsVersion = require('typescript').version
99
var kill = require('tree-kill')
10+
var readline = require('readline')
1011

1112
module.exports = function(script, scriptArgs, nodeArgs, opts) {
1213
if (typeof script !== 'string' || script.length === 0) {
@@ -44,34 +45,7 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
4445
recursive: true
4546
})
4647
var starting = false
47-
watcher.on('change', function(file) {
48-
if (file === compiler.tsConfigPath) {
49-
notify('Reinitializing TS compilation')
50-
compiler.init(opts)
51-
}
52-
/* eslint-disable no-octal-escape */
53-
54-
if (cfg.clear || cfg.cls) {
55-
process.stdout.write('\033[2J\033[H')
56-
}
57-
notify('Restarting', file + ' has been modified')
58-
compiler.compileChanged(file)
59-
if (starting) {
60-
log.debug('Already starting')
61-
return
62-
}
63-
log.debug('Removing all watchers from files')
64-
watcher.removeAll()
65-
starting = true
66-
if (child) {
67-
log.debug('Child is still running, restart upon exit')
68-
child.on('exit', start)
69-
stop()
70-
} else {
71-
log.debug('Child is already stopped, probably due to a previous error')
72-
start()
73-
}
74-
})
48+
watcher.on('change', restart)
7549

7650
watcher.on('fallback', function(limit) {
7751
log.warn('node-dev ran out of file handles after watching %s files.', limit)
@@ -80,6 +54,19 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
8054
if (cfg.deps) log.info('... or add `--no-deps` to use less file handles.')
8155
})
8256

57+
// Read for "rs" from command line
58+
const rl = readline.createInterface({
59+
input: process.stdin,
60+
output: process.stdout,
61+
terminal: false
62+
})
63+
rl.on('line', function(line) {
64+
if (line.trim() === 'rs') {
65+
log.debug('User entered `rs`')
66+
restart('', true)
67+
}
68+
})
69+
8370
/**
8471
* Run the wrapped script.
8572
*/
@@ -192,6 +179,36 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
192179
}
193180
}
194181

182+
function restart(file, isManualRestart) {
183+
if (file === compiler.tsConfigPath) {
184+
notify('Reinitializing TS compilation')
185+
compiler.init(opts)
186+
}
187+
/* eslint-disable no-octal-escape */
188+
if (cfg.clear) process.stdout.write('\033[2J\033[H')
189+
if (isManualRestart) {
190+
notify('Restarting', 'manual restart from user')
191+
} else {
192+
notify('Restarting', file + ' has been modified')
193+
}
194+
compiler.compileChanged(file)
195+
if (starting) {
196+
log.debug('Already starting')
197+
return
198+
}
199+
log.debug('Removing all watchers from files')
200+
watcher.removeAll()
201+
starting = true
202+
if (child) {
203+
log.debug('Child is still running, restart upon exit')
204+
child.on('exit', start)
205+
stop()
206+
} else {
207+
log.debug('Child is already stopped, probably due to a previous error')
208+
start()
209+
}
210+
}
211+
195212
// Relay SIGTERM
196213
process.on('SIGTERM', function() {
197214
log.debug('Process got SIGTERM')

0 commit comments

Comments
 (0)