项目文件夹

文件
wehub-resource-sync 070959e133
landing-page-staging / Deploy landing page to staging (push) Has been skipped
landing-page-ci / Validate landing page (push) Failing after 4s
visual-baseline / Capture visual baselines (push) Has been cancelled
bake-plugin-previews / Bake plugin previews (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:00:47 +08:00

133 行
4.6 KiB
TypeScript

import { readFile } from "node:fs/promises";
import type { ToolPackConfig } from "./config.js";
export const MAC_PREBUNDLED_APP_DIR_NAME = "prebundled";
export const MAC_PREBUNDLE_META_DIR_NAME = "prebundle-meta";
export const MAC_PREBUNDLED_PACKAGED_MAIN_RELATIVE_PATH = "app/prebundled/packaged-main.mjs";
export const MAC_PREBUNDLED_WEB_SIDECAR_RELATIVE_PATH = "app/prebundled/web-sidecar.mjs";
export const MAC_PREBUNDLED_DAEMON_CLI_RELATIVE_PATH = "app/prebundled/daemon/daemon-cli.mjs";
export const MAC_PREBUNDLED_DAEMON_SIDECAR_RELATIVE_PATH = "app/prebundled/daemon/daemon-sidecar.mjs";
export const MAC_PREBUNDLE_ESBUILD_TARGET = "node24";
export const MAC_DAEMON_PREBUNDLE_ESM_REQUIRE_BANNER =
'import { createRequire as __odCreateRequire } from "node:module"; const require = __odCreateRequire(import.meta.url);';
export const MAC_PREBUNDLE_ENTRYPOINTS_DIR_NAME = "prebundle-entrypoints";
export const MAC_PREBUNDLE_RUNTIME_DEPENDENCIES = {
"better-sqlite3": "12.9.0",
"blake3-wasm": "2.1.5",
} as const;
export const MAC_STANDALONE_PREBUNDLE_EXCLUDED_INTERNAL_PACKAGES = [
"@open-design/daemon",
"@open-design/desktop",
"@open-design/launcher-proto",
"@open-design/packaged",
"@open-design/sidecar",
"@open-design/sidecar-proto",
"@open-design/web",
] as const;
export const MAC_PREBUNDLE_POLICIES = {
packagedMain: {
externals: ["electron"],
forbiddenInputs: [
"/apps/web/",
"/node_modules/@open-design/web/",
"/node_modules/next/",
"/node_modules/openai/",
"/node_modules/react/",
"/node_modules/react-dom/",
],
label: "packaged main",
},
daemonCli: {
externals: ["better-sqlite3", "blake3-wasm"],
forbiddenInputs: [
"/node_modules/@open-design/daemon/",
"/node_modules/better-sqlite3/",
"/node_modules/blake3-wasm/",
"/node_modules/electron/",
"/node_modules/next/",
"/node_modules/openai/",
"/node_modules/react/",
"/node_modules/react-dom/",
],
label: "daemon cli",
},
daemonSidecar: {
externals: ["better-sqlite3", "blake3-wasm"],
forbiddenInputs: [
"/node_modules/@open-design/daemon/",
"/node_modules/better-sqlite3/",
"/node_modules/blake3-wasm/",
"/node_modules/electron/",
"/node_modules/next/",
"/node_modules/openai/",
"/node_modules/react/",
"/node_modules/react-dom/",
],
label: "daemon sidecar",
},
webSidecar: {
externals: [],
forbiddenInputs: [
"/node_modules/next/",
"/node_modules/openai/",
"/node_modules/react/",
"/node_modules/react-dom/",
],
label: "web sidecar",
},
} as const;
export type MacPrebundlePolicyName = keyof typeof MAC_PREBUNDLE_POLICIES;
function toPosixPath(value: string): string {
return value.replaceAll("\\", "/");
}
export function shouldUseMacStandalonePrebundle(webOutputMode: ToolPackConfig["webOutputMode"]): boolean {
return webOutputMode === "standalone";
}
export function shouldInstallInternalPackageForMacPrebundle(options: {
packageName: string;
webOutputMode: ToolPackConfig["webOutputMode"];
}): boolean {
if (!shouldUseMacStandalonePrebundle(options.webOutputMode)) return true;
return !MAC_STANDALONE_PREBUNDLE_EXCLUDED_INTERNAL_PACKAGES.includes(
options.packageName as (typeof MAC_STANDALONE_PREBUNDLE_EXCLUDED_INTERNAL_PACKAGES)[number],
);
}
export function findForbiddenMacPrebundleInputs(options: {
forbiddenInputs: readonly string[];
inputs: readonly string[];
}): string[] {
return options.inputs
.map(toPosixPath)
.filter((input) => options.forbiddenInputs.some((forbidden) => input.includes(forbidden)));
}
export async function assertMacPrebundleMetafile(options: {
metafilePath: string;
policyName: MacPrebundlePolicyName;
}): Promise<void> {
const policy = MAC_PREBUNDLE_POLICIES[options.policyName];
const metafile = JSON.parse(await readFile(options.metafilePath, "utf8")) as { inputs?: Record<string, unknown> };
const matched = findForbiddenMacPrebundleInputs({
forbiddenInputs: policy.forbiddenInputs,
inputs: Object.keys(metafile.inputs ?? {}),
});
if (matched.length > 0) {
throw new Error(`${policy.label} prebundle included forbidden inputs: ${matched.join(", ")}`);
}
}
export function renderMacPackagedMainEntry(usePrebundle: boolean): string {
return usePrebundle
? 'import("./prebundled/packaged-main.mjs").catch((error) => {\n console.error("packaged entry failed", error);\n process.exit(1);\n});\n'
: 'import("@open-design/packaged").catch((error) => {\n console.error("packaged entry failed", error);\n process.exit(1);\n});\n';
}