|
| 1 | +#!/usr/bin/env node |
| 2 | +const { execSync } = require('child_process') |
| 3 | +const pathUtil = require('path') |
| 4 | +const fs = require('fs') |
| 5 | +const { Command } = require('commander') |
| 6 | + |
| 7 | +const program = new Command() |
| 8 | +const { version } = require('./package.json') |
| 9 | + |
| 10 | +program |
| 11 | + .name('elizaos') |
| 12 | + .description('elizaOS CLI - Manage your plugins') |
| 13 | + .version(version); |
| 14 | + |
| 15 | +const plugins = new Command() |
| 16 | + .name("plugins") |
| 17 | + .description("manage elizaOS plugins") |
| 18 | + |
| 19 | +async function getPlugins() { |
| 20 | + const resp = await fetch('https://raw.githubusercontent.com/elizaos-plugins/registry/refs/heads/main/index.json') |
| 21 | + return await resp.json(); |
| 22 | +} |
| 23 | + |
| 24 | +plugins |
| 25 | + .command("list") |
| 26 | + .description("list available plugins") |
| 27 | + .option("-t, --type <type>", "filter by type (adapter, client, plugin)") |
| 28 | + .action(async (opts) => { |
| 29 | + try { |
| 30 | + const plugins = await getPlugins() |
| 31 | + const pluginNames = Object.keys(plugins) |
| 32 | + .filter(name => !opts.type || name.includes(opts.type)) |
| 33 | + .sort() |
| 34 | + |
| 35 | + console.info("\nAvailable plugins:") |
| 36 | + for (const plugin of pluginNames) { |
| 37 | + console.info(` ${plugin}`) |
| 38 | + } |
| 39 | + console.info("") |
| 40 | + } catch (error) { |
| 41 | + console.error(error) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | +plugins |
| 46 | + .command("add") |
| 47 | + .description("add a plugin") |
| 48 | + .argument("<plugin>", "plugin name") |
| 49 | + .action(async (plugin, opts) => { |
| 50 | + // ensure git is installed |
| 51 | + try { |
| 52 | + const gitVersion = execSync('git --version', { stdio: 'pipe' }).toString().trim(); |
| 53 | + console.log('using', gitVersion) |
| 54 | + } catch(e) { |
| 55 | + console.error('Please install git to use this utility') |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + const plugins = await getPlugins() |
| 60 | + const repoData = plugins[plugin]?.split(':') |
| 61 | + if (!repoData) { |
| 62 | + console.error('Plugin', plugin, 'not found') |
| 63 | + return |
| 64 | + } |
| 65 | + //console.log('p', plugin, 'type', repoData[0], 'repo', repoData[1]) |
| 66 | + // repo type |
| 67 | + if (repoData[0] !== 'github') { |
| 68 | + console.error('Plugin', plugin, 'uses', repoData[0], ' but this utility only currently support github') |
| 69 | + return |
| 70 | + } |
| 71 | + const parts = repoData[1].split('/') |
| 72 | + const elizaOSroot = pathUtil.resolve(__dirname, '../..') |
| 73 | + const pkgPath = elizaOSroot + '/packages/' + parts[1] |
| 74 | + |
| 75 | + // can't add to char file |
| 76 | + if (!fs.existsSync(pkgPath)) { |
| 77 | + // clone it |
| 78 | + console.log('cloning', parts[1], 'to', pkgPath) |
| 79 | + const gitOutput = execSync('git clone https://github.com/' + repoData[1] + ' ' + pkgPath, { stdio: 'pipe' }).toString().trim(); |
| 80 | + } |
| 81 | + // add core to plugin |
| 82 | + // # pnpm add @elizaos/core@workspace:* --filter ./packages/client-twitter |
| 83 | + console.log('Making sure plugin has access to @elizaos/core') |
| 84 | + const pluginAddCoreOutput = execSync('pnpm add @elizaos/core@workspace:* --filter ./packages/' + parts[1], { cwd: elizaOSroot, stdio: 'pipe' }).toString().trim(); |
| 85 | + //console.log('pluginAddCoreOutput', pluginAddCoreOutput) |
| 86 | + |
| 87 | + // Read the current package.json |
| 88 | + const packageJsonPath = pkgPath + '/package.json' |
| 89 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 90 | + |
| 91 | + if (packageJson.name !== '@elizaos-plugins/' + parts[1]) { |
| 92 | + // Update the name field |
| 93 | + packageJson.name = '@elizaos-plugins/' + parts[1] |
| 94 | + console.log('Updating plugins package.json name to', packageJson.name) |
| 95 | + |
| 96 | + // Write the updated package.json back to disk |
| 97 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)) |
| 98 | + } |
| 99 | + |
| 100 | + //console.log('Update workspace') |
| 101 | + //const updateWorkspaceOutput = execSync('pnpm i --no-frozen-lockfile', { cwd: elizaOSroot, stdio: 'pipe' }).toString().trim(); |
| 102 | + |
| 103 | + // add to agent |
| 104 | + console.log('Adding plugin', plugin, 'to agent/package.json') |
| 105 | + try { |
| 106 | + const pluginAddAgentOutput = execSync('pnpm add ' + plugin + '@workspace:* --filter ./agent', { cwd: elizaOSroot, stdio: 'pipe' }).toString().trim(); |
| 107 | + //console.log('pluginAddAgentOutput', pluginAddAgentOutput) |
| 108 | + } catch (e) { |
| 109 | + console.error('error', e) |
| 110 | + } |
| 111 | + // rebuild |
| 112 | + |
| 113 | + console.log(plugin, 'attempted installation is complete') |
| 114 | + console.log('Remember to add it to your character file\'s plugin field: ["' + plugin + '"]') |
| 115 | + }) |
| 116 | + |
| 117 | +program.addCommand(plugins) |
| 118 | + |
| 119 | +program.parse(process.argv) |
0 commit comments