




















@@ -2,43 +2,70 @@ import { spawnSync } from "node:child_process";
22import { existsSync } from "node:fs";
33import path from "node:path";
44import { fileURLToPath } from "node:url";
5+import { resolvePnpmRunner } from "./pnpm-runner.mjs";
5667const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
78const opusDir = path.join(root, "node_modules", "@discordjs", "opus");
899-if (!existsSync(path.join(opusDir, "package.json"))) {
10-console.error(
11-"Missing node_modules/@discordjs/opus. Run pnpm install first, then retry this opt-in installer.",
12-);
13-process.exit(1);
10+export function resolveNativeOpusInstallCommand(params = {}) {
11+return resolvePnpmRunner({
12+comSpec: params.comSpec,
13+nodeExecPath: params.nodeExecPath,
14+npmExecPath: params.npmExecPath,
15+platform: params.platform,
16+pnpmArgs: [
17+"--dir",
18+params.opusDir,
19+"exec",
20+"node-pre-gyp",
21+"install",
22+"--fallback-to-build",
23+],
24+});
1425}
152616-const install = spawnSync(
17-"pnpm",
18-["--dir", opusDir, "exec", "node-pre-gyp", "install", "--fallback-to-build"],
19-{
27+function isDirectRun(metaUrl = import.meta.url, argvPath = process.argv[1]) {
28+return Boolean(argvPath) && path.resolve(argvPath) === fileURLToPath(metaUrl);
29+}
30+31+export function main() {
32+if (!existsSync(path.join(opusDir, "package.json"))) {
33+console.error(
34+"Missing node_modules/@discordjs/opus. Run pnpm install first, then retry this opt-in installer.",
35+);
36+process.exit(1);
37+}
38+39+const install = resolveNativeOpusInstallCommand({ opusDir });
40+const installResult = spawnSync(install.command, install.args, {
41+cwd: root,
42+env: install.env ?? process.env,
43+stdio: "inherit",
44+shell: install.shell,
45+windowsVerbatimArguments: install.windowsVerbatimArguments,
46+});
47+48+if (installResult.status !== 0) {
49+console.error(
50+"Failed to install @discordjs/opus for the active Node runtime. Use Node 22 for the upstream macOS arm64 prebuild, or install a node-gyp toolchain for source builds.",
51+);
52+process.exit(installResult.status ?? 1);
53+}
54+55+const verify = spawnSync(process.execPath, ["-e", 'require("@discordjs/opus")'], {
2056cwd: root,
2157env: process.env,
2258stdio: "inherit",
23-},
24-);
25-26-if (install.status !== 0) {
27-console.error(
28-"Failed to install @discordjs/opus for the active Node runtime. Use Node 22 for the upstream macOS arm64 prebuild, or install a node-gyp toolchain for source builds.",
29-);
30-process.exit(install.status ?? 1);
31-}
59+});
326033-const verify = spawnSync(process.execPath, ["-e", 'require("@discordjs/opus")'], {
34-cwd: root,
35-env: process.env,
36-stdio: "inherit",
37-});
61+if (verify.status !== 0) {
62+console.error("@discordjs/opus installed, but the active Node runtime still cannot load it.");
63+process.exit(verify.status ?? 1);
64+}
386539-if (verify.status !== 0) {
40-console.error("@discordjs/opus installed, but the active Node runtime still cannot load it.");
41-process.exit(verify.status ?? 1);
66+console.log("native opus ok");
4267}
436844-console.log("native opus ok");
69+if (isDirectRun()) {
70+main();
71+}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。