Skip to content

AI-native design editor. Open-source Figma alternative.

1.9 KB
Raw Blame
/**
 * Resolve a CLI invocation into the form the Tauri process spawner can launch
 * on the current platform.
 *
 * On Windows, npm-installed global CLIs (e.g. `claude-agent-acp`,
 * `openpencil-mcp-http`) are `.cmd` shims. The Rust spawner behind
 * `@tauri-apps/plugin-shell` only resolves real executables, so launching a
 * `.cmd` directly fails with ENOENT. Routing through `cmd /c` lets the shell
 * resolve the shim via PATHEXT.
 *
 * `command` is the name of a shell scope entry in desktop/capabilities/default.json, and
 * each scope entry pins the whole command line. On Windows the entry for `name` is
 * `cmd-<name>`, which runs exactly `cmd /c <name> <args>`: a scope that let `cmd` take any
 * arguments would let the webview run any command.
 *
 * The `userAgent` argument is injectable for testing; it defaults to the live
 * navigator value at runtime, falling back to an empty string in non-browser
 * contexts (e.g. headless tests) where `navigator` is unavailable.
 */
export function detectUserAgent(): string {
  const ua = typeof navigator === 'undefined' ? undefined : navigator.userAgent
  return typeof ua === 'string' ? ua : ''
}

export function resolvePlatformCommand(
  command: string,
  args: string[] = [],
  userAgent: string = detectUserAgent()
): { command: string; args: string[] } {
  return resolveScopedCommand(command, command, args, userAgent)
}

/**
 * Like `resolvePlatformCommand`, for a scope entry named apart from the program it runs, such
 * as `claude-agent-acp-sign-in`, which pins different arguments for `claude-agent-acp`.
 */
export function resolveScopedCommand(
  scope: string,
  program: string,
  args: string[] = [],
  userAgent: string = detectUserAgent()
): { command: string; args: string[] } {
  if (userAgent.includes('Windows')) {
    return { command: `cmd-${scope}`, args: ['/c', program, ...args] }
  }
  return { command: scope, args }
}