UNPKG

@pompeii-labs/cli

Version:

Magma CLI

83 lines (82 loc) 3.01 kB
import chalk from "chalk"; import path from "path"; import { getOrg, isAuthenticated } from "../auth.js"; import { getAgent } from "../api.js"; import fs from "fs"; import { execSync } from "child_process"; async function cloneAgent(agentName, options) { try { if (!isAuthenticated()) { console.log(chalk.red("Not authenticated with Magma. Please run `magma login`.")); process.exit(1); } if (!agentName) { console.log( chalk.red( "Please provide an agent name to clone. Example: `magma clone calendar-agent`" ) ); process.exit(1); } if (!options.directory) { options.directory = agentName; } const org = await getOrg(); if (!org) { console.error( chalk.red( "No organization found. Please run `magma switch` to select an organization." ) ); process.exit(1); } console.log(chalk.dim("Starting clone process...")); const agent = await getAgent(agentName, org.id); if (!fs.existsSync(options.directory)) { fs.mkdirSync(options.directory, { recursive: true }); } if (!fs.existsSync(path.join(options.directory, ".magma"))) { fs.mkdirSync(path.join(options.directory, ".magma"), { recursive: true }); } fs.writeFileSync( path.join(options.directory, ".magma", ".cache"), JSON.stringify({ id: agent.id, name: agent.name, version: agent.version.version }) ); console.log(chalk.dim("Copying agent source files...")); for (const file of agent.version.files) { const filePath = path.join(options.directory, file.path); const dirPath = path.dirname(filePath); fs.mkdirSync(dirPath, { recursive: true }); fs.writeFileSync(filePath, file.content); } console.log(chalk.dim("Installing dependencies...")); const installCommand = `npm install`; execSync(installCommand, { cwd: options.directory }); console.log(chalk.green("\n\u{1F517} Agent cloned successfully!\n")); console.log(chalk.dim(`Next steps:`)); console.log(chalk.dim(`1. ${chalk.bold(`cd ${options.directory}`)}`)); console.log(chalk.dim(`2. ${chalk.bold("magma run")} to run the agent locally `)); process.exit(0); } catch (error) { console.log(error); console.error(chalk.red("\n\u2716 Failed to clone agent:"), error.message); process.exit(1); } } function cloneCommand(program) { program.command("clone").description("Clone a Magma agent repo into a local directory").argument("<agent>", "The name of the agent to clone").argument( "[directory]", "The directory to clone the agent into (default: new directory named after the agent)" ).argument("[version]", "The version of the agent to clone (default: latest)").addHelpText( "after", ` Example: magma clone ${chalk.bold("calendar-agent")} ${chalk.bold("./")} ` ).action(async (agentName, directory, version) => { await cloneAgent(agentName, { directory, version }); }); } export { cloneCommand };