Repository navigation
fix: make palette, preview, and MCP setup match the CLI #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9235351
fix: make palette, preview, and MCP setup match the CLI
SebTardif 28a022d
test: expect ComSpec when the Windows launcher has no override
SebTardif a2ec513
fix: accept preview exit 2 when the dry run would write
SebTardif 06d6d34
fix: stop offering Apply after a conflicting merge preview
SebTardif File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { execFile, type ExecFileOptions } from "node:child_process"; | ||
| import * as path from "node:path"; | ||
| import { promisify } from "node:util"; | ||
|
|
||
| const execFileAsync = promisify(execFile); | ||
|
|
||
| export interface PatchloomLaunchPlan { | ||
| readonly file: string; | ||
| readonly args: string[]; | ||
| /** Always false. `.cmd` shims go through `cmd.exe /d /s /c`, not `shell: true`. */ | ||
| readonly shell: false; | ||
| readonly windowsVerbatimArguments: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * How to spawn a Patchloom binary. | ||
| * Windows `npm install -g` exposes `patchloom.cmd`. Node `execFile` on that | ||
| * shim fails with `spawn EINVAL` unless `cmd.exe` runs it. | ||
| */ | ||
| export function patchloomLaunch( | ||
| binaryPath: string, | ||
| args: readonly string[], | ||
| platform: NodeJS.Platform = process.platform, | ||
| comSpec?: string, | ||
| verbatim = true | ||
| ): PatchloomLaunchPlan { | ||
| if (needsWindowsCommandProcessor(binaryPath, platform)) { | ||
| const script = [binaryPath, ...args].map(quoteCmdArgument).join(" "); | ||
| // `/s` strips one leading quote and the last quote. Callers that set | ||
| // windowsVerbatimArguments must supply that pair. A host that quotes | ||
| // argv itself (native MCP) must not get a second pair. | ||
| const commandLine = verbatim ? `"${script}"` : script; | ||
| return { | ||
| file: resolveComSpec(platform, comSpec), | ||
| args: ["/d", "/s", "/c", commandLine], | ||
| shell: false, | ||
| windowsVerbatimArguments: verbatim | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| file: binaryPath, | ||
| args: [...args], | ||
| shell: false, | ||
| windowsVerbatimArguments: false | ||
| }; | ||
| } | ||
|
|
||
| export async function execPatchloomFile( | ||
| binaryPath: string, | ||
| args: readonly string[], | ||
| options: ExecFileOptions | ||
| ): Promise<{ stdout: string; stderr: string }> { | ||
| const launch = patchloomLaunch(binaryPath, args); | ||
| const result = await execFileAsync(launch.file, launch.args, { | ||
| ...options, | ||
| encoding: "utf8", | ||
| shell: launch.shell, | ||
| windowsVerbatimArguments: launch.windowsVerbatimArguments | ||
| }); | ||
|
SebTardif marked this conversation as resolved.
Dismissed
|
||
| return { | ||
| stdout: typeof result.stdout === "string" ? result.stdout : "", | ||
| stderr: typeof result.stderr === "string" ? result.stderr : "" | ||
| }; | ||
| } | ||
|
|
||
| function needsWindowsCommandProcessor(binaryPath: string, platform: NodeJS.Platform): boolean { | ||
| if (platform !== "win32") { | ||
| return false; | ||
| } | ||
| const extension = path.win32.extname(binaryPath).toLowerCase(); | ||
| if (extension === ".cmd" || extension === ".bat") { | ||
| return true; | ||
| } | ||
| const bareName = !binaryPath.includes("\\") | ||
| && !binaryPath.includes("/") | ||
| && !path.win32.isAbsolute(binaryPath); | ||
| return bareName && extension.length === 0; | ||
| } | ||
|
|
||
| function resolveComSpec(platform: NodeJS.Platform, comSpec?: string): string { | ||
| if (comSpec && comSpec.trim().length > 0) { | ||
| return comSpec; | ||
| } | ||
| if (platform === "win32") { | ||
| const fromEnv = process.env.ComSpec; | ||
| if (fromEnv && fromEnv.trim().length > 0) { | ||
| return fromEnv; | ||
| } | ||
| } | ||
| return "cmd.exe"; | ||
| } | ||
|
|
||
| function quoteCmdArgument(value: string): string { | ||
| if (/[\r\n]/.test(value)) { | ||
| throw new Error( | ||
| "Patchloom cannot pass a newline through the Windows npm shim (patchloom.cmd). Use the managed patchloom.exe, or keep this value on one line." | ||
| ); | ||
| } | ||
| const escaped = value.replaceAll("%", "%%"); | ||
| if (escaped.length === 0 || /[\s"&|<>^()]/.test(escaped)) { | ||
| return `"${escaped.replaceAll("\"", "\"\"")}"`; | ||
| } | ||
| return escaped; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.