Skip to content

Commit

Permalink
fix: directory listener after init (#19)
Browse files Browse the repository at this point in the history
* fix: directory listener after init

* fix: directory listener after init

* fix: directory listener after init
  • Loading branch information
czy88840616 authored May 6, 2024
1 parent 72e5c6f commit 7db4ec3
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pnpm-lock.yaml
yarn.lock
package-lock.json
test/fixtures/add_file/a.ts
test/fixtures/test_build/a.js
36 changes: 21 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,32 @@ function run() {
restart();
}

const sourceAbsoluteDir = path.join(cwd, sourceDir);
/**
* 不同平台的监听有很多差异,所以这里采用全文件监听的方案
* 文件的添加,修改交给 tsc 监听,这里只监听删除
*/
const fileDeleteWatcher = chokidar
.watch(sourceAbsoluteDir, {
cwd: sourceAbsoluteDir,
})
.on('all', (event, path) => {
// windows 下无法触发 unlinkDir 事件
if (event === 'unlink' || event === 'unlinkDir') {
cleanOutDirAndRestart(path);
}
});
let fileDeleteWatcher;
function watchDeleteFile() {
if (fileDeleteWatcher) return;
const sourceAbsoluteDir = path.join(cwd, sourceDir);
/**
* 不同平台的监听有很多差异,所以这里采用全文件监听的方案
* 文件的添加,修改交给 tsc 监听,这里只监听删除
*/
fileDeleteWatcher = chokidar
.watch(sourceAbsoluteDir, {
cwd: sourceAbsoluteDir,
})
.on('all', (event, path) => {
// windows 下无法触发 unlinkDir 事件
if (event === 'unlink' || event === 'unlinkDir') {
cleanOutDirAndRestart(path);
}
});
}

// 启动执行 tsc 命令
const child = forkTsc(tscArgs, {
cwd,
onFirstWatchCompileSuccess: () => {
runAfterTsc();
watchDeleteFile();
if (runCmd) {
runChild = forkRun(runCmd, runArgs, {
cwd,
Expand Down Expand Up @@ -232,6 +237,7 @@ function run() {
onWatchCompileFail: () => {
debug('watch compile fail');
triggerMessage('watch-compile-fail');
watchDeleteFile();
restart.clear();
},
onCompileSuccess: () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"tsc-watch"
],
"scripts": {
"test": "jest --testTimeout=30000 --runInBand --forceExit",
"test": "jest --testTimeout=30000 --runInBand",
"cov": "jest --testTimeout=30000 --coverage --runInBand --forceExit"
},
"repository": {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/test_build/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Test {}
13 changes: 13 additions & 0 deletions test/fixtures/test_build/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"moduleResolution": "node",
},
"exclude": [
"dist",
"node_modules",
"test"
]
}
62 changes: 30 additions & 32 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,37 @@ const { execa, sleep } = require('./util');

const mtscPath = join(__dirname, '../bin/mwtsc.js');

async function removeFile(fileList) {
for (const f of fileList) {
if (existsSync(f)) {
await unlink(f);
}
}
}

describe('/test/index.js', () => {
it.skip('should throw error when no --run parameter', async () => {
const cp = await execa('node', [mtscPath], {});
it('should compile ts file completely and exit', async () => {
const runPath = join(__dirname, 'fixtures/test_build');
await removeFile([
join(runPath, 'a.js'),
]);

const cp = await execa('node', [mtscPath], {
cwd: runPath,
});

await new Promise(resolve => {
const h = setTimeout(() => {
throw new Error('Child process is running timeout');
}, 1000);

cp.on('exit', code => {
clearTimeout(h);
expect(existsSync(join(runPath, 'a.js'))).toBeTruthy();
console.log('exit', code);
resolve();
});

setTimeout(() => {
cp.kill();
}, 3000);
})
});
});

it('should compile ts file and ignore run script without watch args', async () => {
Expand Down Expand Up @@ -48,16 +65,10 @@ describe('/test/index.js', () => {
// prepare
const runPath = join(__dirname, 'fixtures/add_file');
const file = join(runPath, 'a.ts');
const fileList = [
await removeFile([
file,
join(runPath, 'dist/a.js'),
]

for (const f of fileList) {
if (existsSync(f)) {
await unlink(f);
}
}
]);

const cp = await execa('node', [mtscPath, '--watch', '--run', './run.js'], {
cwd: runPath,
Expand Down Expand Up @@ -94,16 +105,10 @@ describe('/test/index.js', () => {
// prepare
const runPath = join(__dirname, 'fixtures/remove_file');
const file = join(runPath, 'src/a.ts');
const fileList = [
await removeFile([
file,
join(runPath, 'dist/a.js'),
]

for (const f of fileList) {
if (existsSync(f)) {
await unlink(f);
}
}
]);

const cp = await execa('node', [mtscPath, '--watch', '--run', './run.js'], {
cwd: runPath,
Expand Down Expand Up @@ -144,17 +149,10 @@ describe('/test/index.js', () => {
// prepare
const runPath = join(__dirname, 'fixtures/add_file');
const file = join(runPath, 'a.ts');

const fileList = [
await removeFile([
file,
join(runPath, 'dist/a.js'),
]

for (const f of fileList) {
if (existsSync(f)) {
await unlink(f);
}
}
]);

// add a error file
writeFileSync(file, 'console.log("a)');
Expand Down
13 changes: 11 additions & 2 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const cp = require('child_process');
function execa(cmd, args, options) {
return new Promise((resolve, reject) => {
// mock execa
const child = cp.spawn(cmd, args, Object.assign({
const child = cp.spawn(cmd, args, {
cwd: __dirname,
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
}, options));
...options,
});

child.on('message', (data) => {
if (args.includes('--watch')) {
Expand All @@ -23,6 +24,14 @@ function execa(cmd, args, options) {
}
}
});

child.on('error', (err) => {
reject(err);
});

child.once('exit', (code) => {
console.log('exit', code);
});
});

return child;
Expand Down

0 comments on commit 7db4ec3

Please sign in to comment.