Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Apr 9, 2023
1 parent e6be22c commit de2688d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 5 additions & 3 deletions bin/peggy-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ class PeggyCLI extends Command {
this.watcher = new Watcher(this.inputFile);

const that = this;
this.watcher.on("change", async event => {
PeggyCLI.print(this.std.err, `"${that.inputFile}" ${event}...`);
this.watcher.on("change", async() => {
PeggyCLI.print(this.std.err, `"${that.inputFile}" changed...`);
this.lastError = null;
await that.run();

Expand All @@ -728,7 +728,9 @@ class PeggyCLI extends Command {
});

return new Promise((resolve, reject) => {
this.watcher.on("error", reject);
this.watcher.on("error", er => {
reject(er);
});
this.watcher.on("close", () => resolve(0));
});
} else {
Expand Down
5 changes: 4 additions & 1 deletion bin/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ class Watcher extends EventEmitter {
}, Watcher.interval);
}
};
const closed = () => this.emit("close");

this.watcher = fs.watch(dir);
this.watcher.on("error", er => {
this.watcher.off("close", closed);
this.watcher.once("close", () => this.emit("error", er));
this.watcher.close();
this.watcher = null;
});
this.watcher.on("close", er => this.emit("close", er));
this.watcher.on("close", closed);
this.watcher.on("change", changed);

// Fire initial time if file exists.
Expand Down
4 changes: 2 additions & 2 deletions test/cli/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1210,15 +1210,15 @@ error: Rule "unknownRule" is not defined
});
});

it("handles chokidar errors", async() => {
it("handles watcher errors", async() => {
const grammar = path.join(__dirname, "fixtures", "simple.peggy");
let count = 0;
await exec({
args: ["-w", grammar],
error: "Fake error",
onstdout(s, cli) {
if (++count === 3) {
cli.watcher?.emit("error", new Error("Fake error"));
cli.watcher?.watcher.emit("error", new Error("Fake error"));
}
},
});
Expand Down

0 comments on commit de2688d

Please sign in to comment.