Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/main/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -54,18 +54,15 @@ export async function addModules(
{ dir, packageManager }: PMOperationOptions,
...names: Array<string>
): Promise<string> {
let nameArgs: Array<string> = [];
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);
}

/**
Expand Down
20 changes: 19 additions & 1 deletion src/main/utils/exec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -40,3 +40,21 @@ export async function exec(dir: string, cliArgs: string): Promise<string> {

return stdout.trim();
}

/**
* Execute a command with arguments in a directory.
*/
export async function execFile(
dir: string,
cmd: string,
args: Array<string>,
): Promise<string> {
await maybeFixPath();

const { stdout } = await promisify(cp_execFile)(cmd, args, {
cwd: dir,
maxBuffer: 200 * 1024 * 100,
});

return stdout.trim();
}
35 changes: 20 additions & 15 deletions tests/main/npm-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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',
]);
});
});

Expand All @@ -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',
]);
});
});
});
Expand Down
Loading