diff --git a/src/main/npm.ts b/src/main/npm.ts index 4c5b16eda5..7003693e4c 100644 --- a/src/main/npm.ts +++ b/src/main/npm.ts @@ -3,7 +3,7 @@ import * as path from 'node:path'; import { IpcMainInvokeEvent, shell } from 'electron'; import { ipcMainManager } from './ipc'; -import { exec } from './utils/exec'; +import { exec, execFile } from './utils/exec'; import { IPackageManager, PMOperationOptions } from '../interfaces'; import { IpcEvents } from '../ipc-events'; @@ -54,18 +54,15 @@ export async function addModules( { dir, packageManager }: PMOperationOptions, ...names: Array ): Promise { - let nameArgs: Array = []; - let installCommand: string; + const cmd = packageManager === 'npm' ? 'npm' : 'yarn'; + const args = + packageManager === 'npm' + ? ['install', '-S', ...names] + : names.length > 0 + ? ['add', ...names] + : ['install']; - if (packageManager === 'npm') { - installCommand = 'npm install'; - nameArgs = names.length > 0 ? ['-S', ...names] : ['--also=dev --prod']; - } else { - installCommand = names.length > 0 ? 'yarn add' : 'yarn install'; - nameArgs = [...names]; - } - - return exec(dir, [installCommand].concat(nameArgs).join(' ')); + return await execFile(dir, cmd, args); } /** diff --git a/src/main/utils/exec.ts b/src/main/utils/exec.ts index d4a957d8f4..f7191586d1 100644 --- a/src/main/utils/exec.ts +++ b/src/main/utils/exec.ts @@ -1,4 +1,4 @@ -import { exec as cp_exec } from 'node:child_process'; +import { exec as cp_exec, execFile as cp_execFile } from 'node:child_process'; import { promisify } from 'node:util'; import shellEnv from 'shell-env'; @@ -40,3 +40,21 @@ export async function exec(dir: string, cliArgs: string): Promise { return stdout.trim(); } + +/** + * Execute a command with arguments in a directory. + */ +export async function execFile( + dir: string, + cmd: string, + args: Array, +): Promise { + await maybeFixPath(); + + const { stdout } = await promisify(cp_execFile)(cmd, args, { + cwd: dir, + maxBuffer: 200 * 1024 * 100, + }); + + return stdout.trim(); +} diff --git a/tests/main/npm-spec.ts b/tests/main/npm-spec.ts index ec2becd015..1ff0b43ae4 100644 --- a/tests/main/npm-spec.ts +++ b/tests/main/npm-spec.ts @@ -5,7 +5,7 @@ import { getIsPackageManagerInstalled, packageRun, } from '../../src/main/npm'; -import { exec } from '../../src/main/utils/exec'; +import { exec, execFile } from '../../src/main/utils/exec'; import { overridePlatform, resetPlatform } from '../utils'; vi.mock('../../src/main/utils/exec'); @@ -127,19 +127,21 @@ describe('npm', () => { 'thing', ); - expect(exec).toHaveBeenCalledWith( - '/my/directory', - 'npm install -S say thing', - ); + expect(execFile).toHaveBeenCalledWith('/my/directory', 'npm', [ + 'install', + '-S', + 'say', + 'thing', + ]); }); - it('attempts to installs all modules', async () => { + it('attempts to install all modules', async () => { addModules({ dir: '/my/directory', packageManager: 'npm' }); - expect(exec).toHaveBeenCalledWith( - '/my/directory', - 'npm install --also=dev --prod', - ); + expect(execFile).toHaveBeenCalledWith('/my/directory', 'npm', [ + 'install', + '-S', + ]); }); }); @@ -151,16 +153,19 @@ describe('npm', () => { 'thing', ); - expect(exec).toHaveBeenCalledWith( - '/my/directory', - 'yarn add say thing', - ); + expect(execFile).toHaveBeenCalledWith('/my/directory', 'yarn', [ + 'add', + 'say', + 'thing', + ]); }); it('attempts to installs all modules', async () => { addModules({ dir: '/my/directory', packageManager: 'yarn' }); - expect(exec).toHaveBeenCalledWith('/my/directory', 'yarn install'); + expect(execFile).toHaveBeenCalledWith('/my/directory', 'yarn', [ + 'install', + ]); }); }); });