Skip to content

Commit 6c4619b

Browse files
committed
add tree-kill and esm support
1 parent feb1fb0 commit 6c4619b

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

bin/ts-node-dev

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ var opts = minimist(devArgs, {
2828
'pretty',
2929
'prefer-ts',
3030
'exec-check',
31-
'debug'
31+
'debug',
32+
'tree-kill'
3233
],
3334
string: [
3435
'compiler',
@@ -65,6 +66,7 @@ var opts = minimist(devArgs, {
6566

6667
var script = opts._[0]
6768
var scriptArgs = opts._.slice(1)
69+
var loadEsm = false
6870

6971
unknown.forEach(function(arg) {
7072
if (arg === script || nodeArgs.indexOf(arg) >= 0) return
@@ -73,13 +75,22 @@ unknown.forEach(function(arg) {
7375
var argOpts = opts[argName]
7476
var argValues = Array.isArray(argOpts) ? argOpts : [argOpts]
7577
argValues.forEach(function(argValue) {
78+
console.log('argValue', argValue, 'arg', arg)
79+
if ((arg === '-r' || arg === '--require') && argValue === 'esm') {
80+
loadEsm = true
81+
return false
82+
}
7683
nodeArgs.push(arg)
7784
if (typeof argValue === 'string') {
7885
nodeArgs.push(argValue)
7986
}
8087
})
8188
})
8289

90+
if (loadEsm) {
91+
require('esm')
92+
}
93+
8394
if (!script) {
8495
console.log('Usage: ts-node-dev [options] script [arguments]\n')
8596
process.exit(1)

lib/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var compiler = require('./compiler')
66
var fs = require('fs')
77
var tsNodeVersion = require('ts-node').VERSION
88
var tsVersion = require('typescript').version
9+
var kill = require('tree-kill')
910

1011
module.exports = function(script, scriptArgs, nodeArgs, opts) {
1112
if (typeof script !== 'string' || script.length === 0) {
@@ -49,9 +50,13 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
4950
}
5051
/* eslint-disable no-octal-escape */
5152
if (cfg.clear) process.stdout.write('\033[2J\033[H')
52-
notify('Restarting', file + ' has been modified')
53-
compiler.compileChanged(file)
54-
if (starting) return
53+
notify('Restarting', file + ' has been modified')
54+
compiler.compileChanged(file)
55+
if (starting) {
56+
log.debug('Already starting')
57+
return
58+
}
59+
log.debug('Removing all watchers from files')
5560
watcher.removeAll()
5661
starting = true
5762
if (child) {
@@ -148,12 +153,22 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
148153

149154
// Upon errors, display a notification and tell the child to exit.
150155
ipc.on(child, 'error', function(m) {
156+
log.debug('Child error')
151157
notify(m.error, m.message, 'error')
152158
stop(m.willTerminate)
153159
})
154160
compiler.writeReadyFile()
155161
}
156-
162+
const killChild = () => {
163+
if (!child) return
164+
log.debug('Sending SIGTERM kill to child pid', child.pid)
165+
if (opts['tree-kill']) {
166+
log.debug('Using tree-kill')
167+
kill(child.pid)
168+
} else {
169+
child.kill('SIGTERM')
170+
}
171+
}
157172
function stop(willTerminate) {
158173
if (!child || child.stopping) {
159174
return
@@ -164,15 +179,15 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) {
164179
log.debug('Disconnecting from child')
165180
child.disconnect()
166181
if (!willTerminate) {
167-
log.debug('Sending SIGTERM kill to child')
168-
child.kill('SIGTERM')
182+
killChild()
169183
}
170184
}
171185
}
172186

173187
// Relay SIGTERM
174188
process.on('SIGTERM', function() {
175-
if (child) child.kill('SIGTERM')
189+
log.debug('Process got SIGTERM')
190+
killChild()
176191
process.exit(0)
177192
})
178193

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-node-dev",
3-
"version": "1.0.0-pre.35",
3+
"version": "1.0.0-pre.36",
44
"description": "Compiles your TS app and restarts when files are modified.",
55
"keywords": [
66
"restart",
@@ -39,7 +39,7 @@
3939
"scripts": {
4040
"ts-node-dev": "node ./bin/ts-node-dev",
4141
"test-node-dev": "tap test/*.js",
42-
"test": "node ./bin/ts-node-dev -r tsconfig-paths/register -r ./test/ts/add-require.js -r ./test/ts/add-require-2.js -O \"{\\\"module\\\": \\\"commonjs\\\"}\" --preserve-symlinks --respawn --ignore-watch 'lib' --ignore-watch bin --prefer-ts --debug --poll --interval 1000 --cache-directory .ts-node --inspect -- test/ts/test-script test-arg --fd",
42+
"test": "node ./bin/ts-node-dev --tree-kill -r tsconfig-paths/register -r ./test/ts/add-require.js -r ./test/ts/add-require-2.js -O \"{\\\"module\\\": \\\"commonjs\\\"}\" --preserve-symlinks --respawn --ignore-watch 'lib' --ignore-watch bin --prefer-ts --debug --poll --interval 1000 --cache-directory .ts-node --inspect -- test/ts/test-script test-arg --fd",
4343
"test-docker": "docker run --rm -v ${PWD}:/app mhart/alpine-node:8.7.0 sh -c 'cd app && node ./bin/ts-node-dev --cache-directory .ts-node test/ts/big'"
4444
},
4545
"dependencies": {
@@ -51,6 +51,7 @@
5151
"node-notifier": "^5.4.0",
5252
"resolve": "^1.0.0",
5353
"rimraf": "^2.6.1",
54+
"tree-kill": "^1.2.1",
5455
"ts-node": "*",
5556
"tsconfig": "^7.0.0"
5657
},

test/ts/test-script.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const obj: A = {
77
}
88
console.log('test', str)
99

10-
//fn()
1110

11+
setInterval(() => {
12+
console.log('Working')
13+
}, 5000)
1214

1315

1416
console.log('test', str)
1517

16-
throw new Error('fds')
18+
//throw new Error('fds')

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,6 +2257,11 @@ tough-cookie@>=0.12.0, tough-cookie@~2.3.0:
22572257
dependencies:
22582258
punycode "^1.4.1"
22592259

2260+
tree-kill@^1.2.1:
2261+
version "1.2.1"
2262+
resolved "https://registry.yarnpkg.com/tree-kill/-/tree-kill-1.2.1.tgz#5398f374e2f292b9dcc7b2e71e30a5c3bb6c743a"
2263+
integrity sha512-4hjqbObwlh2dLyW4tcz0Ymw0ggoaVDMveUB9w8kFSQScdRLo0gxO9J7WFcUBo+W3C1TLdFIEwNOWebgZZ0RH9Q==
2264+
22602265
trim-newlines@^1.0.0:
22612266
version "1.0.0"
22622267
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"

0 commit comments

Comments
 (0)