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
9 changes: 7 additions & 2 deletions src/helpers/utilsLocal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec } from 'child_process';
import { execFile } from 'child_process';
import { SystemType } from '../types/SystemType.js';
import { Architecture } from '../types/Architecture.js';

Expand All @@ -21,9 +21,14 @@ export function isTests() {
return jest || vitest;
}

// Only ever called with literal values today ('dpkg'/'rpm' in ManagerLocal.install()'s Linux
// branch), but uses execFile (no shell) rather than exec with a shell string on principle -
// every other command execution in this codebase avoids building shell strings from values that
// could someday trace back to registry/package metadata, and this should be no exception for
// whoever calls it next.
export function commandExists(cmd: string): Promise<boolean> {
return new Promise(resolve => {
exec(`command -v ${cmd}`, (error, stdout) => {
execFile('which', [cmd], (error, stdout) => {
resolve(Boolean(stdout.trim()) && !error);
});
});
Expand Down
11 changes: 10 additions & 1 deletion tests/helpers/utilsLocal.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest';
import { getArchitecture, getSystem, isTests } from '../../src/helpers/utilsLocal';
import { commandExists, getArchitecture, getSystem, isTests } from '../../src/helpers/utilsLocal';

test('Get Architecture', () => {
if (process.arch === 'arm') {
Expand All @@ -26,3 +26,12 @@ test('Get System', () => {
test('Is tests', () => {
expect(isTests()).toEqual(true);
});

test('Command exists', async () => {
// `which` isn't available as a standalone command on plain Windows (unlike Linux/Mac, which
// is the only place commandExists() is actually used - ManagerLocal.install()'s dpkg/rpm
// check).
if (process.platform === 'win32') return;
expect(await commandExists('node')).toEqual(true);
expect(await commandExists('this-command-should-not-exist-xyz123')).toEqual(false);
});
Loading