Skip to content

Commit 549afa6

Browse files
author
tal-rofe
committed
feat: 🔥 support Windows spawn
support Windows spawn
1 parent afaaa6f commit 549afa6

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

‎src/constants/windows.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import path from 'node:path';
2+
import os from 'node:os';
3+
4+
export const EXECUTE_COMMAND_SCRIPT_FILE_PATH = path.join(os.tmpdir(), 'sudo-cli', 'execute.bat');

‎src/functions/darwin.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'node:path';
2+
13
import fs from 'fs-extra';
24
import format from 'string-template';
35

@@ -16,5 +18,5 @@ export const executePrivilegedDarwin = async (name: string, command: string) =>
1618
`/usr/bin/env SUDO_ASKPASS="${ASK_PASSWORD_SCRIPT_FILE_PATH}" /usr/bin/sudo -A ${command}`,
1719
);
1820

19-
fs.remove(ASK_PASSWORD_SCRIPT_FILE_PATH);
21+
fs.remove(path.dirname(ASK_PASSWORD_SCRIPT_FILE_PATH));
2022
};

‎src/functions/windows.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1-
export const executePrivilegedWindows = async (name: string, command: string) => {
2-
await Promise.resolve([name, command]);
1+
import os from 'node:os';
2+
import path from 'node:path';
3+
4+
import fs from 'fs-extra';
5+
6+
import { EXECUTE_COMMAND_SCRIPT_FILE_PATH } from '@/constants/windows';
7+
import { spawnCommand } from '@/utils/spawn';
8+
9+
export const executePrivilegedWindows = async (command: string) => {
10+
await fs.outputFile(
11+
EXECUTE_COMMAND_SCRIPT_FILE_PATH,
12+
['@echo off', 'chcp 65001>nul', command].join(os.EOL),
13+
{ encoding: 'utf8' },
14+
);
15+
16+
await spawnCommand(
17+
`powershell.exe Start-Process -FilePath "${EXECUTE_COMMAND_SCRIPT_FILE_PATH}" -WorkingDirectory "${process.cwd()}" -WindowStyle hidden -Verb runAs`,
18+
);
19+
20+
fs.remove(path.dirname(EXECUTE_COMMAND_SCRIPT_FILE_PATH));
321
};

‎src/helpers/execute-sudo.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const executeSudo = async (name: string, command: string) => {
88
}
99

1010
if (process.platform === 'win32') {
11-
await executePrivilegedWindows(name, command);
11+
await executePrivilegedWindows(command);
1212
}
1313

1414
if (process.platform === 'linux') {

0 commit comments

Comments
 (0)