#!/usr/bin/env bun import { execSync } from "node:child_process"; import { writeFileSync, unlinkSync } from "node:fs"; import { loadCorpConfigs } from "../src/core/config/loader.js"; import { defaultSealUrl } from "../src/core/auth/hose.js"; const projectId = process.env.APIFOX_PROJECT_ID || "8758920"; const corps = loadCorpConfigs(); console.log(`Loading configurations for ${corps.length} enterprises...`); // 1. Get existing environments from Apifox const envListRaw = execSync(`apifox environment list --project ${projectId}`, { encoding: "utf8" }); const envList = JSON.parse(envListRaw).data || []; const envMap = new Map(envList.map(e => [e.name, e])); for (const c of corps) { const envName = `${c.name} (${c.id})`; const sealUrl = defaultSealUrl(c, c.source); const hoseBaseUrl = c.source?.baseUrl || "https://app.ekuaibao.com"; const hoseApiKey = c.source?.key || c.source?.apiKey || ""; const hoseApiSecret = c.source?.secret || c.source?.apiSecret || ""; const hoseUserId = c.source?.uid || c.source?.userId || ""; const sourceType = c.source?.type || "hose"; const payload = { name: envName, baseUrl: sealUrl, baseUrls: { default: sealUrl }, parameters: { header: [ { name: "Authorization", value: "Bearer {{sealToken}}", enable: true, description: "Seal AI 租户会话 Token (Bearer)" } ], cookie: [], query: [], body: [] }, variables: [ { name: "corpId", value: c.id, type: "string", description: "合思/Seal 企业租户 ID" }, { name: "sourceType", value: sourceType, type: "string", description: "企业数据源类型 (hose / seal)" }, { name: "hoseBaseUrl", value: hoseBaseUrl, type: "string", description: "合思系统域名" }, { name: "hoseApiKey", value: hoseApiKey, type: "string", description: "合思 OpenAPI Key" }, { name: "hoseApiSecret", value: hoseApiSecret, type: "string", description: "合思 OpenAPI Secret" }, { name: "hoseUserId", value: hoseUserId, type: "string", description: "合思操作人员工 ID / StaffId" }, { name: "sealToken", value: "", type: "string", description: "Seal AI 当前有效会话 Token" } ] }; const tmpFile = `/tmp/env-${c.id}.json`; writeFileSync(tmpFile, JSON.stringify(payload, null, 2)); const existing = envMap.get(envName) || envList.find(e => e.name.includes(c.id)); let envId = existing?.id; if (!envId) { console.log(`Creating environment: ${envName}`); const createRes = execSync(`apifox environment create "${envName}" --project ${projectId} --base-url "${sealUrl}"`, { encoding: "utf8" }); const createData = JSON.parse(createRes).data; envId = createData.id; } console.log(`Updating environment variables & headers for: ${envName} (ID: ${envId})`); execSync(`apifox environment update ${envId} --project ${projectId} --file ${tmpFile}`, { encoding: "utf8" }); unlinkSync(tmpFile); } console.log("All enterprise environments successfully synced to Apifox!");