UNPKG

@botpress/adk-cli

Version:

Command-line interface for the Botpress Agent Development Kit (ADK)

71 lines (68 loc) 2.56 kB
// @bun import { DependencyError, dependencyStateSchema, sortKeysDeep } from "./chunk-5zm2mgt9.js"; // src/utils/dependency-state-file.ts import fs from "fs/promises"; import path from "path"; var DEPENDENCY_SNAPSHOT_SECURITY_NOTICE = "Dependency snapshots include all integration and plugin configuration, including values that may be secrets. Only share exported dependency snapshots with trusted recipients."; function defaultDependencySnapshotFileName(projectName, env) { const safeName = projectName.trim().replace(/[^a-zA-Z0-9._-]+/g, "-").replace(/^-+|-+$/g, ""); return `${safeName || "agent"}.dependencies.${env}.json`; } function dependencySnapshotPath(projectPath, env) { return path.join(projectPath, ".adk", "dependencies", `${env}.json`); } function clearDependencyConfig(state) { const integrations = {}; for (const [alias, entry] of Object.entries(state.integrations)) { integrations[alias] = { ...entry, config: {} }; } const plugins = {}; for (const [alias, entry] of Object.entries(state.plugins)) { plugins[alias] = { ...entry, config: {} }; } return dependencyStateSchema.parse({ ...state, integrations, plugins }); } async function readDependencyStateFile(filePath) { let raw; try { raw = await fs.readFile(filePath, "utf8"); } catch (error) { throw new DependencyError({ code: "INVALID_CONFIG", message: `Failed to read dependency snapshot at ${filePath}: ${error instanceof Error ? error.message : String(error)}` }); } let parsedJson; try { parsedJson = JSON.parse(raw); } catch (error) { throw new DependencyError({ code: "INVALID_CONFIG", message: `Dependency snapshot at ${filePath} is not valid JSON: ${error instanceof Error ? error.message : String(error)}` }); } try { return dependencyStateSchema.parse(parsedJson); } catch (error) { throw new DependencyError({ code: "INVALID_CONFIG", message: `Dependency snapshot at ${filePath} failed schema validation`, details: { issues: error.issues ?? String(error) } }); } } async function writeDependencyStateFile(filePath, state) { const validated = dependencyStateSchema.parse(state); await fs.mkdir(path.dirname(filePath), { recursive: true }); await fs.writeFile(filePath, JSON.stringify(sortKeysDeep(validated), null, 2) + ` `, "utf8"); } export { DEPENDENCY_SNAPSHOT_SECURITY_NOTICE, defaultDependencySnapshotFileName, dependencySnapshotPath, clearDependencyConfig, readDependencyStateFile, writeDependencyStateFile };