diff --git a/.gitignore b/.gitignore index 4cf59f3..fe2026d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ temp/ dist/ pnpm-lock.yaml yarn.lock -package-lock.json \ No newline at end of file +package-lock.json +test/fixtures/add_file/a.ts diff --git a/lib/wrap.js b/lib/wrap.js index f565fc5..201f6cf 100644 --- a/lib/wrap.js +++ b/lib/wrap.js @@ -1,6 +1,7 @@ // 拿到执行路径,以及执行文件 const childPath = process.env.CHILD_CMD_PATH; const childCwd = process.env.CHILD_CWD; +const { join } = require('path'); process.on('message', data => { if (data.title === 'server-kill') { @@ -9,4 +10,4 @@ process.on('message', data => { }); process.chdir(childCwd); -require(childPath); +require(join(childCwd, childPath)); diff --git a/package.json b/package.json index f1cdc69..b75aa99 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "execa": "^5.1.1", "jest": "^29.7.0", "mwts": "^1.3.0", - "typescript": "~4.9.5" + "typescript": "^5.4.5" }, "engines": { "node": ">=12.0.0" diff --git a/test/fixtures/add_file/b.ts b/test/fixtures/add_file/b.ts new file mode 100644 index 0000000..6a91933 --- /dev/null +++ b/test/fixtures/add_file/b.ts @@ -0,0 +1 @@ +console.log('b') diff --git a/test/fixtures/add_file/run.js b/test/fixtures/add_file/run.js new file mode 100644 index 0000000..860b5fb --- /dev/null +++ b/test/fixtures/add_file/run.js @@ -0,0 +1,2 @@ +console.log('abc') +process.send({ type: 'ready' }); diff --git a/test/fixtures/add_file/tsconfig.json b/test/fixtures/add_file/tsconfig.json new file mode 100644 index 0000000..b3be0de --- /dev/null +++ b/test/fixtures/add_file/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "target": "es2018", // target es2018 + "module": "commonjs", + "moduleResolution": "node", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "inlineSourceMap":true, + "noImplicitThis": true, + "noUnusedLocals": true, + "stripInternal": true, + "skipLibCheck": true, + "pretty": true, + "outDir": "dist" + }, + "exclude": [ + "dist", + "node_modules", + "test" + ] +} diff --git a/test/index.test.js b/test/index.test.js index c4f35b5..fe058e3 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,6 +1,6 @@ const execa = require('execa'); const { join, resolve } = require('path'); -const { existsSync, unlinkSync } = require('fs'); +const { existsSync, unlinkSync, writeFileSync, readFileSync } = require('fs'); const { forkRun } = require('../lib/process'); const mtscPath = join(__dirname, '../bin/mwtsc.js'); @@ -81,4 +81,41 @@ describe('/test/index.js', () => { }); }); }); + + it('should test ts file change and reload process', async () => { + + // prepare + const runPath = join(__dirname, 'fixtures/add_file'); + const file = join(runPath, 'a.ts'); + if (existsSync(file)) { + unlinkSync(file); + } + + const cp = execa('node', [mtscPath, '--watch', '--run', './run.js'], { + cwd: runPath, + // stdio: 'ignore', + }); + + // add a new file + writeFileSync(file, 'console.log("a")'); + + // change file + writeFileSync(file, 'console.log("b")'); + + await new Promise((resolve, reject) => { + cp.on('exit', code => { + try { + expect(existsSync(join(runPath, 'dist/a.js'))).toBeTruthy(); + expect(readFileSync(join(runPath, 'dist/a.js'), 'utf-8')).toMatch(/b/); + resolve(); + } catch (err) { + reject(err); + } + }); + + setTimeout(() => { + cp.kill(); + }, 10000); + }); + }); });