upgradr 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/package.json +1 -1
- package/src/checker.ts +27 -6
- package/src/index.ts +12 -6
- package/src/notice.ts +4 -0
package/README.md
CHANGED
|
@@ -59,7 +59,11 @@ notice.then((msg) => {
|
|
|
59
59
|
|
|
60
60
|
```ts
|
|
61
61
|
const info = await updater.checkForUpdate();
|
|
62
|
-
if (info.
|
|
62
|
+
if (info.error) {
|
|
63
|
+
// The check didn't complete (offline, rate-limited, registry down, …) —
|
|
64
|
+
// this is distinct from "confirmed up to date".
|
|
65
|
+
console.error(`Update check failed: ${info.error}`);
|
|
66
|
+
} else if (info.hasUpdate) {
|
|
63
67
|
console.log(`Update available: ${info.currentVersion} → ${info.latestVersion}`);
|
|
64
68
|
if (info.changelog) console.log(info.changelog);
|
|
65
69
|
} else if (info.aheadOfLatest) {
|
|
@@ -74,7 +78,11 @@ if (info.hasUpdate) {
|
|
|
74
78
|
```ts
|
|
75
79
|
const result = await updater.upgrade(); // always performs a fresh check first
|
|
76
80
|
|
|
77
|
-
if (!result.
|
|
81
|
+
if (!result.success && !result.performed && result.error) {
|
|
82
|
+
// The check couldn't complete — don't claim "up to date".
|
|
83
|
+
console.error(result.error);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
} else if (!result.hasUpdate) {
|
|
78
86
|
console.log(`Already up to date (v${result.from})`);
|
|
79
87
|
} else if (result.method === "local-dev") {
|
|
80
88
|
console.log("Running from source. Use `git pull && bun install` to update.");
|
package/package.json
CHANGED
package/src/checker.ts
CHANGED
|
@@ -9,6 +9,12 @@ export interface UpdateInfo {
|
|
|
9
9
|
/** `current > latest` — running ahead of the published release (dev build). */
|
|
10
10
|
aheadOfLatest: boolean;
|
|
11
11
|
changelog?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Set when the latest version could not be determined (registry non-OK,
|
|
14
|
+
* network failure, timeout, or offline). When present, `hasUpdate` is `false`
|
|
15
|
+
* because the check did not complete — not because the CLI is current.
|
|
16
|
+
*/
|
|
17
|
+
error?: string;
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
/** The persisted result of a previous check (see `cache.ts`). */
|
|
@@ -24,24 +30,28 @@ export function isNewerVersion(current: string, latest: string): boolean {
|
|
|
24
30
|
return Bun.semver.order(current, latest) === -1;
|
|
25
31
|
}
|
|
26
32
|
|
|
27
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Fetch the latest published version from the npm registry. Returns `null` when
|
|
35
|
+
* the version could not be determined (registry non-OK, network failure,
|
|
36
|
+
* timeout, or offline) so callers can distinguish "confirmed current" from
|
|
37
|
+
* "couldn't check".
|
|
38
|
+
*/
|
|
28
39
|
export async function fetchLatestVersion(opts: {
|
|
29
40
|
packageName: string;
|
|
30
|
-
currentVersion: string;
|
|
31
41
|
fetchImpl?: FetchLike;
|
|
32
42
|
signal?: AbortSignal;
|
|
33
|
-
}): Promise<string> {
|
|
43
|
+
}): Promise<string | null> {
|
|
34
44
|
const doFetch = opts.fetchImpl ?? fetch;
|
|
35
45
|
try {
|
|
36
46
|
const res = await doFetch(
|
|
37
47
|
`https://registry.npmjs.org/${opts.packageName}/latest`,
|
|
38
48
|
{ signal: opts.signal },
|
|
39
49
|
);
|
|
40
|
-
if (!res.ok) return
|
|
50
|
+
if (!res.ok) return null;
|
|
41
51
|
const data = (await res.json()) as { version: string };
|
|
42
52
|
return data.version;
|
|
43
53
|
} catch {
|
|
44
|
-
return
|
|
54
|
+
return null;
|
|
45
55
|
}
|
|
46
56
|
}
|
|
47
57
|
|
|
@@ -98,10 +108,21 @@ export async function checkForUpdate(opts: {
|
|
|
98
108
|
}): Promise<UpdateInfo> {
|
|
99
109
|
const latestVersion = await fetchLatestVersion({
|
|
100
110
|
packageName: opts.packageName,
|
|
101
|
-
currentVersion: opts.currentVersion,
|
|
102
111
|
fetchImpl: opts.fetchImpl,
|
|
103
112
|
signal: opts.signal,
|
|
104
113
|
});
|
|
114
|
+
|
|
115
|
+
if (latestVersion === null) {
|
|
116
|
+
// The check did not complete — surface it rather than reporting "current".
|
|
117
|
+
return {
|
|
118
|
+
currentVersion: opts.currentVersion,
|
|
119
|
+
latestVersion: opts.currentVersion,
|
|
120
|
+
hasUpdate: false,
|
|
121
|
+
aheadOfLatest: false,
|
|
122
|
+
error: `Could not determine the latest version of "${opts.packageName}" from the npm registry.`,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
105
126
|
const hasUpdate = isNewerVersion(opts.currentVersion, latestVersion);
|
|
106
127
|
const aheadOfLatest = isNewerVersion(latestVersion, opts.currentVersion);
|
|
107
128
|
|
package/src/index.ts
CHANGED
|
@@ -122,12 +122,6 @@ export function createUpdater(config: UpdaterConfig): Updater {
|
|
|
122
122
|
// Always re-check: the background cache may be fresh but stale relative
|
|
123
123
|
// to a release that shipped since the last check.
|
|
124
124
|
const info = await checkFresh();
|
|
125
|
-
await saveCache(cfg.cacheDir, {
|
|
126
|
-
lastCheckAt: new Date().toISOString(),
|
|
127
|
-
latestVersion: info.latestVersion,
|
|
128
|
-
hasUpdate: info.hasUpdate,
|
|
129
|
-
changelog: info.changelog,
|
|
130
|
-
});
|
|
131
125
|
|
|
132
126
|
const method = detectInstallMethodImpl({
|
|
133
127
|
localDevEntry: cfg.localDevEntry,
|
|
@@ -139,6 +133,18 @@ export function createUpdater(config: UpdaterConfig): Updater {
|
|
|
139
133
|
hasUpdate: info.hasUpdate,
|
|
140
134
|
};
|
|
141
135
|
|
|
136
|
+
// The check itself failed — don't cache a bogus result or report success.
|
|
137
|
+
if (info.error) {
|
|
138
|
+
return { ...base, performed: false, success: false, error: info.error };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
await saveCache(cfg.cacheDir, {
|
|
142
|
+
lastCheckAt: new Date().toISOString(),
|
|
143
|
+
latestVersion: info.latestVersion,
|
|
144
|
+
hasUpdate: info.hasUpdate,
|
|
145
|
+
changelog: info.changelog,
|
|
146
|
+
});
|
|
147
|
+
|
|
142
148
|
if (!info.hasUpdate) {
|
|
143
149
|
return { ...base, performed: false, success: true };
|
|
144
150
|
}
|
package/src/notice.ts
CHANGED
|
@@ -83,6 +83,10 @@ export async function maybeBackgroundNotice(
|
|
|
83
83
|
signal: controller.signal,
|
|
84
84
|
});
|
|
85
85
|
|
|
86
|
+
// Only cache a completed check — a failed check must not suppress the
|
|
87
|
+
// next attempt (nor be silently reported as "up to date").
|
|
88
|
+
if (info.error) return null;
|
|
89
|
+
|
|
86
90
|
const newCache: UpdateCache = {
|
|
87
91
|
lastCheckAt: new Date().toISOString(),
|
|
88
92
|
latestVersion: info.latestVersion,
|