Skip to content

Commit

Permalink
fix plugins installing during init and plugins add
Browse files Browse the repository at this point in the history
  • Loading branch information
HashWarlock committed Feb 12, 2025
1 parent edcd6b3 commit 88dcefd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/cli/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# No configuration needed for SQLite
# No configuration needed for SQLite
ELIZA_BRANCH=v2-develop
22 changes: 8 additions & 14 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
})
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions packages/cli/src/commands/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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}`)

Expand Down
53 changes: 53 additions & 0 deletions packages/cli/src/utils/install-plugin.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
// 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.`);
}
}
5 changes: 5 additions & 0 deletions packages/cli/src/utils/run-bun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { execa } from "execa";

export async function runBunCommand(args: string[], cwd: string): Promise<void> {
await execa("bun", args, { cwd, stdio: "inherit" });
}

0 comments on commit 88dcefd

Please sign in to comment.