Skip to content

Commit 7f09602

Browse files
committed
refactor: process exit in cli instead of in modules
1 parent ab4c3f2 commit 7f09602

6 files changed

Lines changed: 32 additions & 24 deletions

File tree

packages/scaffold/src/cli.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Base } from "./core/base.js";
2+
import type { Context, OverrideConfig } from "./types/index.js";
13
import process from "node:process";
24
import { Command } from "commander";
35
import pkg from "../package.json" with { type: "json" };
@@ -26,11 +28,11 @@ async function main() {
2628
.description("Build the plugin")
2729
.option("--dev", "Builds the plugin in dev mode")
2830
.option("--dist <dir>", "The relative path for the new output directory (default: build)")
29-
.action((options) => {
31+
.action(async (options) => {
3032
process.env.NODE_ENV = options.dev ? "development" : "production";
31-
Config.loadConfig({
33+
await runCommand(Build, {
3234
dist: options.dist,
33-
}).then(ctx => new Build(ctx).run());
35+
});
3436
});
3537

3638
cli
@@ -45,32 +47,29 @@ async function main() {
4547
// "--only-start",
4648
// "skip building website before deploy it (default: false)",
4749
// )
48-
.action((_options) => {
49-
Config.loadConfig({}).then(ctx => new Serve(ctx).run());
50+
.action(async (_options) => {
51+
await runCommand(Serve, {});
5052
});
5153

5254
cli.command("test")
5355
.description("Run tests")
5456
.option("--abort-on-fail", "Abort the test suite on first failure")
5557
.option("--exit-on-finish", "Exit the test suite after all tests have run")
5658
.option("--no-watch", "Exit the test suite after all tests have run")
57-
.action((options) => {
59+
.action(async (options) => {
5860
process.env.NODE_ENV = "test";
59-
60-
Config.loadConfig({
61+
await runCommand(Test, {
6162
test: {
6263
abortOnFail: options.abortOnFail,
6364
watch: !options.exitOnFinish && options.watch,
6465
},
65-
}).then((ctx) => {
66-
new Test(ctx).run();
6766
});
6867
});
6968

7069
cli
7170
.command("create")
7271
.description("Create the plugin template")
73-
.action((_options: any) => {
72+
.action(async (_options: any) => {
7473
logger.error("The create not yet implemented");
7574
// new Create().run();
7675
});
@@ -83,15 +82,15 @@ async function main() {
8382
.option("-y, --yes", "Skip confirmation")
8483
.action(async (version, options) => {
8584
process.env.NODE_ENV = "production";
86-
Config.loadConfig({
85+
await runCommand(Release, {
8786
release: {
8887
bumpp: {
8988
release: version,
9089
preid: options.preid,
9190
confirm: !options.yes,
9291
},
9392
},
94-
}).then(ctx => new Release(ctx).run());
93+
});
9594
});
9695

9796
cli.arguments("<command>").action((cmd) => {
@@ -100,7 +99,18 @@ async function main() {
10099
});
101100

102101
cli.parse();
103-
// globalOpts = cli.optsWithGlobals();
102+
}
103+
104+
type Constructor<T> = new (ctx: Context) => T;
105+
106+
export async function runCommand<T extends Base>(
107+
CommandClass: Constructor<T>,
108+
config: OverrideConfig,
109+
) {
110+
const ctx = await Config.loadConfig(config);
111+
const instance = new CommandClass(ctx);
112+
process.on("SIGINT", instance.exit.bind(instance));
113+
await instance.run();
104114
}
105115

106116
export default async function mainWithErrorHandler() {

packages/scaffold/src/core/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export abstract class Base {
66
this.ctx = ctx;
77
}
88

9-
abstract run(): any;
9+
abstract run(): void | Promise<void>;
10+
11+
abstract exit(): void;
1012

1113
get logger() {
1214
return this.ctx.logger;

packages/scaffold/src/core/builder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,6 @@ export default class Build extends Base {
231231
zip.addLocalFolder(`${dist}/addon`);
232232
zip.writeZip(`${dist}/${xpiName}.xpi`);
233233
}
234+
235+
exit() {}
234236
}

packages/scaffold/src/core/releaser/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ export default class Release extends Base {
197197
return false;
198198
}
199199

200+
exit() {}
201+
200202
get resolvedCommitMessage() {
201203
return this.ctx.release.bumpp.commit.replace("%s", this.ctx.version);
202204
}

packages/scaffold/src/core/server.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ export default class Serve extends Base {
2020
}
2121

2222
async run() {
23-
// Handle interrupt signal (Ctrl+C) to gracefully terminate Zotero process
24-
// Must be placed at the top to prioritize registration of events to prevent web-ext interference
25-
process.on("SIGINT", this.exit);
26-
2723
this.runner = new ZoteroRunner({
2824
binary: {
2925
path: this.zoteroBinPath,

packages/scaffold/src/core/tester/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ export default class Test extends Base {
3232
}
3333

3434
async run() {
35-
// Handle interrupt signal (Ctrl+C) to gracefully terminate Zotero process
36-
// Must be placed at the top to prioritize registration of events to prevent web-ext interference
37-
process.on("SIGINT", this.exit);
38-
3935
// Empty dirs
4036
await emptyDir(TESTER_PROFILE_DIR);
4137
await emptyDir(TESTER_DATA_DIR);
@@ -149,7 +145,7 @@ export default class Test extends Base {
149145
process.exit(0);
150146
};
151147

152-
private exit = (code?: string | number) => {
148+
exit = (code?: string | number) => {
153149
if (code === "SIGINT") {
154150
this.logger.info("Tester shutdown by user request");
155151
}

0 commit comments

Comments
 (0)