Files
start-os/sdk/base/lib/util/splitCommand.ts
2026-02-05 08:10:53 -07:00

27 lines
785 B
TypeScript

import { arrayOf, string } from "ts-matches"
/**
* Normalizes a command into an array format suitable for execution.
*
* If the command is already an array, it's returned as-is.
* If it's a string, it's wrapped in `sh -c` for shell interpretation.
*
* @param command - Command as string or array of strings
* @returns Command as array suitable for process execution
*
* @example
* ```typescript
* splitCommand(['nginx', '-g', 'daemon off;'])
* // Returns: ['nginx', '-g', 'daemon off;']
*
* splitCommand('nginx -g "daemon off;"')
* // Returns: ['sh', '-c', 'nginx -g "daemon off;"']
* ```
*/
export const splitCommand = (
command: string | [string, ...string[]],
): string[] => {
if (arrayOf(string).test(command)) return command
return ["sh", "-c", command]
}