diff --git a/src/core/config/config-editor.ts b/src/core/config/config-editor.ts index 39ba742..7d348be 100644 --- a/src/core/config/config-editor.ts +++ b/src/core/config/config-editor.ts @@ -1,3 +1,5 @@ +import * as os from 'node:os' +import * as path from 'node:path' import * as clack from '@clack/prompts' import type { Config, ConfigManager } from './config.js' @@ -180,11 +182,11 @@ async function ensureDiscordPlugin(): Promise { return null } try { - const { installNpmPlugin } = await import('../plugin/plugin-installer.js') console.log(dim(`Installing ${DISCORD_PACKAGE}...`)) - await installNpmPlugin(DISCORD_PACKAGE) + const { installNpmPlugin } = await import('../plugin/plugin-installer.js') + const mod = await installNpmPlugin(DISCORD_PACKAGE) console.log(ok(`${DISCORD_PACKAGE} installed`)) - return await import(DISCORD_PACKAGE) + return mod } catch (err) { console.log(warn(`Failed to install: ${(err as Error).message}`)) return null @@ -200,8 +202,6 @@ async function editDiscord(_config: Config, _updates: ConfigUpdates): Promise { - const pluginsDir = path.join(os.homedir(), '.openacp', 'plugins') - await execAsync(`npm install ${packageName}`, { cwd: pluginsDir }) +export async function installNpmPlugin(packageName: string, pluginsDir?: string): Promise { + // Try import first — already installed + try { + return await import(packageName) + } catch { + // Not installed, proceed with install + } + + const dir = pluginsDir ?? path.join(os.homedir(), '.openacp', 'plugins') + + await execAsync(`npm install ${packageName} --prefix "${dir}" --save`, { + timeout: 60000, + }) + + return await import(packageName) } diff --git a/src/core/setup/wizard.ts b/src/core/setup/wizard.ts index 199b41d..43f3979 100644 --- a/src/core/setup/wizard.ts +++ b/src/core/setup/wizard.ts @@ -191,8 +191,9 @@ async function installAndSetupDiscord( spinner.start(`Installing ${packageName}...`); try { const { installNpmPlugin } = await import('../plugin/plugin-installer.js'); - await installNpmPlugin(packageName); - discordPlugin = (await import(packageName)).default; + const pluginsDir = settingsManager.getBasePath(); + const mod = await installNpmPlugin(packageName, pluginsDir); + discordPlugin = mod.default; spinner.stop(ok(`${packageName} installed`)); } catch (installErr) { spinner.stop(fail(`Failed to install ${packageName}: ${(installErr as Error).message}`));