Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-update-install-command.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alchemy/cli": patch
---

Fix update notice so `npm i -g @alchemy/cli@latest` is suggested (forces npm to fetch the newest version) and clear the update-check cache on postinstall to prevent stale "update available" messages after upgrading.
18 changes: 17 additions & 1 deletion scripts/postinstall.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env node

const { unlinkSync } = require("node:fs");
const { join } = require("node:path");

function isGlobalInstall() {
return process.env.npm_config_global === "true";
}
Expand All @@ -8,8 +11,21 @@ function isCI() {
return process.env.CI === "true";
}

function clearUpdateCache() {
const home = process.env.HOME || require("node:os").homedir();
const configHome = process.env.XDG_CONFIG_HOME || join(home, ".config");
const cachePath = process.env.ALCHEMY_CONFIG
? process.env.ALCHEMY_CONFIG.replace(/config\.json$/, ".update-check")
: join(configHome, "alchemy", ".update-check");
try {
unlinkSync(cachePath);
} catch {
// Cache file may not exist yet — that's fine.
}
}

if (isGlobalInstall() && !isCI()) {
// Keep this concise so it stays readable in npm install output.
clearUpdateCache();
console.log("");
console.log("◆ Alchemy CLI installed");
console.log(" Run `alchemy` to get started.");
Expand Down
2 changes: 1 addition & 1 deletion src/lib/update-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { esc } from "./colors.js";
declare const __CLI_VERSION__: string;

const CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
const UPDATE_INSTALL_COMMAND = "npm i -g @alchemy/cli";
const UPDATE_INSTALL_COMMAND = "npm i -g @alchemy/cli@latest";

interface UpdateCache {
latest: string;
Expand Down
12 changes: 6 additions & 6 deletions tests/commands/onboarding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("onboarding flow", () => {
vi.doMock("../../src/lib/update-check.js", () => ({
getUpdateNoticeLines: vi.fn().mockReturnValue([
" Update available 0.2.0 -> 9.9.9",
" Run npm i -g @alchemy/cli to update",
" Run npm i -g @alchemy/cli@latest to update",
]),
}));
vi.doMock("../../src/lib/config.js", () => ({
Expand Down Expand Up @@ -60,7 +60,7 @@ describe("onboarding flow", () => {
vi.doMock("../../src/lib/update-check.js", () => ({
getUpdateNoticeLines: vi.fn().mockReturnValue([
" Update available 0.2.0 -> 9.9.9",
" Run npm i -g @alchemy/cli to update",
" Run npm i -g @alchemy/cli@latest to update",
]),
}));
vi.doMock("../../src/lib/config.js", () => ({
Expand Down Expand Up @@ -101,7 +101,7 @@ describe("onboarding flow", () => {
vi.doMock("../../src/lib/update-check.js", () => ({
getUpdateNoticeLines: vi.fn().mockReturnValue([
" Update available 0.2.0 -> 9.9.9",
" Run npm i -g @alchemy/cli to update",
" Run npm i -g @alchemy/cli@latest to update",
]),
}));
vi.doMock("../../src/lib/config.js", () => ({
Expand Down Expand Up @@ -142,7 +142,7 @@ describe("onboarding flow", () => {
vi.doMock("../../src/lib/update-check.js", () => ({
getUpdateNoticeLines: vi.fn().mockReturnValue([
" Update available 0.2.0 -> 9.9.9",
" Run npm i -g @alchemy/cli to update",
" Run npm i -g @alchemy/cli@latest to update",
]),
}));
vi.doMock("../../src/lib/config.js", () => ({
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("onboarding flow", () => {
vi.doMock("../../src/lib/update-check.js", () => ({
getUpdateNoticeLines: vi.fn().mockReturnValue([
" Update available 0.2.0 -> 9.9.9",
" Run npm i -g @alchemy/cli to update",
" Run npm i -g @alchemy/cli@latest to update",
]),
}));
vi.doMock("../../src/lib/config.js", () => ({
Expand Down Expand Up @@ -216,6 +216,6 @@ describe("onboarding flow", () => {
const completed = await runOnboarding({} as never, "9.9.9");
expect(completed).toBe(false);
expect(logSpy).toHaveBeenCalledWith(" Update available 0.2.0 -> 9.9.9");
expect(logSpy).toHaveBeenCalledWith(" Run npm i -g @alchemy/cli to update");
expect(logSpy).toHaveBeenCalledWith(" Run npm i -g @alchemy/cli@latest to update");
});
});
8 changes: 4 additions & 4 deletions tests/commands/update-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("update-check command", () => {
currentVersion: "0.2.0",
latestVersion: "0.3.0",
updateAvailable: true,
installCommand: "npm i -g @alchemy/cli",
installCommand: "npm i -g @alchemy/cli@latest",
checkedAt: 1_700_000_000_000,
}),
}));
Expand All @@ -39,7 +39,7 @@ describe("update-check command", () => {
currentVersion: "0.2.0",
latestVersion: "0.3.0",
updateAvailable: true,
installCommand: "npm i -g @alchemy/cli",
installCommand: "npm i -g @alchemy/cli@latest",
checkedAt: 1_700_000_000_000,
});
});
Expand All @@ -59,7 +59,7 @@ describe("update-check command", () => {
currentVersion: "0.2.0",
latestVersion: "0.3.0",
updateAvailable: true,
installCommand: "npm i -g @alchemy/cli",
installCommand: "npm i -g @alchemy/cli@latest",
checkedAt: 1_700_000_000_000,
}),
}));
Expand All @@ -77,7 +77,7 @@ describe("update-check command", () => {
["Latest version", "0.3.0"],
["Update available", "yes"],
["Checked at", "2023-11-14T22:13:20.000Z"],
["Install", "npm i -g @alchemy/cli"],
["Install", "npm i -g @alchemy/cli@latest"],
]);
});
});
2 changes: 1 addition & 1 deletion tests/e2e/cli.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ describe("CLI mock E2E", () => {
currentVersion: "0.2.0",
latestVersion: "9.9.9",
updateAvailable: true,
installCommand: "npm i -g @alchemy/cli",
installCommand: "npm i -g @alchemy/cli@latest",
checkedAt,
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/update-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("getUpdateStatus", () => {
currentVersion: "0.0.0",
latestVersion: "9.9.9",
updateAvailable: true,
installCommand: "npm i -g @alchemy/cli",
installCommand: "npm i -g @alchemy/cli@latest",
checkedAt,
});
expect(execFileSync).not.toHaveBeenCalled();
Expand All @@ -132,7 +132,7 @@ describe("getUpdateStatus", () => {
currentVersion: "0.0.0",
latestVersion: "9.9.9",
updateAvailable: true,
installCommand: "npm i -g @alchemy/cli",
installCommand: "npm i -g @alchemy/cli@latest",
checkedAt,
});
});
Expand All @@ -151,7 +151,7 @@ describe("getUpdateStatus", () => {
currentVersion: "0.0.0",
latestVersion: null,
updateAvailable: false,
installCommand: "npm i -g @alchemy/cli",
installCommand: "npm i -g @alchemy/cli@latest",
checkedAt: null,
});
});
Expand All @@ -167,7 +167,7 @@ describe("printUpdateNotice", () => {
const output = writeSpy.mock.calls[0][0] as string;
expect(output).toContain("Update available");
expect(output).toContain("1.0.0");
expect(output).toContain("npm i -g @alchemy/cli");
expect(output).toContain("npm i -g @alchemy/cli@latest");

writeSpy.mockRestore();
});
Expand Down
Loading