Skip to content

Commit

Permalink
chore: update unlink
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Feb 12, 2024
1 parent 5ff274f commit 58929d9
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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();
},
});

Expand All @@ -225,6 +232,9 @@ function run() {
if (fileChangeWatcher) {
await fileChangeWatcher.close();
}
if (fileDirDeleteWatcher) {
await fileDirDeleteWatcher.close();
}
process.exit(0);
} catch (err) {
console.error(err);
Expand Down

0 comments on commit 58929d9

Please sign in to comment.