Skip to content

Commit

Permalink
Merge pull request #20 from letsila/trazafinirina/flu-26-update-checker
Browse files Browse the repository at this point in the history
feat: implement update checker
  • Loading branch information
tsirysndr authored Jan 19, 2024
2 parents 44061d3 + 74297f9 commit fd9b637
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 5 additions & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -219,6 +219,10 @@ export async function main() {
.action(async function () {
await whoami();
})
.globalOption("--check-update <checkUpdate:boolean>", "check for update", {default: true})
.globalAction(async (options: { checkUpdate: boolean }) => {
await checkForUpdate(options)
})
.parse(Deno.args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down
38 changes: 38 additions & 0 deletions src/cmd/upgrade.ts
Original file line number Diff line number Diff line change
@@ -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<void>}
Expand All @@ -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;
}
32 changes: 32 additions & 0 deletions tests/upgrade.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
});

0 comments on commit fd9b637

Please sign in to comment.