@pompeii-labs/cli
Version:
Magma CLI
96 lines (95 loc) • 2.97 kB
JavaScript
import chalk from "chalk";
import path from "path";
import fs from "fs";
import { deployAgent } from "../api.js";
import boxen from "boxen";
import { UI } from "../ui.js";
import { getOrg, isAuthenticated } from "../auth.js";
import { Bundler } from "../bundler.js";
import { exec } from "child_process";
import { promisify } from "util";
const execAsync = promisify(exec);
function showNextSteps(agent, orgId) {
console.log(chalk.dim("\nNext steps:"));
console.log(chalk.cyan.bold("\n Dashboard"));
console.log(" View your agent directly in the dashboard");
console.log(
`
${chalk.dim(`https://magmadeploy.com/${orgId}/agents/${chalk.cyan(agent.id)}`)}`
);
console.log(chalk.cyan.bold("\n WebSocket"));
console.log(
" Head to the dashboard to create an API key, and use the WebSocket endpoint to connect to your agent"
);
console.log(
`
${chalk.dim(
`wss://api.magmadeploy.com?agentId=${chalk.cyan(agent.id)}&apiKey=${chalk.cyan(
"magma_XXXXX"
)}`
)}`
);
}
async function deploy(options) {
const projectDir = path.resolve(options.project);
const org = await getOrg();
if (!org) {
console.error(
chalk.red("No organization found. Please run `magma switch` to select an organization.")
);
process.exit(1);
}
const packageJson = JSON.parse(fs.readFileSync(path.join(projectDir, "package.json"), "utf8"));
console.log(
boxen(chalk.bold(`Deploying ${chalk.cyan(packageJson.name)}`), {
padding: 1,
margin: 1,
borderStyle: "round",
borderColor: "cyan"
})
);
const buildSpinner = UI.spinner("Building agent").start();
try {
await execAsync(`bun run build`, { cwd: projectDir });
} catch (error) {
const errorOutput = error.stdout || error.stderr || error.message;
buildSpinner.fail();
console.log(errorOutput);
console.log(chalk.red("\nFailed to build agent"));
process.exit(1);
}
buildSpinner.succeed();
const bundler = new Bundler(projectDir, org);
const agent = await bundler.run();
const startupSpinner = UI.spinner("Deploying agent").start();
try {
await deployAgent(agent);
startupSpinner.succeed();
console.log(
chalk.cyan(`
\u{1F30B} Agent is ${chalk.green.bold("\u25CF live")} and ready for action!`)
);
showNextSteps(agent, org.id);
console.log();
} catch (error) {
startupSpinner.fail();
throw error;
}
}
function deployCommand(program) {
program.command("deploy").description("Deploy a Magma agent").option("-p, --project <path>", "Path to the project directory", ".").action(async (options) => {
if (!isAuthenticated()) {
console.log(chalk.red("Not authenticated with Magma. Please run `magma login`."));
process.exit(1);
}
try {
await deploy(options);
} catch (error) {
console.error(chalk.red("\n\u2716 Failed to deploy agent:"), error.message);
process.exit(1);
}
});
}
export {
deployCommand
};