From 88dcefd93240a4a825a2bbddb1cfd8bfd9db2a29 Mon Sep 17 00:00:00 2001 From: HashWarlock Date: Wed, 12 Feb 2025 00:06:40 -0500 Subject: [PATCH] fix plugins installing during init and plugins add --- packages/cli/.env.example | 3 +- packages/cli/src/commands/init.ts | 22 ++++------ packages/cli/src/commands/plugins.ts | 6 +-- packages/cli/src/utils/install-plugin.ts | 53 ++++++++++++++++++++++++ packages/cli/src/utils/run-bun.ts | 5 +++ 5 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 packages/cli/src/utils/install-plugin.ts create mode 100644 packages/cli/src/utils/run-bun.ts diff --git a/packages/cli/.env.example b/packages/cli/.env.example index 9a27516c460..2f858fed6a5 100644 --- a/packages/cli/.env.example +++ b/packages/cli/.env.example @@ -1 +1,2 @@ -# No configuration needed for SQLite \ No newline at end of file +# No configuration needed for SQLite +ELIZA_BRANCH=v2-develop \ No newline at end of file diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index e92476094ca..0e44250f61c 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -5,6 +5,8 @@ import { handleError } from "@/src/utils/handle-error" import { logger } from "@/src/utils/logger" import { getAvailableDatabases, getRegistryIndex, listPluginsByType } from "@/src/utils/registry" import { createDatabaseTemplate, createPluginsTemplate, createEnvTemplate } from "@/src/utils/templates" +import { runBunCommand } from "@/src/utils/run-bun" +import { installPlugin } from "@/src/utils/install-plugin" import chalk from "chalk" import { Command } from "commander" import { execa } from "execa" @@ -18,7 +20,7 @@ const initOptionsSchema = z.object({ async function cloneStarterRepo(targetDir: string) { logger.info("Setting up project structure...") - await execa("git", ["clone", "-b", "develop", "https://github.com/elizaos/eliza", "."], { + await execa("git", ["clone", "-b", process.env.ELIZA_BRANCH || "v2-develop", "https://github.com/elizaos/eliza", "."], { cwd: targetDir, stdio: "inherit", }) @@ -74,22 +76,14 @@ async function installDependencies(targetDir: string, database: string, selected }) // Use bun for installation - await execa("bun", ["install", "--no-frozen-lockfile"], { - cwd: targetDir, - stdio: "inherit" - }) - - await execa("bun", ["add", `@elizaos/adapter-${database}`, "--workspace-root"], { - cwd: targetDir, - stdio: "inherit" - }) + await runBunCommand(["install", "--no-frozen-lockfile"], targetDir); + await runBunCommand(["add", `@elizaos/adapter-${database}`, "--workspace-root"], targetDir); if (selectedPlugins.length > 0) { console.log(selectedPlugins) - await execa("bun", ["add", ...selectedPlugins, "--workspace-root"], { - cwd: targetDir, - stdio: "inherit" - }) + for (const plugin of selectedPlugins) { + await installPlugin(plugin, targetDir) + } } } diff --git a/packages/cli/src/commands/plugins.ts b/packages/cli/src/commands/plugins.ts index 950404b26b4..f723abab563 100644 --- a/packages/cli/src/commands/plugins.ts +++ b/packages/cli/src/commands/plugins.ts @@ -4,6 +4,7 @@ import { logger } from "@/src/utils/logger" import { getPluginRepository, getRegistryIndex } from "@/src/utils/registry" import { Command } from "commander" import { execa } from "execa" +import { installPlugin } from "@/src/utils/install-plugin" export const plugins = new Command() .name("plugins") @@ -58,10 +59,7 @@ plugins // Install from GitHub logger.info(`Installing ${plugin}...`) - await execa("bun", ["add", repo], { - cwd, - stdio: "inherit" - }) + await installPlugin(repo, cwd) logger.success(`Successfully installed ${plugin}`) diff --git a/packages/cli/src/utils/install-plugin.ts b/packages/cli/src/utils/install-plugin.ts new file mode 100644 index 00000000000..6e0550aa017 --- /dev/null +++ b/packages/cli/src/utils/install-plugin.ts @@ -0,0 +1,53 @@ +import { execa } from "execa"; +import path from "node:path"; +import { logger } from "@/src/utils/logger"; +import { runBunCommand } from "@/src/utils/run-bun"; +import { promises as fs } from "fs"; + +export async function installPlugin( + pluginName: string, + cwd: string, +): Promise { + // Remove 'github:' or leading '@' prefix if present + const cleanedName = pluginName.replace(/^github:|^@/, ""); + let installed = false; + try { + //logger.info(`Attempting to install ${pluginName} using bun add...`); + //await runBunCommand(["add", `${pluginName}`], cwd); + //logger.success(`Successfully installed ${pluginName} via bun add.`); + //installed = true; + // Set the directory to clone into the packages folder (each plugin gets its own subfolder) + const cloneDir = path.join(cwd, "packages", cleanedName.replace(/\S+\//, "")); + logger.info(`Cloning ${pluginName} from https://github.com/${cleanedName}.git to ${cloneDir}`); + await execa("git",["clone", `https://github.com/${cleanedName}.git`, cloneDir],{ cwd, stdio: "inherit" }); + logger.success(`Successfully cloned repository for ${cleanedName}.`); + installed = true; + } catch (error: any) { + logger.warn( + `failed to install packages for ${cleanedName}, falling back: ${error.message}` + ); + } + + if (installed) { + // Try to read the package.json file from the cloned plugin repo + let pkgName = cleanedName; + const pkgPath = path.join(cwd, "packages", cleanedName.replace(/\S+\//, ""), "package.json"); + try { + const pkgContent = await fs.readFile(pkgPath, "utf-8"); + const pkg = JSON.parse(pkgContent); + if (pkg.name) { + pkgName = pkg.name; + logger.info(`Found package.json name: ${pkgName}`); + } + } catch (err: any) { + logger.warn(`Could not read package.json from ${pkgPath}: ${err.message}`); + } + + logger.info(`Adding ${pkgName} to workspace's agent package (packages/agent/package.json)...`); + await runBunCommand( + ["add", `${pkgName}@workspace:*`, "--filter", "./packages/agent"], + cwd + ); + logger.success(`Successfully added ${pkgName} to agent workspace.`); + } +} \ No newline at end of file diff --git a/packages/cli/src/utils/run-bun.ts b/packages/cli/src/utils/run-bun.ts new file mode 100644 index 00000000000..e0aa5c76bb8 --- /dev/null +++ b/packages/cli/src/utils/run-bun.ts @@ -0,0 +1,5 @@ +import { execa } from "execa"; + +export async function runBunCommand(args: string[], cwd: string): Promise { + await execa("bun", args, { cwd, stdio: "inherit" }); +} \ No newline at end of file