diff --git a/CHANGELOG.md b/CHANGELOG.md index 181a558a7..4234f7b22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ # Change Log +## 2025-04-08 - CLI 0.17.3 + +- feat: Add update prompt on CLI usage [#811](https://github.com/hypermodeinc/modus/pull/811) + ## 2025-04-01 - Runtime 0.17.8 - fix: Update Hypermode api key reading for modus local dev [#805](https://github.com/hypermodeinc/modus/pull/805) diff --git a/cli/src/baseCommand.ts b/cli/src/baseCommand.ts index 984243502..bc3d8d54d 100644 --- a/cli/src/baseCommand.ts +++ b/cli/src/baseCommand.ts @@ -1,4 +1,5 @@ import { Command, Flags } from "@oclif/core"; +import { checkForUpdates } from "./util/updateNotifier.js"; export abstract class BaseCommand extends Command { static baseFlags = { @@ -13,4 +14,13 @@ export abstract class BaseCommand extends Command { hidden: true, }), }; + + async init(): Promise { + await super.init(); + + const cmd = this.id?.split(" ")[0]; + if (cmd !== "version" && !this.argv.includes("--version") && !this.argv.includes("-v")) { + await checkForUpdates(this.config.version); + } + } } diff --git a/cli/src/util/updateNotifier.ts b/cli/src/util/updateNotifier.ts new file mode 100644 index 000000000..475cdf173 --- /dev/null +++ b/cli/src/util/updateNotifier.ts @@ -0,0 +1,32 @@ +import chalk from "chalk"; +import semver from "semver"; +import * as vi from "./versioninfo.js"; +import { isOnline } from "./index.js"; + +/** + * Checks if there's a newer version of the CLI available and prints an update message if needed. + * @param currentVersion Current CLI version + */ +export async function checkForUpdates(currentVersion: string): Promise { + if (!(await isOnline())) { + return; + } + + if (currentVersion.startsWith("v")) { + currentVersion = currentVersion.slice(1); + } + + const latestVersion = await vi.getLatestCliVersion(false); + if (!latestVersion) return; + + const latestVersionNumber = latestVersion.startsWith("v") ? latestVersion.slice(1) : latestVersion; + + if (semver.gt(latestVersionNumber, currentVersion)) { + console.log(); + console.log(chalk.yellow("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")); + console.log(chalk.yellow(`Update available! ${chalk.dim(currentVersion)} → ${chalk.greenBright(latestVersionNumber)}`)); + console.log(chalk.yellow("Run ") + chalk.cyanBright("npm update -g @hypermode/modus-cli") + chalk.yellow(" to update.")); + console.log(chalk.yellow("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")); + console.log(); + } +}