builtin-provider-config.mjs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { loadEndpointEnv } from "./load-endpoint-env.mjs";
  2. import { mkdir, readFile, writeFile } from "node:fs/promises";
  3. import { resolve } from "node:path";
  4. import { parseEnv } from "node:util";
  5. import { pathToFileURL } from "node:url";
  6. import { tsImport } from "tsx/esm/api";
  7. const repositoryRoot = resolve(import.meta.dirname, "..");
  8. /** @param {{root?: string, env?: Record<string, string | undefined>}} options */
  9. export async function resolveBuiltinProviderBuildEnvironment({
  10. root = repositoryRoot,
  11. env = process.env,
  12. } = {}) {
  13. let value = env.ZCODE_ENV;
  14. if (!value?.trim()) {
  15. const files = [
  16. ".env",
  17. ...(env.NODE_ENV === "production"
  18. ? [".env.production"]
  19. : [".env.development", ".env.development.local"]),
  20. ];
  21. for (const file of files) {
  22. let content;
  23. try {
  24. content = await readFile(resolve(root, file), "utf8");
  25. } catch (error) {
  26. if (error.code === "ENOENT") continue;
  27. throw error;
  28. }
  29. const parsed = parseEnv(content);
  30. if (parsed.ZCODE_ENV !== undefined) value = parsed.ZCODE_ENV;
  31. }
  32. }
  33. const normalized = value?.trim().toLowerCase() || "test";
  34. if (normalized !== "test" && normalized !== "production") {
  35. throw new Error(`Invalid ZCODE_ENV for Built-in Provider build: ${normalized}`);
  36. }
  37. return normalized;
  38. }
  39. /** @param {{root?: string, env?: Record<string, string | undefined>}} options */
  40. export async function loadBuiltinProviderConfig({ root = repositoryRoot, env = process.env } = {}) {
  41. env = await loadEndpointEnv({ root, env });
  42. const environment = await resolveBuiltinProviderBuildEnvironment({ root, env });
  43. const sourcePath = resolve(
  44. root,
  45. env.ZCODE_BUILTIN_PROVIDER_CONFIG_FILE?.trim() || "config/provider/zcode-builtin.json",
  46. );
  47. try {
  48. const content = await readFile(sourcePath, "utf8");
  49. // 构建期复用运行时的完整 Release 校验,避免打包成功后才发现 Schema 不兼容。
  50. // tsx 仅供构建工具加载仓库 TS,不进入产品 bundle,也不复制一份校验规则。
  51. // Windows 绝对路径的盘符会被 ESM 当作协议,转为 file URL 后各平台共用同一加载入口。
  52. const { decodeZCodeBuiltinRelease } = await tsImport(
  53. pathToFileURL(resolve(repositoryRoot, "packages/provider-node/src/zcode-builtin-release.ts"))
  54. .href,
  55. import.meta.url,
  56. );
  57. decodeZCodeBuiltinRelease(JSON.parse(content));
  58. return { environment, sourcePath, content };
  59. } catch (error) {
  60. throw new Error(`Invalid Built-in Provider config (${environment}): ${sourcePath}`, {
  61. cause: error,
  62. });
  63. }
  64. }
  65. /** @param {{directory: string, root?: string, env?: Record<string, string | undefined>}} options */
  66. export async function stageBuiltinProviderConfig({ directory, ...options }) {
  67. const config = await loadBuiltinProviderConfig(options);
  68. await mkdir(directory, { recursive: true });
  69. // bootstrap 可以复用 JS,但不能连带复用上一环境/上一版本的独立配置资源。
  70. await writeFile(resolve(directory, "zcode-builtin.json"), config.content, "utf8");
  71. return config;
  72. }