Skip to content

Commit b603bd9

Browse files
fix: use deno's semver to check if update available (#32)
1 parent 4f1ce22 commit b603bd9

File tree

5 files changed

+96
-41
lines changed

5 files changed

+96
-41
lines changed

commands/mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { app, completion, fmt, version } from "../zcli.ts";
2+
import { semver } from "../deps.ts";
23
import { VERSION } from "../version.ts";
34
import { AppError } from "../errors.ts";
45
import { logger } from "../logger.ts";
@@ -126,7 +127,7 @@ export const root = app
126127
const latestVersion = await getLatestVersion(ctx);
127128
const currentVersion = VERSION;
128129

129-
if (currentVersion === latestVersion) {
130+
if (semver.gte(currentVersion, latestVersion)) {
130131
return;
131132
}
132133

commands/upgrade/mod.ts

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { unzip } from "../../lib/unzip.ts";
44
import { logger } from "../../logger.ts";
55
import { path } from "../../deps.ts";
66
import { cache } from "../../cache.ts";
7+
import { AppError } from "../../errors.ts";
78

89
/**
910
* This variable is automatically generated by `zcli add`. Do not remove this
@@ -105,7 +106,7 @@ export async function getLatestVersion(ctx: Context, options: {
105106
logger.info("Checking for the latest version...");
106107

107108
const response = await fetch(
108-
"https://api.github.com/repos/paperspace/cli/releases/latest",
109+
"https://api.github.com/repos/paperspace/cli/releases",
109110
);
110111

111112
if (!response.ok) {
@@ -114,21 +115,33 @@ export async function getLatestVersion(ctx: Context, options: {
114115
}
115116

116117
const json = githubReleaseSchema.parse(await response.json());
117-
118-
await cache.set(
119-
"updateAvailable",
120-
{
121-
version: json.tag_name,
122-
assets: json.assets.map((asset) => ({
123-
name: asset.name,
124-
url: asset.browser_download_url,
125-
})),
126-
},
127-
60 * 60 * 8, // 8 hours
128-
);
129-
130-
logger.info(`Found the latest version: ${json.tag_name}`);
131-
return json.tag_name;
118+
const latest = json.find((release) => !release.prerelease);
119+
120+
if (latest) {
121+
await cache.set(
122+
"updateAvailable",
123+
{
124+
version: latest.tag_name,
125+
assets: latest.assets.map((asset) => ({
126+
name: asset.name,
127+
url: asset.browser_download_url,
128+
})),
129+
},
130+
60 * 60 * 8, // 8 hours
131+
);
132+
133+
logger.info(`Found the latest version: ${latest.tag_name}`);
134+
return latest.tag_name;
135+
} else {
136+
logger.error(
137+
`Failed to find the latest version in the response: ${json}`,
138+
);
139+
140+
throw new AppError({
141+
message: "Failed to find the latest version.",
142+
exitCode: 1,
143+
});
144+
}
132145
}
133146

134147
logger.info(`Returning the latest version from cache: ${cached.version}`);
@@ -153,7 +166,7 @@ export async function getLatestVersionAssets(
153166
logger.info("Checking for the latest version...");
154167

155168
const response = await fetch(
156-
"https://api.github.com/repos/paperspace/cli/releases/latest",
169+
"https://api.github.com/repos/paperspace/cli/releases",
157170
);
158171

159172
if (!response.ok) {
@@ -162,25 +175,37 @@ export async function getLatestVersionAssets(
162175
}
163176

164177
const json = githubReleaseSchema.parse(await response.json());
165-
166-
await cache.set(
167-
"updateAvailable",
168-
{
169-
version: json.tag_name,
170-
assets: json.assets.map((asset) => ({
171-
name: asset.name,
172-
url: asset.browser_download_url,
173-
})),
174-
},
175-
60 * 60 * 8, // 8 hours
176-
);
177-
178-
logger.info(`Found the latest version: ${json.tag_name}`);
179-
180-
return json.assets.map((asset) => ({
181-
name: asset.name,
182-
url: asset.browser_download_url,
183-
}));
178+
const latest = json.find((release) => !release.prerelease);
179+
180+
if (latest) {
181+
await cache.set(
182+
"updateAvailable",
183+
{
184+
version: latest.tag_name,
185+
assets: latest.assets.map((asset) => ({
186+
name: asset.name,
187+
url: asset.browser_download_url,
188+
})),
189+
},
190+
60 * 60 * 8, // 8 hours
191+
);
192+
193+
logger.info(`Found the latest version: ${latest.tag_name}`);
194+
195+
return latest.assets.map((asset) => ({
196+
name: asset.name,
197+
url: asset.browser_download_url,
198+
}));
199+
} else {
200+
logger.error(
201+
`Failed to find the latest version in the response: ${json}`,
202+
);
203+
204+
throw new AppError({
205+
message: "Failed to find the latest version.",
206+
exitCode: 1,
207+
});
208+
}
184209
}
185210

186211
logger.info(`Returning the latest version from cache: ${cached.version}`);
@@ -201,12 +226,13 @@ export const _internals = {
201226
getLatestVersionAssets,
202227
};
203228

204-
const githubReleaseSchema = z.object({
229+
const githubReleaseSchema = z.array(z.object({
205230
tag_name: z.string(),
206231
assets: z.array(
207232
z.object({
208233
name: z.string(),
209234
browser_download_url: z.string(),
210235
}),
211236
),
212-
});
237+
prerelease: z.boolean(),
238+
}));

deno.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export { format as formatBytes } from "https://deno.land/std@0.179.0/fmt/bytes.t
1717
export * as YAML from "https://deno.land/std@0.179.0/encoding/yaml.ts";
1818
export * as TOML from "https://deno.land/std@0.179.0/encoding/toml.ts";
1919
export * as JSONc from "https://deno.land/std@0.179.0/encoding/jsonc.ts";
20+
export * as semver from "https://deno.land/std@0.179.0/semver/mod.ts";
2021

2122
export * as Sentry from "https://deno.land/x/sentry_deno@v0.2.2/main.ts";
2223
export { ms } from "https://deno.land/x/ms@v0.1.0/ms.ts";

test/upgrade.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,32 @@ describe("pspace upgrade", () => {
443443
function mockReleaseApi(config: { version?: string } = {}) {
444444
const { version = "v1.0.0" } = config;
445445

446-
return jsonOk({
446+
return jsonOk([{
447+
tag_name: version,
448+
assets: [
449+
{
450+
name: "pspace-linux.zip",
451+
browser_download_url:
452+
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-linux.zip`,
453+
},
454+
{
455+
name: "pspace-macos.zip",
456+
browser_download_url:
457+
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-macos.zip`,
458+
},
459+
{
460+
name: "pspace-macos-arm.zip",
461+
browser_download_url:
462+
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-macos-arm.zip`,
463+
},
464+
{
465+
name: "pspace-windows.zip",
466+
browser_download_url:
467+
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-windows.zip`,
468+
},
469+
],
470+
prerelease: true,
471+
}, {
447472
tag_name: version,
448473
assets: [
449474
{
@@ -467,7 +492,8 @@ function mockReleaseApi(config: { version?: string } = {}) {
467492
`https://raw.github.com/paperspace/cli/${version}/pspace-windows.zip`,
468493
},
469494
],
470-
});
495+
prerelease: false,
496+
}]);
471497
}
472498

473499
function mergeContext(

0 commit comments

Comments
 (0)