| 14 | export type CliController = ReturnType<typeof createCliController> |
| 15 | |
| 16 | export function createCliController() { |
| 17 | const getStatus = async (): Promise<ElectricCliStatus> => { |
| 18 | const bundledVersion = await readBundledCliVersion() |
| 19 | const installDir = defaultInstallDir() |
| 20 | const installDirOnPath = pathIncludes(installDir) |
| 21 | const managedPath = managedShimPath(installDir) |
| 22 | const matches = await findCommandsOnPath(COMMAND) |
| 23 | const managedShim = await readManagedShim(managedPath) |
| 24 | const firstPath = matches[0] ?? null |
| 25 | |
| 26 | if (!firstPath && !managedShim.exists) { |
| 27 | return status(bundledVersion, { |
| 28 | kind: `not-installed`, |
| 29 | path: null, |
| 30 | version: null, |
| 31 | managedPath: null, |
| 32 | installDir, |
| 33 | installDirOnPath, |
| 34 | error: null, |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | if (managedShim.exists && !managedShim.managed) { |
| 39 | return status(bundledVersion, { |
| 40 | kind: firstPath === managedPath ? `manual` : `shadowed`, |
| 41 | path: firstPath, |
| 42 | version: firstPath ? await readCliVersion(firstPath) : null, |
| 43 | managedPath: null, |
| 44 | installDir, |
| 45 | installDirOnPath, |
| 46 | error: `A file already exists at the managed install path but was not created by Electric Agents Desktop.`, |
| 47 | }) |
| 48 | } |
| 49 | |
| 50 | if (managedShim.exists && firstPath !== managedPath) { |
| 51 | return status(bundledVersion, { |
| 52 | kind: `shadowed`, |
| 53 | path: firstPath, |
| 54 | version: firstPath ? await readCliVersion(firstPath) : null, |
| 55 | managedPath, |
| 56 | installDir, |
| 57 | installDirOnPath, |
| 58 | error: firstPath |
| 59 | ? `A different electric command appears earlier on PATH.` |
| 60 | : `The managed CLI install directory is not on PATH.`, |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | if (managedShim.exists) { |
| 65 | const versionResult = await readCliVersionResult(managedPath) |
| 66 | return status(bundledVersion, { |
| 67 | kind: versionResult.error ? `broken` : `managed`, |
| 68 | path: managedPath, |
| 69 | version: versionResult.version, |
| 70 | managedPath, |
| 71 | installDir, |
| 72 | installDirOnPath, |
| 73 | error: versionResult.error, |