Skip to content

Commit 9266dea

Browse files
committed
refactor: do not destructure when importing global Node modules
1 parent 7b471d2 commit 9266dea

9 files changed

Lines changed: 49 additions & 44 deletions

File tree

src/cli.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import process, { env, exit } from "node:process";
3+
import process from "node:process";
44
import { Command } from "@commander-js/extra-typings";
55
import pkg from "../package.json" with { type: "json" };
66
import { Build, Config, Release, Serve, Test } from "./index.js";
@@ -14,7 +14,7 @@ export default async function main() {
1414

1515
// Env variables are initialized to dev, but can be overridden by each command
1616
// For example, "zotero-plugin build" overrides them to "production"
17-
env.NODE_ENV ??= "development";
17+
process.env.NODE_ENV ??= "development";
1818

1919
const cli = new Command();
2020

@@ -29,7 +29,7 @@ export default async function main() {
2929
.option("--dev", "Builds the plugin in dev mode")
3030
.option("--dist <dir>", "The relative path for the new output directory (default: build)")
3131
.action((options) => {
32-
env.NODE_ENV = options.dev ? "development" : "production";
32+
process.env.NODE_ENV = options.dev ? "development" : "production";
3333
Config.loadConfig({
3434
dist: options.dist,
3535
}).then(ctx => new Build(ctx).run());
@@ -56,7 +56,7 @@ export default async function main() {
5656
.option("--abort-on-fail", "Abort the test suite on first failure")
5757
.option("--exit-on-finish", "Exit the test suite after all tests have run")
5858
.action((options) => {
59-
env.NODE_ENV = "test";
59+
process.env.NODE_ENV = "test";
6060

6161
Config.loadConfig({}).then((ctx) => {
6262
if (options.abortOnFail) {
@@ -84,7 +84,7 @@ export default async function main() {
8484
.option("--preid <preid>", "ID for prerelease")
8585
.option("-y, --yes", "Skip confirmation")
8686
.action(async (version, options) => {
87-
env.NODE_ENV = "production";
87+
process.env.NODE_ENV = "production";
8888
Config.loadConfig({
8989
release: {
9090
bumpp: {
@@ -115,5 +115,5 @@ process.on("uncaughtException", onError);
115115

116116
function onError(err: Error) {
117117
logger.error(err);
118-
exit(1);
118+
process.exit(1);
119119
}

src/core/builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Manifest } from "../types/manifest.js";
33
import type { UpdateJSON } from "../types/update-json.js";
44
import { readFile, writeFile } from "node:fs/promises";
55
import { basename, dirname } from "node:path";
6-
import { env } from "node:process";
6+
import process from "node:process";
77
import AdmZip from "adm-zip";
88
import chalk from "chalk";
99
import { toMerged } from "es-toolkit";
@@ -19,7 +19,7 @@ export default class Build extends Base {
1919
private isPreRelease: boolean;
2020
constructor(ctx: Context) {
2121
super(ctx);
22-
env.NODE_ENV ??= "production";
22+
process.env.NODE_ENV ??= "production";
2323
this.buildTime = "";
2424
this.isPreRelease = this.ctx.version.includes("-");
2525
}
@@ -33,7 +33,7 @@ export default class Build extends Base {
3333
const t = new Date();
3434
this.buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t);
3535
this.logger.info(
36-
`Building version ${chalk.blue(version)} to ${chalk.blue(dist)} at ${chalk.blue(this.buildTime)} in ${chalk.blue(env.NODE_ENV)} mode.`,
36+
`Building version ${chalk.blue(version)} to ${chalk.blue(dist)} at ${chalk.blue(this.buildTime)} in ${chalk.blue(process.env.NODE_ENV)} mode.`,
3737
);
3838
await this.ctx.hooks.callHook("build:init", this.ctx);
3939

@@ -58,7 +58,7 @@ export default class Build extends Base {
5858

5959
/** ======== build resolved =========== */
6060

61-
if (env.NODE_ENV === "production") {
61+
if (process.env.NODE_ENV === "production") {
6262
this.logger.tip("Packing plugin");
6363
await this.pack();
6464
await this.ctx.hooks.callHook("build:pack", this.ctx);

src/core/releaser/github.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Context } from "../../types/index.js";
22
import { readFile, stat } from "node:fs/promises";
33
import { basename, join } from "node:path";
4-
import { env } from "node:process";
4+
import process from "node:process";
55
import mime from "mime";
66
import { Octokit } from "octokit";
77
import { glob } from "tinyglobby";
@@ -172,10 +172,10 @@ export default class GitHub extends ReleaseBase {
172172
}
173173

174174
getClient(): Octokit {
175-
if (!env.GITHUB_TOKEN)
175+
if (!process.env.GITHUB_TOKEN)
176176
throw new Error("No GITHUB_TOKEN.");
177177
const client = new Octokit({
178-
auth: env.GITHUB_TOKEN,
178+
auth: process.env.GITHUB_TOKEN,
179179
userAgent: "zotero-plugin-scaffold",
180180
});
181181

src/core/server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Context } from "../types/index.js";
22
import { existsSync } from "node:fs";
33
import { join } from "node:path";
4-
import process, { env, exit } from "node:process";
4+
import process from "node:process";
55
import chokidar from "chokidar";
66
import { debounce } from "es-toolkit";
77
import { ZoteroRunner } from "../utils/zotero-runner.js";
@@ -118,14 +118,14 @@ export default class Serve extends Base {
118118

119119
private onZoteroExit = (_code?: number | null, _signal?: any) => {
120120
this.logger.info(`Zotero terminated.`);
121-
exit();
121+
process.exit();
122122
};
123123

124124
get zoteroBinPath() {
125125
if (this._zoteroBinPath)
126126
return this._zoteroBinPath;
127127

128-
this._zoteroBinPath = env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH;
128+
this._zoteroBinPath = process.env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH;
129129
if (!this._zoteroBinPath || !existsSync(this._zoteroBinPath))
130130
throw new Error("The Zotero binary not found.");
131131

@@ -136,14 +136,14 @@ export default class Serve extends Base {
136136
if (this._profilePath)
137137
return this._profilePath;
138138

139-
this._profilePath = env.ZOTERO_PLUGIN_PROFILE_PATH;
139+
this._profilePath = process.env.ZOTERO_PLUGIN_PROFILE_PATH;
140140
if (!this._profilePath || !existsSync(this._profilePath))
141141
throw new Error("The Zotero profile not found.");
142142

143143
return this._profilePath;
144144
}
145145

146146
get dataDir() {
147-
return env.ZOTERO_PLUGIN_DATA_DIR ?? "";
147+
return process.env.ZOTERO_PLUGIN_DATA_DIR ?? "";
148148
}
149149
}

src/core/tester.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Context } from "../types/index.js";
22
import http from "node:http";
33
import { join, resolve } from "node:path";
4-
import process, { cwd, env, exit } from "node:process";
4+
import process, { cwd } from "node:process";
55
import { build } from "esbuild";
66
import { copy, emptyDir, outputFile, outputJSON, pathExists } from "fs-extra/esm";
77
import { isCI } from "std-env";
@@ -22,7 +22,7 @@ export default class Test extends Base {
2222

2323
constructor(ctx: Context) {
2424
super(ctx);
25-
env.NODE_ENV ??= "test";
25+
process.env.NODE_ENV ??= "test";
2626

2727
this.builder = new Build(ctx);
2828

@@ -552,7 +552,7 @@ export default class Test extends Base {
552552
await installZoteroLinux();
553553

554554
// Set Environment Variable for Zotero Bin Path
555-
env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH = `${cwd()}/Zotero_linux-x86_64/zotero`;
555+
process.env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH = `${cwd()}/Zotero_linux-x86_64/zotero`;
556556

557557
const xvfb = new Xvfb({
558558
timeout: 2000,
@@ -589,22 +589,22 @@ export default class Test extends Base {
589589

590590
if (code === 1) {
591591
this.logger.error("Test run failed");
592-
exit(1);
592+
process.exit(1);
593593
}
594594
else if (code === "SIGINT") {
595595
this.logger.info("Tester shutdown by user request");
596-
exit();
596+
process.exit();
597597
}
598598
else {
599599
this.logger.success("Test run completed successfully");
600-
exit();
600+
process.exit();
601601
}
602602
};
603603

604604
private get zoteroBinPath() {
605-
if (!env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH)
605+
if (!process.env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH)
606606
throw new Error("No Zotero Found.");
607-
return env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH;
607+
return process.env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH;
608608
}
609609

610610
private get profilePath() {

src/utils/headless.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { execSync } from "node:child_process";
2-
import { exit } from "node:process";
2+
import process from "node:process";
33
import { isLinux } from "std-env";
44
import { logger } from "./log.js";
55

66
export async function installXvfb() {
77
logger.debug("Installing xvfb...");
88
if (!isLinux) {
99
logger.error("Unsupported platform. Please install Xvfb manually.");
10-
exit(1);
10+
process.exit(1);
1111
}
1212
try {
1313
execSync("which xvfb", { stdio: "ignore" });
@@ -30,7 +30,7 @@ export async function installXvfb() {
3030
}
3131
catch (error) {
3232
logger.error("Failed to install Xvfb:", error);
33-
exit(1);
33+
process.exit(1);
3434
}
3535
}
3636
}

src/utils/log.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import process, { env } from "node:process";
1+
import process from "node:process";
22
import readline from "node:readline";
33
import chalk from "chalk";
44
import { isPlainObject } from "es-toolkit";
@@ -26,8 +26,8 @@ export class Log {
2626
constructor(level?: LOG_LEVEL) {
2727
if (isDebug)
2828
this.logLevel = LOG_LEVEL.trace;
29-
else if (env.ZOTERO_PLUGIN_LOG_LEVEL)
30-
this.logLevel = LOG_LEVEL[env.ZOTERO_PLUGIN_LOG_LEVEL as LogLevelType];
29+
else if (process.env.ZOTERO_PLUGIN_LOG_LEVEL)
30+
this.logLevel = LOG_LEVEL[process.env.ZOTERO_PLUGIN_LOG_LEVEL as LogLevelType];
3131
else if (level)
3232
this.logLevel = level;
3333
else

src/utils/process.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { execSync } from "node:child_process";
2-
import { platform } from "node:process";
2+
import process from "node:process";
33

44
export function isRunning(query: string) {
55
let cmd = "";
6-
switch (platform) {
6+
switch (process.platform) {
77
case "win32":
88
cmd = `tasklist`;
99
break;
@@ -27,3 +27,12 @@ export function isRunning(query: string) {
2727
}
2828

2929
export const ExitSignals = ["exit", "SIGABRT", "SIGALRM", "SIGHUP", "SIGINT", "SIGTERM"];
30+
31+
export function whenExit(cb: (_code: number, _signal: string) => any) {
32+
ExitSignals.forEach((signal) => {
33+
process.once(
34+
signal,
35+
(_code, _signal) => cb(_code, _signal),
36+
);
37+
});
38+
}

src/utils/zotero-runner.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ChildProcessWithoutNullStreams } from "node:child_process";
22
import { execSync, spawn } from "node:child_process";
33
import { readFile } from "node:fs/promises";
44
import { join, resolve } from "node:path";
5-
import { env } from "node:process";
5+
import process from "node:process";
66
import { delay } from "es-toolkit";
77
import { outputFile, outputJSON, pathExists, readJSON, remove } from "fs-extra/esm";
88
import { isLinux, isMacOS, isWindows } from "std-env";
@@ -117,19 +117,15 @@ export class ZoteroRunner {
117117
const remotePort = await findFreeTcpPort();
118118
args.push("-start-debugger-server", String(remotePort));
119119

120-
const defaultFirefoxEnv = {
120+
const env = {
121+
...process.env,
121122
XPCOM_DEBUG_BREAK: "stack",
122123
NS_TRACE_MALLOC_DISABLE_STACKS: "1",
123124
};
124125

125126
// Using `spawn` so we can stream logging as they come in, rather than
126127
// buffer them up until the end, which can easily hit the max buffer size.
127-
this.zotero = spawn(this.options.binaryPath, args, {
128-
env: {
129-
...env,
130-
...defaultFirefoxEnv,
131-
},
132-
});
128+
this.zotero = spawn(this.options.binaryPath, args, { env });
133129

134130
// Handle Zotero log, necessary on macOS
135131
this.zotero.stdout?.on("data", (_data) => {});
@@ -287,8 +283,8 @@ export class ZoteroRunner {
287283
export function killZotero() {
288284
function kill() {
289285
try {
290-
if (env.ZOTERO_PLUGIN_KILL_COMMAND) {
291-
execSync(env.ZOTERO_PLUGIN_KILL_COMMAND);
286+
if (process.env.ZOTERO_PLUGIN_KILL_COMMAND) {
287+
execSync(process.env.ZOTERO_PLUGIN_KILL_COMMAND);
292288
}
293289
else if (isWindows) {
294290
execSync("taskkill /f /im zotero.exe");

0 commit comments

Comments
 (0)