|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import fs from "node:fs"; |
| 5 | +import os from "node:os"; |
| 6 | +import path from "node:path"; |
| 7 | +import type { SpawnSyncReturns } from "node:child_process"; |
| 8 | + |
| 9 | +export function buildVersionedUninstallUrl(version: string): string { |
| 10 | + const stableVersion = String(version || "").trim().replace(/^v/, "").replace(/-.*/, ""); |
| 11 | + return `https://raw.githubusercontent.com/NVIDIA/NemoClaw/refs/tags/v${stableVersion}/uninstall.sh`; |
| 12 | +} |
| 13 | + |
| 14 | +export function resolveUninstallScript( |
| 15 | + candidates: string[], |
| 16 | + existsSyncImpl: (path: string) => boolean = fs.existsSync, |
| 17 | +): string | null { |
| 18 | + for (const candidate of candidates) { |
| 19 | + if (existsSyncImpl(candidate)) { |
| 20 | + return candidate; |
| 21 | + } |
| 22 | + } |
| 23 | + return null; |
| 24 | +} |
| 25 | + |
| 26 | +export function exitWithSpawnResult( |
| 27 | + result: Pick<SpawnSyncReturns<string>, "status" | "signal">, |
| 28 | + exit: (code: number) => never = (code) => process.exit(code), |
| 29 | +): never { |
| 30 | + if (result.status !== null) { |
| 31 | + return exit(result.status); |
| 32 | + } |
| 33 | + |
| 34 | + if (result.signal) { |
| 35 | + const signalNumber = os.constants.signals[result.signal]; |
| 36 | + return exit(signalNumber ? 128 + signalNumber : 1); |
| 37 | + } |
| 38 | + |
| 39 | + return exit(1); |
| 40 | +} |
| 41 | + |
| 42 | +export interface RunUninstallCommandDeps { |
| 43 | + args: string[]; |
| 44 | + rootDir: string; |
| 45 | + currentDir: string; |
| 46 | + remoteScriptUrl: string; |
| 47 | + env: NodeJS.ProcessEnv; |
| 48 | + spawnSyncImpl: ( |
| 49 | + file: string, |
| 50 | + args: string[], |
| 51 | + options?: Record<string, unknown>, |
| 52 | + ) => Pick<SpawnSyncReturns<string>, "status" | "signal">; |
| 53 | + execFileSyncImpl: (file: string, args: string[], options?: Record<string, unknown>) => void; |
| 54 | + existsSyncImpl?: (path: string) => boolean; |
| 55 | + mkdtempSyncImpl?: (prefix: string) => string; |
| 56 | + rmSyncImpl?: (path: string, options?: { recursive?: boolean; force?: boolean }) => void; |
| 57 | + tmpdirFn?: () => string; |
| 58 | + log?: (message?: string) => void; |
| 59 | + error?: (message?: string) => void; |
| 60 | + exit?: (code: number) => never; |
| 61 | +} |
| 62 | + |
| 63 | +export function runUninstallCommand(deps: RunUninstallCommandDeps): never { |
| 64 | + const log = deps.log ?? console.log; |
| 65 | + const error = deps.error ?? console.error; |
| 66 | + const exit = deps.exit ?? ((code: number) => process.exit(code)); |
| 67 | + const existsSyncImpl = deps.existsSyncImpl ?? fs.existsSync; |
| 68 | + const mkdtempSyncImpl = deps.mkdtempSyncImpl ?? fs.mkdtempSync; |
| 69 | + const rmSyncImpl = deps.rmSyncImpl ?? fs.rmSync; |
| 70 | + const tmpdirFn = deps.tmpdirFn ?? os.tmpdir; |
| 71 | + |
| 72 | + const localScript = resolveUninstallScript( |
| 73 | + [path.join(deps.rootDir, "uninstall.sh"), path.join(deps.currentDir, "..", "uninstall.sh")], |
| 74 | + existsSyncImpl, |
| 75 | + ); |
| 76 | + if (localScript) { |
| 77 | + log(` Running local uninstall script: ${localScript}`); |
| 78 | + const result = deps.spawnSyncImpl("bash", [localScript, ...deps.args], { |
| 79 | + stdio: "inherit", |
| 80 | + cwd: deps.rootDir, |
| 81 | + env: deps.env, |
| 82 | + }); |
| 83 | + return exitWithSpawnResult(result, exit); |
| 84 | + } |
| 85 | + |
| 86 | + log(` Local uninstall script not found; falling back to ${deps.remoteScriptUrl}`); |
| 87 | + const uninstallDir = mkdtempSyncImpl(path.join(tmpdirFn(), "nemoclaw-uninstall-")); |
| 88 | + const uninstallScript = path.join(uninstallDir, "uninstall.sh"); |
| 89 | + let result: Pick<SpawnSyncReturns<string>, "status" | "signal"> | undefined; |
| 90 | + let downloadFailed = false; |
| 91 | + try { |
| 92 | + try { |
| 93 | + deps.execFileSyncImpl("curl", ["-fsSL", deps.remoteScriptUrl, "-o", uninstallScript], { |
| 94 | + stdio: "inherit", |
| 95 | + }); |
| 96 | + } catch { |
| 97 | + error(` Failed to download uninstall script from ${deps.remoteScriptUrl}`); |
| 98 | + downloadFailed = true; |
| 99 | + } |
| 100 | + if (!downloadFailed) { |
| 101 | + result = deps.spawnSyncImpl("bash", [uninstallScript, ...deps.args], { |
| 102 | + stdio: "inherit", |
| 103 | + cwd: deps.rootDir, |
| 104 | + env: deps.env, |
| 105 | + }); |
| 106 | + } |
| 107 | + } finally { |
| 108 | + rmSyncImpl(uninstallDir, { recursive: true, force: true }); |
| 109 | + } |
| 110 | + if (downloadFailed) { |
| 111 | + return exit(1); |
| 112 | + } |
| 113 | + return exitWithSpawnResult(result || { status: 1, signal: null }, exit); |
| 114 | +} |
0 commit comments