command-invocation.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { spawnSync } from 'node:child_process';
  2. import { existsSync } from 'node:fs';
  3. import { win32 } from 'node:path';
  4. import type { PluginHookCommandHandler, PluginHookSourceFormat } from './contracts.js';
  5. export interface HookCommandInvocation {
  6. readonly command: string;
  7. readonly args: readonly string[];
  8. readonly shell: boolean;
  9. }
  10. export interface HookCommandInvocationInput {
  11. readonly command: string;
  12. readonly args?: readonly string[];
  13. readonly shell?: PluginHookCommandHandler['shell'];
  14. readonly sourceFormat: PluginHookSourceFormat;
  15. }
  16. export interface HookCommandInvocationDependencies {
  17. readonly platform?: NodeJS.Platform;
  18. readonly environment?: NodeJS.ProcessEnv;
  19. readonly pathExists?: (candidate: string) => boolean;
  20. readonly findExecutable?: (name: string) => string | undefined;
  21. }
  22. /** Resolves compatible shell-form commands without relying on cmd.exe on Windows. */
  23. export function resolveHookCommandInvocation(
  24. input: HookCommandInvocationInput,
  25. dependencies: HookCommandInvocationDependencies = {},
  26. ): HookCommandInvocation {
  27. if (input.args) return { command: input.command, args: input.args, shell: false };
  28. const platform = dependencies.platform ?? process.platform;
  29. if (input.sourceFormat !== 'CLAUDE') {
  30. if (input.shell === 'bash')
  31. return { command: 'bash', args: ['-c', input.command], shell: false };
  32. if (input.shell === 'powershell') {
  33. return powershellInvocation(input.command, platform, dependencies);
  34. }
  35. return { command: input.command, args: [], shell: true };
  36. }
  37. if (platform !== 'win32') {
  38. if (input.shell === 'powershell')
  39. return powershellInvocation(input.command, platform, dependencies);
  40. return {
  41. command: input.shell === 'bash' ? 'bash' : 'sh',
  42. args: ['-c', input.command],
  43. shell: false,
  44. };
  45. }
  46. if (input.shell === 'powershell')
  47. return powershellInvocation(input.command, platform, dependencies);
  48. const gitBash = findWindowsGitBash(dependencies);
  49. if (gitBash) return { command: gitBash, args: ['-c', input.command], shell: false };
  50. if (input.shell === 'bash') {
  51. throw new Error('Compatible Hook requested Git Bash, but Git Bash is unavailable');
  52. }
  53. return powershellInvocation(input.command, platform, dependencies);
  54. }
  55. function powershellInvocation(
  56. command: string,
  57. platform: NodeJS.Platform,
  58. dependencies: HookCommandInvocationDependencies,
  59. ): HookCommandInvocation {
  60. const executable =
  61. platform === 'win32'
  62. ? (findExecutable(dependencies, 'pwsh.exe', platform) ?? 'powershell.exe')
  63. : (findExecutable(dependencies, 'pwsh', platform) ?? 'pwsh');
  64. return {
  65. command: executable,
  66. args: ['-NoProfile', '-NonInteractive', '-Command', command],
  67. shell: false,
  68. };
  69. }
  70. function findWindowsGitBash(dependencies: HookCommandInvocationDependencies): string | undefined {
  71. const environment = dependencies.environment ?? process.env;
  72. const pathExists = dependencies.pathExists ?? existsSync;
  73. const gitBashEnvironmentKey = ['CLA', 'UDE_CODE_GIT_BASH_PATH'].join('');
  74. const configured = environment[gitBashEnvironmentKey]?.trim();
  75. const candidates = [
  76. configured,
  77. environment.ProgramFiles && win32.join(environment.ProgramFiles, 'Git', 'bin', 'bash.exe'),
  78. environment.ProgramW6432 && win32.join(environment.ProgramW6432, 'Git', 'bin', 'bash.exe'),
  79. environment['ProgramFiles(x86)'] &&
  80. win32.join(environment['ProgramFiles(x86)'], 'Git', 'bin', 'bash.exe'),
  81. environment.LOCALAPPDATA &&
  82. win32.join(environment.LOCALAPPDATA, 'Programs', 'Git', 'bin', 'bash.exe'),
  83. ];
  84. for (const candidate of candidates) {
  85. if (candidate && pathExists(candidate)) return candidate;
  86. }
  87. const git = findExecutable(dependencies, 'git.exe', 'win32');
  88. if (git) {
  89. const gitDirectory = win32.dirname(git);
  90. const derived =
  91. win32.basename(gitDirectory).toLowerCase() === 'cmd'
  92. ? win32.join(win32.dirname(gitDirectory), 'bin', 'bash.exe')
  93. : win32.join(gitDirectory, 'bash.exe');
  94. if (pathExists(derived)) return derived;
  95. }
  96. const bash = findExecutable(dependencies, 'bash.exe', 'win32');
  97. if (bash && /[\\/]Git[\\/]/iu.test(bash) && pathExists(bash)) return bash;
  98. return undefined;
  99. }
  100. function findExecutable(
  101. dependencies: HookCommandInvocationDependencies,
  102. name: string,
  103. platform: NodeJS.Platform,
  104. ): string | undefined {
  105. if (dependencies.findExecutable) return dependencies.findExecutable(name);
  106. const finder = platform === 'win32' ? 'where.exe' : 'which';
  107. const result = spawnSync(finder, [name], {
  108. encoding: 'utf8',
  109. windowsHide: true,
  110. timeout: 1_000,
  111. });
  112. if (result.status !== 0) return undefined;
  113. return result.stdout
  114. .split(/\r?\n/u)
  115. .map((line) => line.trim())
  116. .find(Boolean);
  117. }