Skip to content

Commit

Permalink
fix: ctrl+c should kill Zotero
Browse files Browse the repository at this point in the history
  • Loading branch information
northword committed Sep 1, 2024
1 parent 3430f54 commit 364a0f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { env, exit } from "node:process";
import process, { env, exit } from "node:process";
import { Command } from "@commander-js/extra-typings";
import updateNotifier from "update-notifier";
import { name, version } from "../package.json";
Expand All @@ -16,6 +16,10 @@ const logger = new Log();

export default async function main() {
updateNotifier({ pkg: { name, version } }).notify();
// Remove SIGINT listeners registered by other programs to
// ensure that Scaffold-registered listeners take effect.
// see https://github.com/yeoman/update-notifier/pull/237
process.removeAllListeners("SIGINT");

// Env variables are initialized to dev, but can be overridden by each command
// For example, "zotero-plugin build" overrides them to "production"
Expand Down
9 changes: 4 additions & 5 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ export default class Serve extends Base {
async run() {
// Handle interrupt signal (Ctrl+C) to gracefully terminate Zotero process
// Must be placed at the top to prioritize registration of events to prevent web-ext interference
process.on("SIGINT", () => {
this.exit();
});
process.on("SIGINT", this.exit);

await this.ctx.hooks.callHook("serve:init", this.ctx);

Expand Down Expand Up @@ -98,13 +96,14 @@ export default class Serve extends Base {
await this.ctx.hooks.callHook("serve:onReloaded", this.ctx);
}

exit() {
// Use arrow functions to keep `this`
exit = () => {
this.logger.info("Server shutdown by user request.");
this.runner?.exit();
// Sometimes `runner.exit()` cannot kill the Zotero,
// so we force kill it.
killZotero();
this.ctx.hooks.callHook("serve:exit", this.ctx);
process.exit();
}
};
}

0 comments on commit 364a0f3

Please sign in to comment.