From 8c91cd226850563e943ce41c268430fdeb2c1f36 Mon Sep 17 00:00:00 2001
From: yi-ge
Date: Thu, 4 Mar 2021 19:26:07 +0800
Subject: [PATCH 1/6] add --file-change-hook flag
---
README.md | 1 +
src/bin.ts | 2 ++
src/index.ts | 21 +++++++++++++++++++++
test/fixture/fileChangHook.js | 4 ++++
test/tsnd.test.ts | 15 +++++++++++++--
5 files changed, 41 insertions(+), 2 deletions(-)
create mode 100644 test/fixture/fileChangHook.js
diff --git a/README.md b/README.md
index 5b8fa7e..795b73e 100644
--- a/README.md
+++ b/README.md
@@ -52,6 +52,7 @@ Look up flags and options can be used [in ts-node's docs](https://github.com/Typ
* `--rs` - Allow to restart with "rs" line entered in stdio, disabled by default.
* `--notify` - to display desktop-notifications (Notifications are only displayed if `node-notifier` is installed).
* `--cache-directory` - tmp dir which is used to keep the compiled sources (by default os tmp directory is used)
+* `--file-change-hook` - Bind a file watch hook.
If you need to detect that you are running with `ts-node-dev`, check if `process.env.TS_NODE_DEV` is set.
diff --git a/src/bin.ts b/src/bin.ts
index 0f3ef47..5e90bba 100644
--- a/src/bin.ts
+++ b/src/bin.ts
@@ -94,6 +94,7 @@ const devFlags = {
'debounce',
'watch',
'cache-directory',
+ 'file-change-hook'
],
}
@@ -117,6 +118,7 @@ type DevOptions = {
'exec-check': boolean
'exit-child': boolean
'cache-directory': string
+ 'file-change-hook': string
'error-recompile': boolean
quiet: boolean
'tree-kill': boolean
diff --git a/src/index.ts b/src/index.ts
index ed1be9c..8fe60b9 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,8 @@
import { fork, ChildProcess } from 'child_process'
import chokidar from 'chokidar'
import fs from 'fs'
+import path from 'path'
+import vm from 'vm'
import readline from 'readline'
const kill = require('tree-kill')
@@ -240,6 +242,25 @@ export const runDev = (
} else {
notify('Restarting', file + ' has been modified')
}
+
+ if (opts['file-change-hook']) {
+ const scriptPath = path.resolve(process.cwd(), opts['file-change-hook'])
+ const code = `require("${scriptPath}").default("${file}");`
+ try {
+ const result = vm.runInNewContext(code, {
+ require: require("esm")(module),
+ module,
+ console
+ });
+ if (!result) {
+ notify('FileChangeHook', 'Hook exit code: 0.')
+ }
+ } catch (err) {
+ notify('FileChangeHook', err)
+ console.log(err)
+ }
+ }
+
compiler.compileChanged(file)
if (starting) {
log.debug('Already starting')
diff --git a/test/fixture/fileChangHook.js b/test/fixture/fileChangHook.js
new file mode 100644
index 0000000..1734308
--- /dev/null
+++ b/test/fixture/fileChangHook.js
@@ -0,0 +1,4 @@
+export default function (file) {
+ console.log('change file:' + file);
+ return 0
+}
\ No newline at end of file
diff --git a/test/tsnd.test.ts b/test/tsnd.test.ts
index 991a9f2..0651fea 100644
--- a/test/tsnd.test.ts
+++ b/test/tsnd.test.ts
@@ -66,7 +66,7 @@ describe('ts-node-dev', function () {
await ps.waitForLine(/\[ERROR\]/)
const out = ps.getStdout()
const err = ps.getStderr()
-
+
t.ok(/Compilation error in/.test(err), 'Reports error file')
t.ok(/[ERROR].*Unable to compile TypeScript/.test(out), 'Report TS error')
t.ok(/Argument of type/.test(out), 'Report TS error diagnostics')
@@ -159,7 +159,7 @@ describe('ts-node-dev', function () {
await ps.waitForLine(/JS MODULE/)
t.ok(true, 'ok')
await ps.exit()
- })
+ })
it('should handle -r esm option and load JS modules', async () => {
const ps = spawnTsNodeDev([`--respawn`, `-r esm`, `js-module.js`].join(' '))
@@ -361,4 +361,15 @@ describe('ts-node-dev', function () {
const list = fs.readdirSync(cacheDir)
t.ok(list[0] === 'compiled', '"compiled" dir is there')
})
+
+ it('should handle --file-change-hook flag', async () => {
+ writeFile('test.ts', 'a')
+ const ps = spawnTsNodeDev([`--file-change-hook`, `fileChangHook.js`, `--respawn`, `--watch`, `test.ts`, `simple.ts`].join(' '))
+ await ps.waitForLine(/v1/)
+ writeFile('test.ts', 'b')
+ await ps.waitForLine(/Restarting.*test.ts/)
+ t.ok(true, 'works')
+ await ps.exit()
+ await removeFile('test.ts')
+ })
})
From 4de3d904ad0373e9698fef910578aad0aa207c67 Mon Sep 17 00:00:00 2001
From: yi-ge
Date: Thu, 4 Mar 2021 20:03:08 +0800
Subject: [PATCH 2/6] update esm
---
package.json | 2 +-
yarn.lock | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/package.json b/package.json
index 91ad2c7..a4258e7 100644
--- a/package.json
+++ b/package.json
@@ -44,6 +44,7 @@
"chokidar": "^3.5.1",
"dateformat": "~1.0.4-1.2.3",
"dynamic-dedupe": "^0.3.0",
+ "esm": "^3.2.25",
"minimist": "^1.2.5",
"mkdirp": "^1.0.4",
"resolve": "^1.0.0",
@@ -70,7 +71,6 @@
"chalk": "^4.1.0",
"coffee-script": "^1.8.0",
"eslint": "^7.7.0",
- "esm": "^3.2.22",
"fs-extra": "^9.0.1",
"mocha": "^8.1.1",
"np": "^6.5.0",
diff --git a/yarn.lock b/yarn.lock
index 064eda2..31721d1 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1457,10 +1457,10 @@ eslint@^7.7.0:
text-table "^0.2.0"
v8-compile-cache "^2.0.3"
-esm@^3.2.22:
- version "3.2.22"
- resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.22.tgz#5062c2e22fee3ccfee4e8f20da768330da90d6e3"
- integrity sha512-z8YG7U44L82j1XrdEJcqZOLUnjxco8pO453gKOlaMD1/md1n/5QrscAmYG+oKUspsmDLuBFZrpbxI6aQ67yRxA==
+esm@^3.2.25:
+ version "3.2.25"
+ resolved "https://registry.npm.taobao.org/esm/download/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10"
+ integrity sha1-NCwYwp1WFXaIulzjH4Qx+7eVzBA=
espree@^7.2.0:
version "7.2.0"
From 84ed72b3c196d798e56eef855c29641c08c1b81b Mon Sep 17 00:00:00 2001
From: yi-ge
Date: Thu, 4 Mar 2021 21:16:20 +0800
Subject: [PATCH 3/6] update --file-change-hook
---
package.json | 2 +-
src/index.ts | 21 +++++++++++----------
2 files changed, 12 insertions(+), 11 deletions(-)
diff --git a/package.json b/package.json
index a4258e7..23dd545 100644
--- a/package.json
+++ b/package.json
@@ -97,4 +97,4 @@
"publishConfig": {
"registry": "https://registry.npmjs.org"
}
-}
+}
\ No newline at end of file
diff --git a/src/index.ts b/src/index.ts
index 8fe60b9..96e6e83 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -40,9 +40,9 @@ export const runDev = (
// The child_process
let child:
| (ChildProcess & {
- stopping?: boolean
- respawn?: boolean
- })
+ stopping?: boolean
+ respawn?: boolean
+ })
| undefined
const wrapper = resolveMain(__dirname + '/wrap.js')
@@ -92,12 +92,12 @@ export const runDev = (
log.info(
'ts-node-dev ver. ' +
- version +
- ' (using ts-node ver. ' +
- tsNodeVersion +
- ', typescript ver. ' +
- tsVersion +
- ')'
+ version +
+ ' (using ts-node ver. ' +
+ tsNodeVersion +
+ ', typescript ver. ' +
+ tsVersion +
+ ')'
)
/**
@@ -250,7 +250,8 @@ export const runDev = (
const result = vm.runInNewContext(code, {
require: require("esm")(module),
module,
- console
+ console,
+ process
});
if (!result) {
notify('FileChangeHook', 'Hook exit code: 0.')
From b9adfe7bc4b6bed550f0c07c180738063da2231b Mon Sep 17 00:00:00 2001
From: yi-ge
Date: Thu, 4 Mar 2021 23:05:01 +0800
Subject: [PATCH 4/6] fix test file bug
---
test/fixture/{fileChangHook.js => fileChangeHook.js} | 0
test/tsnd.test.ts | 2 +-
2 files changed, 1 insertion(+), 1 deletion(-)
rename test/fixture/{fileChangHook.js => fileChangeHook.js} (100%)
diff --git a/test/fixture/fileChangHook.js b/test/fixture/fileChangeHook.js
similarity index 100%
rename from test/fixture/fileChangHook.js
rename to test/fixture/fileChangeHook.js
diff --git a/test/tsnd.test.ts b/test/tsnd.test.ts
index 0651fea..d11ca7b 100644
--- a/test/tsnd.test.ts
+++ b/test/tsnd.test.ts
@@ -364,7 +364,7 @@ describe('ts-node-dev', function () {
it('should handle --file-change-hook flag', async () => {
writeFile('test.ts', 'a')
- const ps = spawnTsNodeDev([`--file-change-hook`, `fileChangHook.js`, `--respawn`, `--watch`, `test.ts`, `simple.ts`].join(' '))
+ const ps = spawnTsNodeDev([`--file-change-hook`, `fileChangeHook.js`, `--respawn`, `--watch`, `test.ts`, `simple.ts`].join(' '))
await ps.waitForLine(/v1/)
writeFile('test.ts', 'b')
await ps.waitForLine(/Restarting.*test.ts/)
From cf5083092cd34be2a6ccf17d3ddb82532a549827 Mon Sep 17 00:00:00 2001
From: yi-ge
Date: Fri, 5 Mar 2021 15:49:24 +0800
Subject: [PATCH 5/6] fix esm bug
---
package.json | 2 +-
src/index.ts | 4 ++--
yarn.lock | 8 ++++----
3 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/package.json b/package.json
index 23dd545..733b7d4 100644
--- a/package.json
+++ b/package.json
@@ -44,7 +44,7 @@
"chokidar": "^3.5.1",
"dateformat": "~1.0.4-1.2.3",
"dynamic-dedupe": "^0.3.0",
- "esm": "^3.2.25",
+ "esm": "3.2.22",
"minimist": "^1.2.5",
"mkdirp": "^1.0.4",
"resolve": "^1.0.0",
diff --git a/src/index.ts b/src/index.ts
index 96e6e83..bc6d2dd 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -244,9 +244,9 @@ export const runDev = (
}
if (opts['file-change-hook']) {
- const scriptPath = path.resolve(process.cwd(), opts['file-change-hook'])
- const code = `require("${scriptPath}").default("${file}");`
try {
+ const scriptPath = path.resolve(process.cwd(), opts['file-change-hook'])
+ const code = `require("${scriptPath}").default("${file}");`
const result = vm.runInNewContext(code, {
require: require("esm")(module),
module,
diff --git a/yarn.lock b/yarn.lock
index 31721d1..d5d23a9 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1457,10 +1457,10 @@ eslint@^7.7.0:
text-table "^0.2.0"
v8-compile-cache "^2.0.3"
-esm@^3.2.25:
- version "3.2.25"
- resolved "https://registry.npm.taobao.org/esm/download/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10"
- integrity sha1-NCwYwp1WFXaIulzjH4Qx+7eVzBA=
+esm@3.2.22:
+ version "3.2.22"
+ resolved "https://registry.npm.taobao.org/esm/download/esm-3.2.22.tgz#5062c2e22fee3ccfee4e8f20da768330da90d6e3"
+ integrity sha1-UGLC4i/uPM/uTo8g2naDMNqQ1uM=
espree@^7.2.0:
version "7.2.0"
From f4e0a413f3ffe063dcc642121c88c97dd0f7d326 Mon Sep 17 00:00:00 2001
From: yi-ge
Date: Fri, 5 Mar 2021 15:54:45 +0800
Subject: [PATCH 6/6] restore esm
---
package.json | 2 +-
yarn.lock | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/package.json b/package.json
index 733b7d4..23dd545 100644
--- a/package.json
+++ b/package.json
@@ -44,7 +44,7 @@
"chokidar": "^3.5.1",
"dateformat": "~1.0.4-1.2.3",
"dynamic-dedupe": "^0.3.0",
- "esm": "3.2.22",
+ "esm": "^3.2.25",
"minimist": "^1.2.5",
"mkdirp": "^1.0.4",
"resolve": "^1.0.0",
diff --git a/yarn.lock b/yarn.lock
index d5d23a9..31721d1 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1457,10 +1457,10 @@ eslint@^7.7.0:
text-table "^0.2.0"
v8-compile-cache "^2.0.3"
-esm@3.2.22:
- version "3.2.22"
- resolved "https://registry.npm.taobao.org/esm/download/esm-3.2.22.tgz#5062c2e22fee3ccfee4e8f20da768330da90d6e3"
- integrity sha1-UGLC4i/uPM/uTo8g2naDMNqQ1uM=
+esm@^3.2.25:
+ version "3.2.25"
+ resolved "https://registry.npm.taobao.org/esm/download/esm-3.2.25.tgz#342c18c29d56157688ba5ce31f8431fbb795cc10"
+ integrity sha1-NCwYwp1WFXaIulzjH4Qx+7eVzBA=
espree@^7.2.0:
version "7.2.0"