diff --git a/deps.ts b/deps.ts index 4c71552..1775eae 100644 --- a/deps.ts +++ b/deps.ts @@ -7,6 +7,7 @@ export { red, brightGreen, gray, + yellow, } from "https://deno.land/std@0.192.0/fmt/colors.ts"; export { z } from "https://deno.land/x/zod@v3.22.2/mod.ts"; export { decompress } from "https://deno.land/x/zip@v1.2.5/mod.ts"; diff --git a/main.ts b/main.ts index ecaa42b..a662bf9 100644 --- a/main.ts +++ b/main.ts @@ -2,7 +2,7 @@ import { Command } from "cliffy/command"; import run from "./src/cmd/run.ts"; import init from "./src/cmd/init.ts"; import search from "./src/cmd/search.ts"; -import upgrade from "./src/cmd/upgrade.ts"; +import upgrade, { checkForUpdate } from "./src/cmd/upgrade.ts"; import listJobs from "./src/cmd/list.ts"; import generateWorkflow from "./src/cmd/github.ts"; import generateGitlabCIConfig from "./src/cmd/gitlab.ts"; @@ -219,6 +219,10 @@ export async function main() { .action(async function () { await whoami(); }) + .globalOption("--check-update ", "check for update", {default: true}) + .globalAction(async (options: { checkUpdate: boolean }) => { + await checkForUpdate(options) + }) .parse(Deno.args); } diff --git a/src/cmd/agent.ts b/src/cmd/agent.ts index cd6e91f..dc65577 100644 --- a/src/cmd/agent.ts +++ b/src/cmd/agent.ts @@ -23,7 +23,7 @@ import { } from "../utils.ts"; import { hostname, release, cpus, arch, totalmem, platform } from "node:os"; import { Agent } from "../types.ts"; -import O from "https://esm.sh/v133/mimic-fn@4.0.0/denonext/mimic-fn.mjs"; +// import O from "https://esm.sh/v133/mimic-fn@4.0.0/denonext/mimic-fn.mjs"; async function startAgent() { console.log(` diff --git a/src/cmd/upgrade.ts b/src/cmd/upgrade.ts index 745f4bd..c8ae486 100644 --- a/src/cmd/upgrade.ts +++ b/src/cmd/upgrade.ts @@ -1,3 +1,6 @@ +import { VERSION } from "../consts.ts"; +import { yellow, green } from "../../deps.ts"; + /** * Upgrades FluentCI by installing the latest version from the Deno registry. * @returns {Promise} @@ -23,3 +26,38 @@ async function upgrade() { } export default upgrade; + +export async function checkForUpdate(options: { checkUpdate: boolean }) { + const { checkUpdate } = options + if (!checkUpdate) { + return + } + + try { + const result = await fetch("https://api.github.com/repos/fluentci-io/fluentci/releases/latest") + const releaseInfo = await result.json() + + if (versionGreaterThan(releaseInfo.tag_name, VERSION)) { + console.log( + `${green('A new release of fluentci is available:')} ${VERSION} → ${releaseInfo.tag_name} \nTo upgrade: run fluentci upgrade\n${releaseInfo.url} + `) + } + } catch (e) { + console.log(` + ${yellow('WARNING: ')} checking for udpate failed ${e} + `) + } +} + +export const versionGreaterThan = (v1: string, v2: string): boolean => { + const numbers1 = v1.replace('v', '').split('.').map(Number) + const numbers2 = v2.replace('v', '').split('.').map(Number) + + for (let i = 0; i < 3; i++) { + if (numbers1[i] > numbers2[i]) { + return true; + } + } + + return false; +} diff --git a/tests/upgrade.test.ts b/tests/upgrade.test.ts new file mode 100644 index 0000000..fcc41d7 --- /dev/null +++ b/tests/upgrade.test.ts @@ -0,0 +1,32 @@ +import { assertEquals } from "https://deno.land/std@0.206.0/assert/mod.ts"; +import { versionGreaterThan } from '../src/cmd/upgrade.ts' + +Deno.test("semver comparasion", () => { + const tests = [ + { + v1: "v0.10.1", + v2: "v0.10.2", + want: false + }, + { + v1: "v0.10.2", + v2: "v0.10.1", + want: true + }, + { + v1: "v2.10.1", + v2: "v0.10.1", + want: true + }, + { + v1: "v0.10.0", + v2: "v0.10.0", + want: false + }, + ] + + tests.forEach((tt) => { + const res = versionGreaterThan(tt.v1, tt.v2) + assertEquals(res, tt.want) + }) +}); \ No newline at end of file