-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.ts
More file actions
130 lines (109 loc) · 3.36 KB
/
server.ts
File metadata and controls
130 lines (109 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type { Context } from "../types/index.js";
import { existsSync } from "node:fs";
import { join } from "node:path";
import process from "node:process";
import { watch } from "../utils/watcher.js";
import { ZoteroRunner } from "../utils/zotero-runner.js";
import { Base } from "./base.js";
import Build from "./builder.js";
export default class Serve extends Base {
private builder: Build;
private runner?: ZoteroRunner;
private _zoteroBinPath?: string;
constructor(ctx: Context) {
super(ctx);
process.env.NODE_ENV ??= "development";
this.builder = new Build(ctx);
}
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);
this.runner = new ZoteroRunner({
binary: {
path: this.zoteroBinPath,
devtools: this.ctx.server.devtools,
args: this.ctx.server.startArgs,
},
profile: {
path: this.profilePath,
dataDir: this.dataDir,
// keepChanges: this.ctx.server.keepProfileChanges,
createIfMissing: this.ctx.server.createProfileIfMissing,
},
plugins: {
list: [{
id: this.ctx.id,
sourceDir: join(this.ctx.dist, "addon"),
}],
asProxy: this.ctx.server.asProxy,
},
});
await this.ctx.hooks.callHook("serve:init", this.ctx);
// prebuild
if (this.ctx.server.prebuild) {
await this.builder.run();
await this.ctx.hooks.callHook("serve:prebuild", this.ctx);
}
// start Zotero
await this.runner.run();
this.runner.zotero?.on("exit", this.onZoteroExit);
this.runner.zotero?.on("close", this.onZoteroExit);
// watch
await this.watch();
}
/**
* watch source dir and build when file changed
*/
async watch() {
const { source } = this.ctx;
watch(
source,
{
onReady: async () => {
await this.ctx.hooks.callHook("serve:ready", this.ctx);
},
onChange: async (path) => {
await this.ctx.hooks.callHook("serve:onChanged", this.ctx, path);
if (path.endsWith(".ts") || path.endsWith(".tsx")) {
await this.builder.esbuild();
}
else {
await this.builder.run();
}
await this.reload();
},
},
);
}
async reload() {
this.logger.tip("Reloading...");
await this.runner?.reloadAllPlugins();
await this.ctx.hooks.callHook("serve:onReloaded", this.ctx);
}
// Use arrow functions to keep `this`
exit = () => {
this.logger.info("Server shutdown by user request.");
this.runner?.exit();
this.ctx.hooks.callHook("serve:exit", this.ctx);
process.exit();
};
private onZoteroExit = (_code?: number | null, _signal?: any) => {
this.logger.info(`Zotero terminated.`);
process.exit();
};
get zoteroBinPath() {
if (this._zoteroBinPath)
return this._zoteroBinPath;
this._zoteroBinPath = process.env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH;
if (!this._zoteroBinPath || !existsSync(this._zoteroBinPath))
throw new Error("The Zotero binary not found.");
return this._zoteroBinPath;
}
get profilePath() {
return process.env.ZOTERO_PLUGIN_PROFILE_PATH;
}
get dataDir() {
return process.env.ZOTERO_PLUGIN_DATA_DIR;
}
}