newpr 0.5.1 → 0.5.2
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/package.json +1 -1
- package/src/cli/update-check.ts +20 -19
package/package.json
CHANGED
package/src/cli/update-check.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const PACKAGE_NAME = "newpr";
|
|
2
|
-
const CHECK_INTERVAL_MS = 4 * 60 * 60 * 1000;
|
|
3
2
|
|
|
4
3
|
interface UpdateInfo {
|
|
5
4
|
current: string;
|
|
@@ -7,27 +6,32 @@ interface UpdateInfo {
|
|
|
7
6
|
needsUpdate: boolean;
|
|
8
7
|
}
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
interface CachedCheck {
|
|
10
|
+
latest: string;
|
|
11
|
+
checkedAt: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function readCache(): Promise<CachedCheck | null> {
|
|
11
15
|
try {
|
|
12
|
-
const file = Bun.file(`${process.env.HOME}/.newpr/
|
|
13
|
-
|
|
14
|
-
return
|
|
16
|
+
const file = Bun.file(`${process.env.HOME}/.newpr/update-cache.json`);
|
|
17
|
+
if (!(await file.exists())) return null;
|
|
18
|
+
return JSON.parse(await file.text()) as CachedCheck;
|
|
15
19
|
} catch {
|
|
16
|
-
return
|
|
20
|
+
return null;
|
|
17
21
|
}
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
async function
|
|
24
|
+
async function writeCache(latest: string): Promise<void> {
|
|
21
25
|
const dir = `${process.env.HOME}/.newpr`;
|
|
22
26
|
const { mkdirSync } = await import("node:fs");
|
|
23
27
|
try { mkdirSync(dir, { recursive: true }); } catch {}
|
|
24
|
-
await Bun.write(`${dir}/
|
|
28
|
+
await Bun.write(`${dir}/update-cache.json`, JSON.stringify({ latest, checkedAt: Date.now() }));
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
async function fetchLatestVersion(): Promise<string | null> {
|
|
28
32
|
try {
|
|
29
33
|
const controller = new AbortController();
|
|
30
|
-
const timeout = setTimeout(() => controller.abort(),
|
|
34
|
+
const timeout = setTimeout(() => controller.abort(), 5000);
|
|
31
35
|
const res = await fetch(`https://registry.npmjs.org/${PACKAGE_NAME}/latest`, {
|
|
32
36
|
signal: controller.signal,
|
|
33
37
|
headers: { Accept: "application/json" },
|
|
@@ -53,23 +57,20 @@ function compareVersions(current: string, latest: string): boolean {
|
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
export async function checkForUpdate(currentVersion: string): Promise<UpdateInfo | null> {
|
|
56
|
-
const lastCheck = await getLastCheckTime();
|
|
57
|
-
if (Date.now() - lastCheck < CHECK_INTERVAL_MS) return null;
|
|
58
|
-
|
|
59
60
|
const latest = await fetchLatestVersion();
|
|
60
|
-
await
|
|
61
|
-
|
|
62
|
-
if (!latest) return null;
|
|
63
|
-
if (!compareVersions(currentVersion, latest)) return null;
|
|
61
|
+
if (latest) await writeCache(latest);
|
|
64
62
|
|
|
65
|
-
|
|
63
|
+
const version = latest ?? (await readCache())?.latest;
|
|
64
|
+
if (!version) return null;
|
|
65
|
+
if (!compareVersions(currentVersion, version)) return null;
|
|
66
|
+
return { current: currentVersion, latest: version, needsUpdate: true };
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
export function printUpdateNotice(info: UpdateInfo): void {
|
|
69
70
|
const msg = [
|
|
70
71
|
"",
|
|
71
|
-
` Update available: ${info.current} → \x1b[32m${info.latest}\x1b[0m`,
|
|
72
|
-
`
|
|
72
|
+
` \x1b[33m⚡\x1b[0m Update available: \x1b[2m${info.current}\x1b[0m → \x1b[32m${info.latest}\x1b[0m`,
|
|
73
|
+
` Run \x1b[36mbun add -g ${PACKAGE_NAME}\x1b[0m to update`,
|
|
73
74
|
"",
|
|
74
75
|
].join("\n");
|
|
75
76
|
process.stderr.write(msg);
|