From b32c6c8defd750dc1252eb448fe6b5435be482b1 Mon Sep 17 00:00:00 2001 From: 0xmrpeter <0xmrpeter@gmail.com> Date: Fri, 27 Mar 2026 16:04:08 +0700 Subject: [PATCH] fix: replace execSync npm install with async utility Extract installNpmPlugin() into a shared utility that uses async exec instead of execSync, preventing event loop blocking. Used by both config-editor.ts and wizard.ts instead of inline execSync calls. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/core/config/config-editor.ts | 7 +++---- src/core/plugin/plugin-installer.ts | 27 +++++++++++++++++++++++++++ src/core/setup/wizard.ts | 12 ++++-------- 3 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 src/core/plugin/plugin-installer.ts diff --git a/src/core/config/config-editor.ts b/src/core/config/config-editor.ts index 5fa2c1f..7d348be 100644 --- a/src/core/config/config-editor.ts +++ b/src/core/config/config-editor.ts @@ -1,4 +1,3 @@ -import { execSync } from 'node:child_process' import * as os from 'node:os' import * as path from 'node:path' import * as clack from '@clack/prompts' @@ -183,11 +182,11 @@ async function ensureDiscordPlugin(): Promise { return null } try { - const pluginsDir = path.join(os.homedir(), '.openacp', 'plugins') console.log(dim(`Installing ${DISCORD_PACKAGE}...`)) - execSync(`npm install ${DISCORD_PACKAGE}`, { cwd: pluginsDir, stdio: 'pipe' }) + 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 diff --git a/src/core/plugin/plugin-installer.ts b/src/core/plugin/plugin-installer.ts new file mode 100644 index 0000000..c73c406 --- /dev/null +++ b/src/core/plugin/plugin-installer.ts @@ -0,0 +1,27 @@ +import { exec } from 'node:child_process' +import { promisify } from 'node:util' +import * as os from 'node:os' +import * as path from 'node:path' + +const execAsync = promisify(exec) + +/** + * Install an npm package to the plugins directory and return the loaded module. + * Tries to import first; if not installed, runs npm install asynchronously. + */ +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 ddeabcb..43f3979 100644 --- a/src/core/setup/wizard.ts +++ b/src/core/setup/wizard.ts @@ -1,5 +1,3 @@ -import { execSync } from "node:child_process"; -import * as path from "node:path"; import * as clack from "@clack/prompts"; import type { Config, ConfigManager } from "../config/config.js"; import type { ChannelId } from "./types.js"; @@ -192,12 +190,10 @@ async function installAndSetupDiscord( const spinner = clack.spinner(); spinner.start(`Installing ${packageName}...`); try { - const pluginsDir = path.join(settingsManager.getBasePath(), 'plugins'); - execSync(`npm install ${packageName}`, { - cwd: pluginsDir, - stdio: 'pipe', - }); - discordPlugin = (await import(packageName)).default; + const { installNpmPlugin } = await import('../plugin/plugin-installer.js'); + 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}`));