This commit is contained in:
07akioni
2026-07-28 15:21:34 +08:00
parent b926044c13
commit c30c891628
11 changed files with 130 additions and 135 deletions
@@ -1,13 +1,9 @@
/** Cross-platform open-with-default-application used by the local GUI carrier. */
import { execFile } from 'node:child_process'
import { runNativeCommand, type NativeCommandRunner } from './native-command.ts'
/** Testable command boundary; native implementations never invoke a shell. */
export type PathOpenerRunner = (
command: string,
args: readonly string[],
signal: AbortSignal,
) => Promise<{ stdout: string; stderr: string }>
export type PathOpenerRunner = NativeCommandRunner
/** Injectable platform facts for deterministic adapter tests. */
export interface PathOpenerInternals {
@@ -15,27 +11,6 @@ export interface PathOpenerInternals {
run?: PathOpenerRunner
}
const runCommand: PathOpenerRunner = (command, args, signal) =>
new Promise((resolve, reject) => {
execFile(
command,
[...args],
{ encoding: 'utf8', signal, windowsHide: true },
(error, stdout, stderr) => {
if (error !== null) {
const failure = Object.assign(new Error(error.message, { cause: error }), {
code: error.code,
stdout,
stderr,
})
reject(failure)
return
}
resolve({ stdout, stderr })
},
)
})
/** PowerShell single-quoted literal (doubles embedded quotes). */
function powershellLiteral(path: string): string {
return `'${path.replace(/'/g, "''")}'`
@@ -53,7 +28,7 @@ export async function openNativePath(
internals: PathOpenerInternals = {},
): Promise<void> {
const platform = internals.platform ?? process.platform
const run = internals.run ?? runCommand
const run = internals.run ?? runNativeCommand
if (platform === 'darwin') {
await run('open', [path], signal)