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
1 change: 1 addition & 0 deletions src/dev/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { walk, WalkError } from "https://deno.land/std@0.193.0/fs/walk.ts";
export { parse } from "https://deno.land/std@0.193.0/flags/mod.ts";
export { gte } from "https://deno.land/std@0.193.0/semver/mod.ts";
export { existsSync } from "https://deno.land/std@0.193.0/fs/mod.ts";
export * as semver from "https://deno.land/std@0.195.0/semver/mod.ts";

// ts-morph
export { Node, Project } from "https://deno.land/x/ts_morph@17.0.1/mod.ts";
27 changes: 19 additions & 8 deletions src/dev/update_check.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors, join } from "./deps.ts";
import { colors, join, semver } from "./deps.ts";

export interface CheckFile {
last_checked: string;
Expand Down Expand Up @@ -45,10 +45,18 @@ async function fetchLatestVersion() {
throw new Error(`Could not fetch latest version.`);
}

async function readCurrentVersion() {
const versions = (await import("../../versions.json", {
"assert": { type: "json" },
})).default as string[];
return versions[0];
}

export async function updateCheck(
interval: number,
getCacheDir = getFreshCacheDir,
getLatestVersion = fetchLatestVersion,
getCurrentVersion = readCurrentVersion,
) {
// Skip update checks on CI or Deno Deploy
if (
Expand All @@ -70,13 +78,11 @@ export async function updateCheck(
}
}

const versions = (await import("../../versions.json", {
"assert": { type: "json" },
})).default as string[];
const version = await getCurrentVersion();

let checkFile: CheckFile = {
current_version: versions[0],
latest_version: versions[0],
current_version: version,
latest_version: version,
last_checked: new Date(0).toISOString(),
};
try {
Expand All @@ -89,7 +95,7 @@ export async function updateCheck(
}

// Update current version
checkFile.current_version = versions[0];
checkFile.current_version = version;

// Only check in the specificed interval
if (Date.now() >= new Date(checkFile.last_checked).getTime() + interval) {
Expand All @@ -105,7 +111,12 @@ export async function updateCheck(
}
}

if (checkFile.current_version !== checkFile.latest_version) {
// Only show update message if current version is smaller than latest
const currentVersion = semver.parse(checkFile.current_version);
const latestVersion = semver.parse(checkFile.latest_version);
if (
semver.lt(currentVersion, latestVersion)
) {
const current = colors.bold(colors.rgb8(checkFile.current_version, 208));
const latest = colors.bold(colors.rgb8(checkFile.latest_version, 121));
console.log(
Expand Down
33 changes: 33 additions & 0 deletions tests/cli_update_check_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,36 @@ Deno.test({
await Deno.remove(tmpDirName, { recursive: true });
},
});

Deno.test({
name: "only shows update message when current < latest",
async fn() {
const tmpDirName = await Deno.makeTempDir();

const checkFile: CheckFile = {
current_version: "9999.999.0",
latest_version: "1.2.0",
last_checked: new Date().toISOString(),
};

await Deno.writeTextFile(
join(tmpDirName, "latest.json"),
JSON.stringify(checkFile, null, 2),
);

const out = await new Deno.Command(Deno.execPath(), {
args: ["run", "-A", "./tests/fixture_update_check/mod.ts"],
env: {
HOME: tmpDirName,
LATEST_VERSION: versions[0],
CURRENT_VERSION: "99999.9999.00",
},
}).output();

const decoder = new TextDecoder();
const stdout = colors.stripColor(decoder.decode(out.stdout));
assertNotMatch(stdout, /Fresh .* is available/);

await Deno.remove(tmpDirName, { recursive: true });
},
});
12 changes: 11 additions & 1 deletion tests/fixture_update_check/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ async function getLatestVersion() {
return Deno.env.get("LATEST_VERSION") ?? "99.99.999";
}

// deno-lint-ignore require-await
async function getCurrentVersion() {
return Deno.env.get("CURRENT_VERSION")!;
}

const interval = +(Deno.env.get("UPDATE_INTERVAL") ?? 1000);
await updateCheck(interval, () => Deno.env.get("HOME")!, getLatestVersion);
await updateCheck(
interval,
() => Deno.env.get("HOME")!,
getLatestVersion,
Deno.env.has("CURRENT_VERSION") ? getCurrentVersion : undefined,
);