Files
orca/src/shared/shell-argument-quoting.ts
T
NeilandClaude 6d8e4cfef2 refactor(shell): one shell-argument-quoting module for the duplicated quote copies (#30564)
* refactor(shell): one shell-argument-quoting module for the duplicated quote copies

Each former copy now calls the canonical function with its exact spelling; a table test pins
byte-identical output for every copy over hostile inputs. Distinct spellings stay distinct
functions: '\'' vs '"'"', the two plain-word sets, and the cmd.exe doubled quote.

Co-Authored-By: Claude <noreply@anthropic.com>

* style: keep the quoting import at the top of two import-less files

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-18 23:12:27 -07:00

32 lines
1.4 KiB
TypeScript

// One home for shell-argument quoting. Each spelling below is a separate contract: callers embed,
// compare or parse back these exact bytes, so variants stay separate functions, never flags.
// PowerShell quoting lives in powershell-native-argument.ts; CommandLineToArgvW in @orca/process-host.
/** POSIX sh single-quoting: each `'` closes the quote, is escaped, and reopens it (`'\''`). */
export function quotePosixShell(value: string): string {
return `'${value.replace(/'/g, "'\\''")}'`
}
/** As `quotePosixShell`, but a non-empty word of only `A-Za-z0-9_./@:-` stays bare. */
export function quotePosixShellUnlessPlain(value: string): string {
return /^[a-zA-Z0-9_./@:-]+$/.test(value) ? value : quotePosixShell(value)
}
/** As `quotePosixShellUnlessPlain`, except `@` also forces quoting. */
export function quotePosixShellUnlessPlainNoAt(value: string): string {
return /^[A-Za-z0-9_./:-]+$/.test(value) ? value : quotePosixShell(value)
}
/**
* POSIX sh single-quoting that spells each `'` as `'"'"'`. Installed CLI wrappers are read back
* in this spelling (legacy-appimage-cli-wrapper.ts, wsl-cli-scripts.ts), so it must not change.
*/
export function quotePosixShellDoubleQuotedApostrophe(value: string): string {
return `'${value.replaceAll("'", `'"'"'`)}'`
}
/** cmd.exe double-quoting with `"` doubled. Leaves `%`, `^` and `!` as they are. */
export function quoteCmdDoubleQuoted(value: string): string {
return `"${value.replace(/"/g, '""')}"`
}