The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- The interpreter sits beside the launcher it belongs to. |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as NodeFS from "node:fs"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | |
| 6 | /** The `AZ_INSTALLER` value each launcher sets, which `az` only reads for its user agent. */ |
| 7 | export type AzureCliInstaller = "MSI" | "ZIP"; |
| 8 | |
| 9 | export interface AzureCliInstall { |
| 10 | /** The interpreter the launcher runs: `<install>\python.exe -IBm azure.cli`. */ |
| 11 | readonly interpreter: string; |
| 12 | readonly installer: AzureCliInstaller; |
| 13 | } |
| 14 | |
| 15 | const INSTALLERS_BY_LAUNCHER_DIRECTORY: Readonly<Record<string, AzureCliInstaller>> = { |
| 16 | wbin: "MSI", |
| 17 | bin: "ZIP", |
| 18 | }; |
| 19 | |
| 20 | /** The MSI and ZIP builds both launch the interpreter beside the install root, from `wbin` and `bin`. */ |
| 21 | export const azureCliInstall = ( |
| 22 | launcherPath: string, |
| 23 | pathExists: (filePath: string) => boolean = NodeFS.existsSync, |
| 24 | ): AzureCliInstall | undefined => { |
| 25 | const path = NodePath.win32; |
| 26 | const extension = path.extname(launcherPath).toLowerCase(); |
| 27 | if (extension !== ".cmd" && extension !== ".bat") return undefined; |
| 28 | const launcherDirectory = path.dirname(launcherPath); |
| 29 | const installer = |
| 30 | INSTALLERS_BY_LAUNCHER_DIRECTORY[path.basename(launcherDirectory).toLowerCase()]; |
| 31 | if (installer === undefined) return undefined; |
| 32 | const install = path.dirname(launcherDirectory); |
| 33 | // Another tool's `bin\az.cmd` beside a `python.exe` is not the CLI; the CLI ships its own packages. |
| 34 | const cliPackage = path.join(install, "Lib", "site-packages", "azure", "cli"); |
| 35 | if (!pathExists(cliPackage)) return undefined; |
| 36 | const interpreter = path.join(install, "python.exe"); |
| 37 | return pathExists(interpreter) ? { interpreter, installer } : undefined; |
| 38 | }; |
| 39 | |
| 40 | /** The launcher path comes from the host's PATH, so this reads the Windows layout it names. */ |
| 41 | export const AzureCliInstallLocation = Context.Reference< |
| 42 | (launcherPath: string) => AzureCliInstall | undefined |
| 43 | >("@t3tools/source-control-azure-devops/azureCliInstallLocation", { |
| 44 | defaultValue: () => azureCliInstall, |
| 45 | }); |