The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off |
| 2 | // Windows cleanup must outlive this executable to delete its directory; scoped ChildProcess kills children on exit. |
| 3 | import * as NodeChildProcess from "node:child_process"; |
| 4 | |
| 5 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 6 | import * as Console from "effect/Console"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as FileSystem from "effect/FileSystem"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import * as Schema from "effect/Schema"; |
| 12 | import { Command, Flag, GlobalFlag, Prompt } from "effect/cli"; |
| 13 | |
| 14 | import * as BootService from "../cloud/bootService.ts"; |
| 15 | import { |
| 16 | findPosixLauncher, |
| 17 | generatedWindowsShimTarget, |
| 18 | isVersionExecutable, |
| 19 | pinnedRuntimeVersionsDir, |
| 20 | readPosixLauncherTarget, |
| 21 | resolveLauncherPath, |
| 22 | } from "../cloud/pinnedRuntime.ts"; |
| 23 | import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts"; |
| 24 | import * as CliService from "./service.ts"; |
| 25 | import { awaitDetachedSpawn } from "../process/windowsVerbatimSpawn.ts"; |
| 26 | import { findWindowsShim } from "./update.ts"; |
| 27 | |
| 28 | export class CliUninstallError extends Schema.TaggedError<CliUninstallError>()( |
| 29 | "CliUninstallError", |
| 30 | { reason: Schema.String }, |
| 31 | ) { |
| 32 | override get message(): string { |
| 33 | return this.reason; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | export interface UninstallPlan { |
| 38 | readonly service: boolean; |
| 39 | readonly otherHomeService: string | undefined; |
| 40 | readonly versionsDir: string; |
| 41 | readonly launchers: ReadonlyArray<string>; |
| 42 | readonly runtimeDir: string | undefined; |
| 43 | readonly userdataDir: string; |
| 44 | } |
| 45 | |
| 46 | /** Delete only launchers targeting this home's runtime/versions. */ |
| 47 | export const findOwnedLauncher = Effect.fn("cli.uninstall.find_launcher")(function* (input: { |
| 48 | readonly launchedAs: string | undefined; |
| 49 | readonly versionsDir: string; |
| 50 | }) { |
| 51 | const fs = yield* FileSystem.FileSystem; |
| 52 | const path = yield* Path.Path; |
| 53 | const platform = yield* HostProcess.Platform; |
| 54 | if (input.launchedAs === undefined) return undefined; |
| 55 | if (platform === "win32") { |
| 56 | const shimPath = yield* findWindowsShim(input.launchedAs); |
| 57 | if (shimPath === undefined) return undefined; |
| 58 | const contents = yield* fs.readFileString(shimPath).pipe(Effect.option); |
| 59 | const target = Option.isSome(contents) ? generatedWindowsShimTarget(contents.value) : undefined; |
| 60 | return target !== undefined && isVersionExecutable(path, input.versionsDir, target) |
| 61 | ? shimPath |
| 62 | : undefined; |
| 63 | } |
| 64 | return yield* findPosixLauncher({ launchedAs: input.launchedAs, versionsDir: input.versionsDir }); |
| 65 | }); |
| 66 | |
| 67 | /** Whether `launcher` is a link, unedited `t3` script, or unedited `t3.cmd` into this home's runtime/versions. */ |
| 68 | const ownsLauncher = Effect.fn("cli.uninstall.owns_launcher")(function* ( |
| 69 | launcher: string, |
| 70 | versionsDir: string, |
| 71 | ) { |
| 72 | const fs = yield* FileSystem.FileSystem; |
| 73 | const path = yield* Path.Path; |
| 74 | const platform = yield* HostProcess.Platform; |
| 75 | const realVersionsDir = yield* fs |
| 76 | .realPath(versionsDir) |
| 77 | .pipe(Effect.orElseSucceed(() => versionsDir)); |
| 78 | // The link's own text keeps a pruned (dangling) target; the real path sees through a |
| 79 | // symlinked T3 home. |
| 80 | const targets = |
| 81 | platform === "win32" |
| 82 | ? [ |
| 83 | yield* fs.readFileString(launcher).pipe( |
| 84 | Effect.map(generatedWindowsShimTarget), |
| 85 | Effect.orElseSucceed(() => undefined), |
| 86 | ), |
| 87 | ] |
| 88 | : [ |
| 89 | yield* fs.readLink(launcher).pipe( |
| 90 | Effect.map((link) => path.resolve(path.dirname(launcher), link)), |
| 91 | Effect.orElseSucceed(() => undefined), |
| 92 | ), |
| 93 | yield* fs.realPath(launcher).pipe(Effect.orElseSucceed(() => undefined)), |
| 94 | yield* readPosixLauncherTarget(launcher), |
| 95 | ]; |
| 96 | return targets.some( |
| 97 | (target) => |
| 98 | target !== undefined && |
| 99 | (isVersionExecutable(path, versionsDir, target) || |
| 100 | isVersionExecutable(path, realVersionsDir, target)), |
| 101 | ); |
| 102 | }); |
| 103 | |
| 104 | /** |
| 105 | * Launchers recorded in `runtime/launchers` by the install scripts and `t3 install` that still |
| 106 | * point into this home's runtime/versions. npx can run a different version than the installed |
| 107 | * one, so the launcher this process started from does not always name them. |
| 108 | */ |
| 109 | export const findRecordedLaunchers = Effect.fn("cli.uninstall.find_recorded_launchers")(function* ( |
| 110 | versionsDir: string, |
| 111 | ) { |
| 112 | const fs = yield* FileSystem.FileSystem; |
| 113 | const path = yield* Path.Path; |
| 114 | const recorded = yield* fs |
| 115 | .readFileString(path.join(path.dirname(versionsDir), "launchers")) |
| 116 | .pipe(Effect.orElseSucceed(() => "")); |
| 117 | const owned: Array<string> = []; |
| 118 | for (const launcher of recorded.split(/\r?\n/).filter(Boolean)) { |
| 119 | if (yield* ownsLauncher(launcher, versionsDir)) owned.push(launcher); |
| 120 | } |
| 121 | return owned; |
| 122 | }); |
| 123 | |
| 124 | // cmd.exe needs verbatim quoting and a literal path, with delayed expansion disabled. |
| 125 | export function spawnWindowsRemoval(comspec: string, dir: string) { |
| 126 | return NodeChildProcess.spawn( |
| 127 | comspec, |
| 128 | [ |
| 129 | "/d", |
| 130 | "/v:off", |
| 131 | "/s", |
| 132 | "/c", |
| 133 | '"ping -n 3 127.0.0.1 >nul & rmdir /s /q "%T3_UNINSTALL_RUNTIME%""', |
| 134 | ], |
| 135 | { |
| 136 | env: { ...process.env, T3_UNINSTALL_RUNTIME: dir }, |
| 137 | windowsVerbatimArguments: true, |
| 138 | detached: true, |
| 139 | stdio: "ignore", |
| 140 | windowsHide: true, |
| 141 | }, |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | const planUninstall = Effect.fn("cli.uninstall.plan")(function* (input: { |
| 146 | readonly baseDir: string; |
| 147 | }) { |
| 148 | const fs = yield* FileSystem.FileSystem; |
| 149 | const path = yield* Path.Path; |
| 150 | const service = yield* BootService.BootService; |
| 151 | const status = yield* service.status; |
| 152 | const installedBaseDir = |
| 153 | status.supported && status.installed ? status.installedBaseDir : undefined; |
| 154 | const servesThisHome = |
| 155 | installedBaseDir !== undefined && |
| 156 | path.resolve(installedBaseDir) === path.resolve(input.baseDir); |
| 157 | const versionsDir = pinnedRuntimeVersionsDir(path, input.baseDir); |
| 158 | const runtimeDir = path.dirname(versionsDir); |
| 159 | const launchedAs = (yield* HostProcess.IsExecutable) ? yield* resolveLauncherPath : undefined; |
| 160 | const launchedFrom = yield* findOwnedLauncher({ launchedAs, versionsDir }); |
| 161 | const plan: UninstallPlan = { |
| 162 | service: servesThisHome, |
| 163 | otherHomeService: servesThisHome ? undefined : installedBaseDir, |
| 164 | versionsDir, |
| 165 | launchers: [ |
| 166 | ...new Set([ |
| 167 | ...(launchedFrom === undefined ? [] : [launchedFrom]), |
| 168 | ...(yield* findRecordedLaunchers(versionsDir)), |
| 169 | ]), |
| 170 | ], |
| 171 | runtimeDir: (yield* fs.exists(runtimeDir).pipe(Effect.orElseSucceed(() => false))) |
| 172 | ? runtimeDir |
| 173 | : undefined, |
| 174 | userdataDir: path.join(input.baseDir, "userdata"), |
| 175 | }; |
| 176 | return plan; |
| 177 | }); |
| 178 | |
| 179 | export const uninstallCommand = Command.make("uninstall", { |
| 180 | ...projectLocationFlags, |
| 181 | yes: Flag.Boolean("yes").pipe( |
| 182 | Flag.withAlias("y"), |
| 183 | Flag.withDescription( |
| 184 | "Remove everything without asking. Required from a script, where there is no prompt.", |
| 185 | ), |
| 186 | Flag.withDefault(false), |
| 187 | ), |
| 188 | }).pipe( |
| 189 | Command.withDescription( |
| 190 | "Remove t3 from this machine: the background service, the launcher, and every downloaded version. Your projects and threads are kept.", |
| 191 | ), |
| 192 | Command.withHandler((flags) => |
| 193 | Effect.gen(function* () { |
| 194 | const logLevel = yield* GlobalFlag.LogLevel; |
| 195 | const config = yield* resolveCliAuthConfig(flags, logLevel); |
| 196 | return yield* runUninstall({ baseDir: config.baseDir, assumeYes: flags.yes }).pipe( |
| 197 | Effect.provide(CliService.layer(config)), |
| 198 | ); |
| 199 | }), |
| 200 | ), |
| 201 | ); |
| 202 | |
| 203 | const logOtherHomeService = (plan: UninstallPlan) => |
| 204 | plan.otherHomeService === undefined |
| 205 | ? Effect.void |
| 206 | : Console.log( |
| 207 | `The T3 Code service serves ${plan.otherHomeService}, so it was not removed. Pass \`${BootService.formatBootServiceBaseDirOption(plan.otherHomeService)}\` to remove it.`, |
| 208 | ); |
| 209 | |
| 210 | const runUninstall = Effect.fn("cli.uninstall.run")(function* (input: { |
| 211 | readonly baseDir: string; |
| 212 | readonly assumeYes: boolean; |
| 213 | }) { |
| 214 | const fs = yield* FileSystem.FileSystem; |
| 215 | const platform = yield* HostProcess.Platform; |
| 216 | const environment = yield* HostProcess.Environment; |
| 217 | const service = yield* BootService.BootService; |
| 218 | const plan = yield* planUninstall({ baseDir: input.baseDir }); |
| 219 | |
| 220 | if (!plan.service && plan.launchers.length === 0 && plan.runtimeDir === undefined) { |
| 221 | yield* Console.log(`Nothing to remove: t3 is not installed for ${input.baseDir}.`); |
| 222 | if (!(yield* HostProcess.IsExecutable)) { |
| 223 | yield* Console.log( |
| 224 | " This t3 runs from a Node script, so it was installed by npm or built from source. Remove it the same way (`npm uninstall -g t3`, or delete the checkout).", |
| 225 | ); |
| 226 | } |
| 227 | yield* logOtherHomeService(plan); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | yield* Console.log("This will remove:"); |
| 232 | if (plan.service) yield* Console.log(" the background service (stopping it first)"); |
| 233 | for (const launcher of plan.launchers) yield* Console.log(` the launcher at ${launcher}`); |
| 234 | if (plan.runtimeDir !== undefined) { |
| 235 | yield* Console.log(` every downloaded version under ${plan.runtimeDir}`); |
| 236 | } |
| 237 | yield* Console.log( |
| 238 | `Your projects, threads, and settings under ${plan.userdataDir} are kept. Delete that directory yourself if you want them gone too.`, |
| 239 | ); |
| 240 | |
| 241 | if (!input.assumeYes) { |
| 242 | if (!(process.stdin.isTTY && process.stdout.isTTY)) { |
| 243 | return yield* new CliUninstallError({ |
| 244 | reason: |
| 245 | "Not a terminal, so nothing was removed. Rerun with --yes to confirm from a script.", |
| 246 | }); |
| 247 | } |
| 248 | const confirmed = yield* Prompt.run( |
| 249 | Prompt.Confirm({ message: "Remove t3 from this machine?", initial: false }), |
| 250 | ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) })); |
| 251 | if (!confirmed) { |
| 252 | yield* Console.log("Left as is."); |
| 253 | return; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (plan.service) { |
| 258 | yield* service.uninstall; |
| 259 | yield* Console.log("Removed the background service."); |
| 260 | } |
| 261 | for (const launcher of plan.launchers) { |
| 262 | // The prompt can wait a long time; never delete a file that replaced the launcher meanwhile. |
| 263 | if (!(yield* ownsLauncher(launcher, plan.versionsDir))) { |
| 264 | yield* Console.log(`Left ${launcher}: it no longer runs this t3.`); |
| 265 | continue; |
| 266 | } |
| 267 | yield* fs |
| 268 | .remove(launcher, { force: true }) |
| 269 | .pipe( |
| 270 | Effect.mapError( |
| 271 | () => new CliUninstallError({ reason: `Could not remove the launcher at ${launcher}.` }), |
| 272 | ), |
| 273 | ); |
| 274 | yield* Console.log(`Removed ${launcher}.`); |
| 275 | } |
| 276 | if (plan.runtimeDir !== undefined) { |
| 277 | // Windows refuses to unlink this running executable; a detached shell removes it after exit. |
| 278 | if (platform === "win32") { |
| 279 | const runtimeDir = plan.runtimeDir; |
| 280 | const comspec = environment["ComSpec"] ?? environment["COMSPEC"] ?? "cmd.exe"; |
| 281 | const notScheduled = () => |
| 282 | new CliUninstallError({ |
| 283 | reason: `Could not schedule removal of ${runtimeDir}. Delete it yourself once this window is closed.`, |
| 284 | }); |
| 285 | yield* awaitDetachedSpawn(() => spawnWindowsRemoval(comspec, runtimeDir)).pipe( |
| 286 | Effect.mapError(notScheduled), |
| 287 | ); |
| 288 | yield* Console.log(`${runtimeDir} will be removed once t3 exits.`); |
| 289 | } else { |
| 290 | yield* fs |
| 291 | .remove(plan.runtimeDir, { recursive: true, force: true }) |
| 292 | .pipe( |
| 293 | Effect.mapError( |
| 294 | () => new CliUninstallError({ reason: `Could not remove ${plan.runtimeDir}.` }), |
| 295 | ), |
| 296 | ); |
| 297 | yield* Console.log(`Removed ${plan.runtimeDir}.`); |
| 298 | } |
| 299 | } |
| 300 | yield* Console.log(""); |
| 301 | yield* Console.log("t3 is uninstalled. Thanks for trying T3 Code."); |
| 302 | yield* logOtherHomeService(plan); |
| 303 | }); |