Skip to content

Commit 8c91cd2

Browse files
committed
add --file-change-hook flag
1 parent d2b7ec1 commit 8c91cd2

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Look up flags and options can be used [in ts-node's docs](https://github.com/Typ
5252
* `--rs` - Allow to restart with "rs" line entered in stdio, disabled by default.
5353
* `--notify` - to display desktop-notifications (Notifications are only displayed if `node-notifier` is installed).
5454
* `--cache-directory` - tmp dir which is used to keep the compiled sources (by default os tmp directory is used)
55+
* `--file-change-hook` - Bind a file watch hook.
5556

5657
If you need to detect that you are running with `ts-node-dev`, check if `process.env.TS_NODE_DEV` is set.
5758

src/bin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const devFlags = {
9494
'debounce',
9595
'watch',
9696
'cache-directory',
97+
'file-change-hook'
9798
],
9899
}
99100

@@ -117,6 +118,7 @@ type DevOptions = {
117118
'exec-check': boolean
118119
'exit-child': boolean
119120
'cache-directory': string
121+
'file-change-hook': string
120122
'error-recompile': boolean
121123
quiet: boolean
122124
'tree-kill': boolean

src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { fork, ChildProcess } from 'child_process'
22
import chokidar from 'chokidar'
33
import fs from 'fs'
4+
import path from 'path'
5+
import vm from 'vm'
46
import readline from 'readline'
57

68
const kill = require('tree-kill')
@@ -240,6 +242,25 @@ export const runDev = (
240242
} else {
241243
notify('Restarting', file + ' has been modified')
242244
}
245+
246+
if (opts['file-change-hook']) {
247+
const scriptPath = path.resolve(process.cwd(), opts['file-change-hook'])
248+
const code = `require("${scriptPath}").default("${file}");`
249+
try {
250+
const result = vm.runInNewContext(code, {
251+
require: require("esm")(module),
252+
module,
253+
console
254+
});
255+
if (!result) {
256+
notify('FileChangeHook', 'Hook exit code: 0.')
257+
}
258+
} catch (err) {
259+
notify('FileChangeHook', err)
260+
console.log(err)
261+
}
262+
}
263+
243264
compiler.compileChanged(file)
244265
if (starting) {
245266
log.debug('Already starting')

test/fixture/fileChangHook.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function (file) {
2+
console.log('change file:' + file);
3+
return 0
4+
}

test/tsnd.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('ts-node-dev', function () {
6666
await ps.waitForLine(/\[ERROR\]/)
6767
const out = ps.getStdout()
6868
const err = ps.getStderr()
69-
69+
7070
t.ok(/Compilation error in/.test(err), 'Reports error file')
7171
t.ok(/[ERROR].*Unable to compile TypeScript/.test(out), 'Report TS error')
7272
t.ok(/Argument of type/.test(out), 'Report TS error diagnostics')
@@ -159,7 +159,7 @@ describe('ts-node-dev', function () {
159159
await ps.waitForLine(/JS MODULE/)
160160
t.ok(true, 'ok')
161161
await ps.exit()
162-
})
162+
})
163163

164164
it('should handle -r esm option and load JS modules', async () => {
165165
const ps = spawnTsNodeDev([`--respawn`, `-r esm`, `js-module.js`].join(' '))
@@ -361,4 +361,15 @@ describe('ts-node-dev', function () {
361361
const list = fs.readdirSync(cacheDir)
362362
t.ok(list[0] === 'compiled', '"compiled" dir is there')
363363
})
364+
365+
it('should handle --file-change-hook flag', async () => {
366+
writeFile('test.ts', 'a')
367+
const ps = spawnTsNodeDev([`--file-change-hook`, `fileChangHook.js`, `--respawn`, `--watch`, `test.ts`, `simple.ts`].join(' '))
368+
await ps.waitForLine(/v1/)
369+
writeFile('test.ts', 'b')
370+
await ps.waitForLine(/Restarting.*test.ts/)
371+
t.ok(true, 'works')
372+
await ps.exit()
373+
await removeFile('test.ts')
374+
})
364375
})

0 commit comments

Comments
 (0)