Skip to content

Commit

Permalink
fix: improve stability when update tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Apr 15, 2024
1 parent 8343292 commit b6ea97e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-keys-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rnm/tscx": patch
---

fix: improve stability when update tsconfig
37 changes: 22 additions & 15 deletions packages/tscx/src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface TscxOptions extends CompilerOptions {
}

export class Action {
private lastUpdateTsconfigTime = Date.now();

private readonly compiler;
private watcher?: FSWatcher;
constructor(private readonly options: TscxOptions) {
Expand All @@ -23,19 +25,23 @@ export class Action {
.map((i) => path.resolve(process.cwd(), i))
.concat(path.resolve(process.cwd(), this.options.project));

this.watcher = chokidar.watch(watchFiles, {
ignored: ["**/node_modules/**", "**/.git/**", this.compiler.getOutDir()],
ignoreInitial: true,
});
this.watcher
this.watcher = chokidar
.watch(watchFiles, {
ignored: [
"**/node_modules/**",
"**/.git/**",
this.compiler.getOutDir(),
],
ignoreInitial: true,
})
.on("add", (filepath) => this.cb(filepath))
.on("unlink", (filepath) => this.cb(filepath))
.on("change", (filepath) => this.cb(filepath))
.on("ready", () => this.cb());
}

private cb(filepath?: string) {
console.log("Recompile for the file updated", filepath);
console.log("File changes detected", filepath);
// user edit non-tsconfig files
if (
!filepath ||
Expand All @@ -46,6 +52,10 @@ export class Action {
}

// user edit tsconfig file
const now = Date.now();
if (now - this.lastUpdateTsconfigTime < 1000) {
return;
}
try {
this.compiler.refreshTsConfig();
} catch (e) {
Expand All @@ -55,15 +65,12 @@ export class Action {
);
return;
}
this.watcher
?.close()
.then(() => {
this.setupWatcher();
})
.catch((e) => {
console.error("Close watcher fail!", e);
process.exit(1);
});
this.watcher?.close().catch((e) => {
console.error("Close watcher fail!", e);
process.exit(1);
});
this.setupWatcher();
this.lastUpdateTsconfigTime = now;
}

start() {
Expand Down

0 comments on commit b6ea97e

Please sign in to comment.