(force: boolean)
| 44 | } |
| 45 | |
| 46 | async function runUpdate(force: boolean): Promise<number> { |
| 47 | const latest = await fetchLatestNpmVersion(PACKAGE_NAME) |
| 48 | if (latest === null) { |
| 49 | console.error("Could not reach the npm registry. Check your network connection and try again.") |
| 50 | return 1 |
| 51 | } |
| 52 | if (compareVersions(VERSION, latest) >= 0) { |
| 53 | console.log(`${PRODUCT_NAME} v${VERSION} is already up to date (latest: v${latest}).`) |
| 54 | return 0 |
| 55 | } |
| 56 | console.log(`Updating ${PRODUCT_NAME} v${VERSION} → v${latest}…`) |
| 57 | const global = isGlobalInstall() |
| 58 | if (!global && !force) { |
| 59 | const root = await getGlobalInstallRoot() |
| 60 | console.error( |
| 61 | `This CLI was not installed globally (entrypoint = ${process.argv[1] || import.meta.url || "<unknown>"}).\n` + |
| 62 | `To update a local/dev install, run \`npm install -g ${PACKAGE_NAME}@latest\` manually, or \`npm install\` inside the source checkout.` + |
| 63 | (root ? `\nGlobal install root detected at: ${root}` : ""), |
| 64 | ) |
| 65 | return 1 |
| 66 | } |
| 67 | if (!global && force) { |
| 68 | const root = await getGlobalInstallRoot() |
| 69 | console.warn( |
| 70 | `Warning: forcing global install even though this CLI doesn't look like a global install.` + |
| 71 | (root ? ` Detected global root: ${root}.` : ""), |
| 72 | ) |
| 73 | } |
| 74 | clearUpdateCache() |
| 75 | const code = await runNpmUpdate(PACKAGE_NAME) |
| 76 | if (code === 0) { |
| 77 | console.log(`Updated to v${latest}. Run \`${PRODUCT_NAME.split(" ")[0].toLowerCase()}\` again to use it.`) |
| 78 | } |
| 79 | return code |
| 80 | } |
| 81 | |
| 82 | /** Pop `flag <value>` (accepting -flag and --flag) out of args; returns the value. */ |
| 83 | function takeFlagValue(args: string[], name: string): string | undefined { |
no test coverage detected