builtin-provider-config.mjs 3.0 KB

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