Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit dd9ed26

Browse files
committed
Disable watcher whilst building, fixes #34
1 parent e3f5d8a commit dd9ed26

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"types": "dist/index.d.ts",
1616
"files": [
1717
"dist",
18-
"src",
19-
"vendor"
18+
"src"
2019
],
2120
"scripts": {
2221
"build": "tsc -p tsconfig.build.json",

src/options/processor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export class OptionsProcessor {
122122
this._scriptBlueprints[scriptPath] = new ScriptBlueprint(code, scriptPath);
123123
}
124124

125-
runCustomBuild(command: string, basePath?: string): Promise<void> {
125+
runCustomBuild(command: string, basePath?: string): Promise<boolean> {
126126
return this._buildMutex.run(
127127
() =>
128128
new Promise((resolve) => {
@@ -132,12 +132,13 @@ export class OptionsProcessor {
132132
stdio: "inherit",
133133
});
134134
build.on("exit", (code) => {
135-
if (code === 0) {
135+
const succeeded = code === 0;
136+
if (succeeded) {
136137
this.log.info("Build succeeded");
137138
} else {
138139
this.log.error(`Build failed with exit code ${code}`);
139140
}
140-
resolve();
141+
resolve(succeeded);
141142
});
142143
})
143144
);

src/options/watcher.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class OptionsWatcher {
2222
private _watcher?: chokidar.FSWatcher;
2323
private _watchedPaths?: Set<string>;
2424
private _extraWatchedPaths?: Set<string>;
25+
private _building = false;
2526

2627
readonly initPromise: Promise<void>;
2728

@@ -118,7 +119,7 @@ export class OptionsWatcher {
118119
.on("unlink", boundCallback);
119120
}
120121

121-
private _watchedPathCallback(eventPath: string) {
122+
private async _watchedPathCallback(eventPath: string) {
122123
if (
123124
this._options?.buildWatchPath &&
124125
eventPath.startsWith(this._options.buildWatchPath)
@@ -129,13 +130,22 @@ export class OptionsWatcher {
129130
);
130131
// Re-run build, this should change a script triggering the watcher
131132
// again
132-
void this._processor.runCustomBuild(
133-
this._options.buildCommand,
134-
this._options.buildBasePath
135-
);
133+
this._building = true;
134+
try {
135+
const succeeded = await this._processor.runCustomBuild(
136+
this._options.buildCommand,
137+
this._options.buildBasePath
138+
);
139+
if (succeeded) await this.reloadOptions(false);
140+
} finally {
141+
// Wait a little bit before starting to process watch events again
142+
// to allow built file changes to come through
143+
setTimeout(() => (this._building = false), 50);
144+
}
136145
}
137-
} else {
138-
// If the path isn't in buildWatchPath, reload options and scripts
146+
} else if (!this._building) {
147+
// If the path isn't in buildWatchPath, reload options and scripts,
148+
// provided we're not currently building
139149
this.log.debug(`${path.relative("", eventPath)} changed, reloading...`);
140150

141151
// Log options is this was an options file, we don't want to spam the log
@@ -145,7 +155,7 @@ export class OptionsWatcher {
145155
eventPath === this._processor.packagePath ||
146156
eventPath === this._options?.envPath;
147157

148-
void this.reloadOptions(log);
158+
await this.reloadOptions(log);
149159
}
150160
}
151161

test/options/watcher.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ watchTest("rebuilds if watched build path changes", async (t) => {
222222
const watchedPath = path.join(tmp, "watch.txt");
223223
const scriptPath = path.join(tmp, "script.js");
224224
const relativeWatchedPath = path.relative("", watchedPath);
225-
const relativeScriptPath = path.relative("", scriptPath);
226225
await fs.writeFile(watchedPath, "1", "utf8");
227226

228227
const watcher = new OptionsWatcher(
@@ -247,7 +246,6 @@ watchTest("rebuilds if watched build path changes", async (t) => {
247246
await fs.writeFile(watchedPath, "2", "utf8");
248247
options = await next();
249248
t.is(log.debugs[0], `${relativeWatchedPath} changed, rebuilding...`);
250-
t.is(log.debugs[1], `${relativeScriptPath} changed, reloading...`);
251249
t.is(options.scripts?.[scriptPath].code.trim(), `// build${os.EOL}// build`);
252250
});
253251

0 commit comments

Comments
 (0)