From 58929d969630ecabc5fcff628d75f8c46fcb68d2 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 13 Feb 2024 00:10:13 +0800 Subject: [PATCH] chore: update unlink --- lib/index.js | 78 +++++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/lib/index.js b/lib/index.js index 343ff74..03cdad6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -36,9 +36,11 @@ function run() { let tsconfig; let fileDeleteWatcher; let fileChangeWatcher; + let fileDirDeleteWatcher; let hasPaths = false; let tsconfigPath; let isCompileSuccess = false; + let fileChangeList = []; const projectIndex = tscArgs.findIndex(arg => arg === '--project'); if (projectIndex !== -1) { @@ -98,14 +100,33 @@ function run() { // 避免重复触发文件变化 isCompileSuccess = false; } + output(`${fileChangeList.length} ${colors.dim('Files has been changed.')}`, true); + // 清空文件列表 + fileChangeList.length = 0; runChild && runChild.restart(); }, 1000); + function runAfterTsc() { + if (hasPaths) { + // 替换 tsconfig 中的 alias 路径 + replaceTscAliasPaths({ + configFile: tsconfigPath, + outDir, + });2 + } + // 拷贝非 ts 文件 + copyFilesRecursive( + path.join(cwd, sourceDir), + path.join(cwd, outDir), + allowJs + ); + } function onFileChange(p) { // 这里使用了标识来判断是否编译成功,理论上会有时序问题,但是本质上事件还是同步执行的,测试下来感觉没有问题 if (!isCompileSuccess) return; - debug(`${colors.dim('File ')}${p}${colors.dim(' has been changed')}`); + debug(`${colors.dim('File ')}${p}${colors.dim(' has been changed.')}`); + fileChangeList.push(p); // 单个文件的 hot reload 处理 restart(); } @@ -114,19 +135,7 @@ function run() { const child = forkTsc(tscArgs, { cwd, onFirstWatchCompileSuccess: () => { - if (hasPaths) { - // 替换 tsconfig 中的 alias 路径 - replaceTscAliasPaths({ - configFile: tsconfigPath, - outDir, - }); - } - // copy non-ts files - copyFilesRecursive( - path.join(cwd, sourceDir), - path.join(cwd, outDir), - allowJs - ); + runAfterTsc(); if (runCmd) { runChild = forkRun(runCmd, runArgs, { cwd, @@ -162,22 +171,31 @@ function run() { fileDeleteWatcher .on('unlink', p => { + output(`File ${p} has been removed`, true); const filePath = path.join(cwd, outDir, p.replace(/\.ts$/, '.js')); if (fs.existsSync(filePath)) { fs.unlinkSync(filePath); - output(`File ${path} has been removed`); - onFileChange(path); + onFileChange(filePath); } - }) + }); + + fileDirDeleteWatcher = chokidar.watch('**/**', { + cwd: path.join(cwd, sourceDir), + }) .on('unlinkDir', p => { - console.log(`Directory ${p} has been removed`) + // 碰到任意的删除情况,直接删除整个构建目录 + deleteFolderRecursive(path.join(cwd, outDir)); + forkTsc(tscArgs,{ + cwd, + }); + runAfterTsc(); + restart(); }); fileChangeWatcher = chokidar.watch('**/**.js', { cwd: path.join(cwd, outDir), - }); - - fileChangeWatcher.on('change', onFileChange); + }) + .on('change', onFileChange); } } else { output( @@ -190,25 +208,14 @@ function run() { }, onWatchCompileSuccess: () => { isCompileSuccess = true; + restart(); }, onWatchCompileFail: () => { isCompileSuccess = false; restart.clear(); }, onCompileSuccess: () => { - if (hasPaths) { - // 替换 tsconfig 中的 alias 路径 - replaceTscAliasPaths({ - configFile: tsconfigPath, - outDir, - }); - } - // 拷贝非 ts 文件 - copyFilesRecursive( - path.join(cwd, sourceDir), - path.join(cwd, outDir), - allowJs - ); + runAfterTsc(); }, }); @@ -225,6 +232,9 @@ function run() { if (fileChangeWatcher) { await fileChangeWatcher.close(); } + if (fileDirDeleteWatcher) { + await fileDirDeleteWatcher.close(); + } process.exit(0); } catch (err) { console.error(err);