Skip to content

Commit

Permalink
refactor: optimize action
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Dec 15, 2024
1 parent 7ccb8cc commit 68c2c34
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
8 changes: 1 addition & 7 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ export default new Builder()
.enablePackagejson()
.enableJavascript()
.enableTypescript({
omit: [
"@fenge/no-restricted-loops",
"@typescript-eslint/no-floating-promises",
"es-x/no-top-level-await",
"unicorn/no-process-exit",
"no-console",
],
omit: ["@fenge/no-restricted-loops", "no-console"],
})
.toConfig();
39 changes: 25 additions & 14 deletions packages/tscx/src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,9 @@ export class Action {
this.compiler = new Compiler(options);
}

private setupWatcher() {
const include = this.compiler.getInclude() ?? [];
const watchFiles =
include.length <= 0
? [process.cwd()]
: include
.map((i) => path.resolve(process.cwd(), i))
.concat(path.resolve(process.cwd(), this.options.project));

private watch(paths: string[]) {
this.watcher = chokidar
.watch(watchFiles, {
.watch(paths, {
ignored: [
"**/node_modules/**",
"**/.git/**",
Expand All @@ -41,6 +33,29 @@ export class Action {
.on("ready", () => this.cb());
}

private setupWatcher() {
const include = this.compiler.getInclude() ?? [];
const watchFiles =
include.length <= 0
? [process.cwd()]
: include
.map((i) => path.resolve(process.cwd(), i))
.concat(path.resolve(process.cwd(), this.options.project));

if (this.watcher) {
// If this method throw an error (I mean the promise rejected), the process will exit with non-zero code.
// See https://github.com/zanminkian/zanminkian.github.io/issues/54
this.watcher.close().then(
() => this.watch(watchFiles),
(e) => {
throw new Error("Close watcher fail!", { cause: e });
},
);
} else {
this.watch(watchFiles);
}
}

private cb(filepath?: string) {
console.log("File changes detected", filepath);
// user edit non-tsconfig files
Expand All @@ -66,10 +81,6 @@ export class Action {
);
return;
}
this.watcher?.close().catch((e) => {
console.error("Close watcher fail!", e);
process.exit(1);
});
this.setupWatcher();
this.lastUpdateTsconfigTime = now;
}
Expand Down

0 comments on commit 68c2c34

Please sign in to comment.