(options: WrapperScriptOptions)
| 49 | * Pattern: trap 'echo $? > exit_code' EXIT && cd /path && export K=V && script |
| 50 | */ |
| 51 | export function buildWrapperScript(options: WrapperScriptOptions): string { |
| 52 | const parts: string[] = []; |
| 53 | |
| 54 | // Set up trap first to capture exit code. |
| 55 | // |
| 56 | // IMPORTANT: Do NOT inline shellQuote(exitCodePath) inside a double-quoted trap string. |
| 57 | // If the path contains a single quote (e.g. processId derived from script contains quotes), |
| 58 | // shellQuote() will emit the POSIX escape pattern '\''"'"'\'', which contains double quotes |
| 59 | // and will break the surrounding double quotes. |
| 60 | // |
| 61 | // Instead, assign the (quoted) path to a variable and reference it from the trap. |
| 62 | parts.push(`__MUX_EXIT_CODE_PATH=${shellQuote(options.exitCodePath)}`); |
| 63 | parts.push(`trap 'echo $? > "$__MUX_EXIT_CODE_PATH"' EXIT`); |
| 64 | |
| 65 | // Change to working directory |
| 66 | parts.push(`cd ${shellQuote(options.cwd)}`); |
| 67 | |
| 68 | // Add environment variable exports |
| 69 | if (options.env) { |
| 70 | for (const [key, value] of Object.entries(options.env)) { |
| 71 | parts.push(buildShellExport(key, value)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Add the actual script |
| 76 | parts.push(options.script); |
| 77 | |
| 78 | return parts.join(" && "); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Options for building the spawn command. |
no test coverage detected