Skip to content

Commit

Permalink
fix: use deno's semver to check if update available (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
jared-paperspace authored Mar 14, 2023
1 parent 4f1ce22 commit b603bd9
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 41 deletions.
3 changes: 2 additions & 1 deletion commands/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app, completion, fmt, version } from "../zcli.ts";
import { semver } from "../deps.ts";
import { VERSION } from "../version.ts";
import { AppError } from "../errors.ts";
import { logger } from "../logger.ts";
Expand Down Expand Up @@ -126,7 +127,7 @@ export const root = app
const latestVersion = await getLatestVersion(ctx);
const currentVersion = VERSION;

if (currentVersion === latestVersion) {
if (semver.gte(currentVersion, latestVersion)) {
return;
}

Expand Down
102 changes: 64 additions & 38 deletions commands/upgrade/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { unzip } from "../../lib/unzip.ts";
import { logger } from "../../logger.ts";
import { path } from "../../deps.ts";
import { cache } from "../../cache.ts";
import { AppError } from "../../errors.ts";

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

const response = await fetch(
"https://api.github.com/repos/paperspace/cli/releases/latest",
"https://api.github.com/repos/paperspace/cli/releases",
);

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

const json = githubReleaseSchema.parse(await response.json());

await cache.set(
"updateAvailable",
{
version: json.tag_name,
assets: json.assets.map((asset) => ({
name: asset.name,
url: asset.browser_download_url,
})),
},
60 * 60 * 8, // 8 hours
);

logger.info(`Found the latest version: ${json.tag_name}`);
return json.tag_name;
const latest = json.find((release) => !release.prerelease);

if (latest) {
await cache.set(
"updateAvailable",
{
version: latest.tag_name,
assets: latest.assets.map((asset) => ({
name: asset.name,
url: asset.browser_download_url,
})),
},
60 * 60 * 8, // 8 hours
);

logger.info(`Found the latest version: ${latest.tag_name}`);
return latest.tag_name;
} else {
logger.error(
`Failed to find the latest version in the response: ${json}`,
);

throw new AppError({
message: "Failed to find the latest version.",
exitCode: 1,
});
}
}

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

const response = await fetch(
"https://api.github.com/repos/paperspace/cli/releases/latest",
"https://api.github.com/repos/paperspace/cli/releases",
);

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

const json = githubReleaseSchema.parse(await response.json());

await cache.set(
"updateAvailable",
{
version: json.tag_name,
assets: json.assets.map((asset) => ({
name: asset.name,
url: asset.browser_download_url,
})),
},
60 * 60 * 8, // 8 hours
);

logger.info(`Found the latest version: ${json.tag_name}`);

return json.assets.map((asset) => ({
name: asset.name,
url: asset.browser_download_url,
}));
const latest = json.find((release) => !release.prerelease);

if (latest) {
await cache.set(
"updateAvailable",
{
version: latest.tag_name,
assets: latest.assets.map((asset) => ({
name: asset.name,
url: asset.browser_download_url,
})),
},
60 * 60 * 8, // 8 hours
);

logger.info(`Found the latest version: ${latest.tag_name}`);

return latest.assets.map((asset) => ({
name: asset.name,
url: asset.browser_download_url,
}));
} else {
logger.error(
`Failed to find the latest version in the response: ${json}`,
);

throw new AppError({
message: "Failed to find the latest version.",
exitCode: 1,
});
}
}

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

const githubReleaseSchema = z.object({
const githubReleaseSchema = z.array(z.object({
tag_name: z.string(),
assets: z.array(
z.object({
name: z.string(),
browser_download_url: z.string(),
}),
),
});
prerelease: z.boolean(),
}));
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { format as formatBytes } from "https://deno.land/[email protected]/fmt/bytes.t
export * as YAML from "https://deno.land/[email protected]/encoding/yaml.ts";
export * as TOML from "https://deno.land/[email protected]/encoding/toml.ts";
export * as JSONc from "https://deno.land/[email protected]/encoding/jsonc.ts";
export * as semver from "https://deno.land/[email protected]/semver/mod.ts";

export * as Sentry from "https://deno.land/x/[email protected]/main.ts";
export { ms } from "https://deno.land/x/[email protected]/ms.ts";
Expand Down
30 changes: 28 additions & 2 deletions test/upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,32 @@ describe("pspace upgrade", () => {
function mockReleaseApi(config: { version?: string } = {}) {
const { version = "v1.0.0" } = config;

return jsonOk({
return jsonOk([{
tag_name: version,
assets: [
{
name: "pspace-linux.zip",
browser_download_url:
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-linux.zip`,
},
{
name: "pspace-macos.zip",
browser_download_url:
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-macos.zip`,
},
{
name: "pspace-macos-arm.zip",
browser_download_url:
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-macos-arm.zip`,
},
{
name: "pspace-windows.zip",
browser_download_url:
`https://raw.github.com/paperspace/cli/${version}-beta/pspace-windows.zip`,
},
],
prerelease: true,
}, {
tag_name: version,
assets: [
{
Expand All @@ -467,7 +492,8 @@ function mockReleaseApi(config: { version?: string } = {}) {
`https://raw.github.com/paperspace/cli/${version}/pspace-windows.zip`,
},
],
});
prerelease: false,
}]);
}

function mergeContext(
Expand Down

0 comments on commit b603bd9

Please sign in to comment.