Skip to content

Commit a68b0ec

Browse files
committed
fix: Fixed running sudo commands on linux and commands with quotes
1 parent b69734e commit a68b0ec

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/utils/spawn.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ export async function spawnSafe(cmd: string, options?: SpawnOptions, pluginName?
6868
// Mac OS uses -SN instead of -Sn
6969
let command;
7070
if (OsUtils.isMacOS()) {
71-
command = options?.requiresRoot ? `sudo -k; sudo -SN <<< "${password}" ${cmd}` : cmd;
71+
command = options?.requiresRoot ? `sudo -k >/dev/null 2>&1; sudo -SN <<< "${password}" bash ${options?.interactive ? '-i' : ''} -c "${cmd.replaceAll('"', '\\"')}"` : cmd;
7272
} else {
73-
command = options?.requiresRoot ? `sudo -k; sudo -Sk <<< "${password}" ${cmd}` : cmd;
73+
command = options?.requiresRoot ? `sudo -k >/dev/null 2>&1; sudo -S <<< "${password}" bash ${options?.interactive ? '-i' : ''} -c "${cmd.replaceAll('"', '\\"')}"` : cmd;
7474
}
7575

76+
console.log(command);
77+
7678
const args = options?.interactive ? ['-i', '-c', command] : ['-c', command]
7779

7880
// Run the command in a pty for interactivity

src/utils/sudo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { execSync } from 'node:child_process';
22

33
import { OsUtils } from './os-utils.js';
4+
import { ShellUtils } from './shell.js';
45

56
export const SudoUtils = {
67
validate(password?: string): boolean {
@@ -12,7 +13,7 @@ export const SudoUtils = {
1213
}
1314

1415
// Sudo with -Snv will not prompt if within sudo cache timeout
15-
execSync(`sudo -Skv ${password ? `<<< ${password}` : ''} >/dev/null 2>&1`, { stdio: 'ignore' })
16+
execSync(`sudo -Skv ${password ? `<<< '${password}'` : ''}`, { stdio: 'ignore', shell: ShellUtils.getDefaultShell() })
1617
return true;
1718
} catch {
1819
return false;

0 commit comments

Comments
 (0)