UNPKG

@botpress/adk-cli

Version:

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

149 lines (147 loc) 4.51 kB
// @bun import { dependencySnapshotPath, readDependencyStateFile } from "./chunk-2xth50pf.js"; import { confirmProdAndRetry, loadProjectDM, parseFormat, parseTarget, runDependencyCommand } from "./chunk-69myctr2.js"; import"./chunk-3k2q12qe.js"; import"./chunk-3ahwp6fe.js"; import { DependencyError } from "./chunk-5zm2mgt9.js"; import"./chunk-kk3h6qaj.js"; import"./chunk-gzwt1qdr.js"; import"./chunk-nxy2ya5r.js"; import"./chunk-wzj4dc7n.js"; import"./chunk-p0hjqn4r.js"; import"./chunk-np5wcwfv.js"; import"./chunk-dq2xpa24.js"; import"./chunk-6w0knnta.js"; import"./chunk-40x04ckt.js"; import"./chunk-t76d8fxx.js"; import"./chunk-nh2akp42.js"; import"./chunk-0fdvzjbh.js"; import"./chunk-2a5b6azq.js"; import"./chunk-vay209b5.js"; import"./chunk-3xrpxgq4.js"; import"./chunk-rfm3jr1m.js"; import"./chunk-w346ejn9.js"; import"./chunk-knvm2anf.js"; import"./chunk-65h5trb5.js"; import"./chunk-s2akeqpw.js"; import"./chunk-6771vrjp.js"; import"./chunk-g8mm42v1.js"; import"./chunk-50hzjdck.js"; import"./chunk-nn2jb0x0.js"; import"./chunk-v8xvth6j.js"; import"./chunk-kkk13rcb.js"; import"./chunk-ytpp1kam.js"; import"./chunk-na956zz3.js"; import"./chunk-f4bw8q7c.js"; import"./chunk-0v8vgrns.js"; import"./chunk-54qt5g7m.js"; import"./chunk-dhs2bg35.js"; // src/commands/dependencies/import.ts import fs from "fs/promises"; import path from "path"; async function adkDependenciesImport(input, options = {}) { await runDependencyCommand({ options, run: async (logger) => { const format = parseFormat(options.format); const inputPath = path.resolve(process.cwd(), input); const state = await readDependencyStateFile(inputPath); const target = parseImportTarget(options.target, state.env); const { dm, project } = await loadProjectDM(target); const localSnapshotPath = dependencySnapshotPath(project.path, target); const previousSnapshot = await readOptionalFile(localSnapshotPath); let result; try { const applyOptions = { dryRun: options.dryRun, yes: options.yes }; result = await confirmProdAndRetry(() => dm.applyState(state, applyOptions), () => dm.applyState(state, { ...applyOptions, yes: true }), logger); } catch (error) { await restoreOptionalFile(localSnapshotPath, previousSnapshot); throw error; } if (result.dryRun) { await restoreOptionalFile(localSnapshotPath, previousSnapshot); } if (format === "text") { printApplyResult(logger, inputPath, target, result); } if (format === "text" && result.errors.length > 0) { throw new DependencyError({ code: "INVALID_CONFIG", message: `Dependency import completed with errors`, details: { errors: result.errors } }); } if (format === "json") { return { extras: { input: inputPath, target, sourceEnv: state.env, dryRun: result.dryRun }, data: { result } }; } return {}; } }); } function parseImportTarget(raw, fallback) { if (!raw) { return fallback; } const parsed = parseTarget(raw); if (parsed === "all") { throw new DependencyError({ code: "MISSING_INPUT", message: `'all' is not valid for --target on dependency import. Use 'dev' or 'prod'.` }); } return parsed; } async function readOptionalFile(filePath) { try { return await fs.readFile(filePath, "utf8"); } catch (error) { if (error.code === "ENOENT") { return null; } throw error; } } async function restoreOptionalFile(filePath, contents) { if (contents === null) { await fs.rm(filePath, { force: true }); return; } await fs.mkdir(path.dirname(filePath), { recursive: true }); await fs.writeFile(filePath, contents, "utf8"); } function printApplyResult(logger, inputPath, target, result) { const verb = result.dryRun ? "Plan" : "Imported"; logger.info(`${verb} ${inputPath} -> ${target}:`); const actions = [...result.applied, ...result.skipped]; if (actions.length === 0 && result.errors.length === 0) { logger.info(" no changes"); } for (const action of actions) { logger.info(` ${action.action} ${action.type}:${action.alias}`); } for (const error of result.errors) { logger.info(` x ${error.action.action} ${error.action.type}:${error.action.alias} - ${error.code}: ${error.message}`, "red"); } } export { adkDependenciesImport };