-
Notifications
You must be signed in to change notification settings - Fork 139
feat(cli): notify when a newer published version is available #1402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Chase J (chajac)
wants to merge
4
commits into
main
Choose a base branch
from
chajac/cli-version-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
24041b1
feat(cli): notify when a newer version is published
chajac 9e1a5c5
chore(cli): add changeset for update notice
chajac 9e9c0bb
refactor(cli): drop lint suppression and trim update-check comments
chajac 7093914
fix(cli): keep a failed update notice from failing the command
chajac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@qawolf/cli": minor | ||
| --- | ||
|
|
||
| The CLI now checks the npm registry for a newer published version while a command runs. After the command completes, the CLI shows an update notice one time for each new version. Human mode shows a styled note. Agent mode writes plain text to stderr. JSON mode writes a note diagnostic to stderr and does not change stdout. Set QAWOLF_NO_UPDATE_CHECK=1 to disable the check. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export const updateCheckMessages = { | ||
| title: "Update available", | ||
| body: (current: string, latest: string): string => | ||
| `@qawolf/cli ${current} → ${latest}\nRun \`npm install -g @qawolf/cli\` to update.`, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
|
|
||
| import { isNewerVersion } from "./version.js"; | ||
|
|
||
| describe("isNewerVersion", () => { | ||
| it("detects newer patch, minor, and major releases", () => { | ||
| expect(isNewerVersion("1.3.2", "1.3.3")).toBe(true); | ||
| expect(isNewerVersion("1.3.2", "1.4.0")).toBe(true); | ||
| expect(isNewerVersion("1.3.2", "2.0.0")).toBe(true); | ||
| }); | ||
|
|
||
| it("compares numerically, not lexicographically", () => { | ||
| expect(isNewerVersion("1.9.0", "1.10.0")).toBe(true); | ||
| expect(isNewerVersion("1.10.0", "1.9.0")).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false for equal or older versions", () => { | ||
| expect(isNewerVersion("1.3.2", "1.3.2")).toBe(false); | ||
| expect(isNewerVersion("1.3.2", "1.3.1")).toBe(false); | ||
| expect(isNewerVersion("2.0.0", "1.9.9")).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when either version is not a plain release", () => { | ||
| expect(isNewerVersion("1.3.2", "1.4.0-beta.1")).toBe(false); | ||
| expect(isNewerVersion("1.3.2-beta.1", "1.4.0")).toBe(false); | ||
| expect(isNewerVersion("1.3.2", "not-a-version")).toBe(false); | ||
| expect(isNewerVersion("", "1.4.0")).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| type Release = { major: number; minor: number; patch: number }; | ||
|
|
||
| function parseRelease(version: string): Release | undefined { | ||
| const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version); | ||
| if (!match) return undefined; | ||
| return { | ||
| major: Number(match[1]), | ||
| minor: Number(match[2]), | ||
| patch: Number(match[3]), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Only plain `major.minor.patch` versions compare. Prereleases and | ||
| * unparseable input return false, so callers stay silent rather than | ||
| * mis-notify. | ||
| */ | ||
| export function isNewerVersion(current: string, latest: string): boolean { | ||
| const cur = parseRelease(current); | ||
| const next = parseRelease(latest); | ||
| if (cur === undefined || next === undefined) return false; | ||
| if (next.major !== cur.major) return next.major > cur.major; | ||
| if (next.minor !== cur.minor) return next.minor > cur.minor; | ||
| return next.patch > cur.patch; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
|
|
||
| import type { Fs } from "~/shell/fs.js"; | ||
| import { makeMemoryFs } from "~/shell/fs.testUtils.js"; | ||
| import { startUpdateCheck } from "./updateCheck.js"; | ||
|
|
||
| // Let the fetch's .then chain run before reading the settled value. | ||
| const settle = () => new Promise((resolve) => setTimeout(resolve, 0)); | ||
|
|
||
| function makeNotifier(overrides: { | ||
| env?: Record<string, string | undefined>; | ||
| currentVersion?: string; | ||
| fs?: Fs; | ||
| fetchLatestVersion?: () => Promise<string | undefined>; | ||
| renderNotice?: () => void; | ||
| }) { | ||
| const written: { body: string; title: string }[] = []; | ||
| let fetchCalls = 0; | ||
| const notifier = startUpdateCheck({ | ||
| env: overrides.env ?? {}, | ||
| currentVersion: overrides.currentVersion ?? "1.0.0", | ||
| configDir: "/config", | ||
| fs: overrides.fs ?? makeMemoryFs(), | ||
| fetchLatestVersion: () => { | ||
| fetchCalls += 1; | ||
| return ( | ||
| overrides.fetchLatestVersion ?? (() => Promise.resolve("2.0.0")) | ||
| )(); | ||
| }, | ||
| renderNotice: (body, title) => { | ||
| written.push({ body, title }); | ||
| overrides.renderNotice?.(); | ||
| }, | ||
| }); | ||
| return { notifier, written, fetchCalls: () => fetchCalls }; | ||
| } | ||
|
|
||
| describe("startUpdateCheck", () => { | ||
| it("announces a newer version after the fetch settles", async () => { | ||
| const { notifier, written } = makeNotifier({}); | ||
| await settle(); | ||
| await notifier.notifyIfOutdated(); | ||
| expect(written).toHaveLength(1); | ||
| expect(written[0]?.body).toContain("1.0.0 → 2.0.0"); | ||
| expect(written[0]?.title).toBe("Update available"); | ||
| }); | ||
|
|
||
| it("stays silent while the fetch is still pending", async () => { | ||
| const { notifier, written } = makeNotifier({ | ||
| fetchLatestVersion: () => new Promise(() => {}), | ||
| }); | ||
| await notifier.notifyIfOutdated(); | ||
| expect(written).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("stays silent when the published version is not newer", async () => { | ||
| const { notifier, written } = makeNotifier({ | ||
| currentVersion: "2.0.0", | ||
| fetchLatestVersion: () => Promise.resolve("2.0.0"), | ||
| }); | ||
| await settle(); | ||
| await notifier.notifyIfOutdated(); | ||
| expect(written).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("announces each version at most once across runs", async () => { | ||
| const fs = makeMemoryFs(); | ||
| const first = makeNotifier({ fs }); | ||
| await settle(); | ||
| await first.notifier.notifyIfOutdated(); | ||
| expect(first.written).toHaveLength(1); | ||
|
|
||
| const second = makeNotifier({ fs }); | ||
| await settle(); | ||
| await second.notifier.notifyIfOutdated(); | ||
| expect(second.written).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("announces again when an even newer version ships", async () => { | ||
| const fs = makeMemoryFs(); | ||
| const first = makeNotifier({ fs }); | ||
| await settle(); | ||
| await first.notifier.notifyIfOutdated(); | ||
|
|
||
| const second = makeNotifier({ | ||
| fs, | ||
| fetchLatestVersion: () => Promise.resolve("3.0.0"), | ||
| }); | ||
| await settle(); | ||
| await second.notifier.notifyIfOutdated(); | ||
| expect(second.written).toHaveLength(1); | ||
| expect(second.written[0]?.body).toContain("1.0.0 → 3.0.0"); | ||
| }); | ||
|
|
||
| it("does not fetch when QAWOLF_NO_UPDATE_CHECK is set", async () => { | ||
| const { notifier, written, fetchCalls } = makeNotifier({ | ||
| env: { QAWOLF_NO_UPDATE_CHECK: "1" }, | ||
| }); | ||
| await settle(); | ||
| await notifier.notifyIfOutdated(); | ||
| expect(fetchCalls()).toBe(0); | ||
| expect(written).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("still announces (and never throws) when the state file is unwritable", async () => { | ||
| const fs: Fs = { | ||
| ...makeMemoryFs(), | ||
| readFile: () => Promise.reject(new Error("boom")), | ||
| mkdir: () => Promise.reject(new Error("boom")), | ||
| writeFile: () => Promise.reject(new Error("boom")), | ||
| }; | ||
| const { notifier, written } = makeNotifier({ fs }); | ||
| await settle(); | ||
| await notifier.notifyIfOutdated(); | ||
| expect(written).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("does not throw when rendering fails, and retries next run", async () => { | ||
| const fs = makeMemoryFs(); | ||
| const first = makeNotifier({ | ||
| fs, | ||
| renderNotice: () => { | ||
| throw new Error("EPIPE"); | ||
| }, | ||
| }); | ||
| await settle(); | ||
| await first.notifier.notifyIfOutdated(); | ||
|
|
||
| // A failed render must not record the version as announced. | ||
| const second = makeNotifier({ fs }); | ||
| await settle(); | ||
| await second.notifier.notifyIfOutdated(); | ||
| expect(second.written).toHaveLength(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.