From 190eca6f6e3e78ab9936b826268570c8223ac0da Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Thu, 15 Apr 2021 00:57:15 +0200 Subject: [PATCH] check for mahor version for updates --- package.json | 2 ++ src/handlers/upgrade.ts | 31 +++++++++++++++++++------------ src/index.ts | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 880f591..dd66d3d 100644 --- a/package.json +++ b/package.json @@ -17,12 +17,14 @@ "typescript": "^3.9" }, "dependencies": { + "@types/semver": "^7.3.4", "axios": "^0.19", "clitastic": "^0.1.2", "colors": "^1", "commander": "^6.2", "cron-parser": "2.x.x", "js-yaml": "3.x.x", + "semver": "^7.3.5", "uhrwerk": "1.x.x" } } diff --git a/src/handlers/upgrade.ts b/src/handlers/upgrade.ts index 3c6880b..3098393 100644 --- a/src/handlers/upgrade.ts +++ b/src/handlers/upgrade.ts @@ -2,6 +2,7 @@ import { chmodSync } from 'fs' import axios from 'axios' import { Writer } from 'clitastic' +import semver from 'semver' import { INSTALL_DIR, VERSION } from '..' import { checkIfResticIsAvailable, downloadFile, exec } from '../utils' @@ -18,19 +19,25 @@ export async function upgrade() { responseType: 'json', }) - if (json.tag_name != VERSION) { - const platformMap: { [key: string]: string } = { - darwin: 'macos', + const latest = semver.coerce(json.tag_name) + const current = semver.coerce(VERSION) + if (!latest || !current) throw new Error('Could not parse versions numbers.') + if (semver.gt(latest, current) && semver.major(latest) === semver.major(current)) { + // Update to compatible + if (json.tag_name != VERSION) { + const platformMap: { [key: string]: string } = { + darwin: 'macos', + } + + const name = `autorestic_${platformMap[process.platform] || process.platform}_${process.arch}` + const dl = json.assets.find((asset: any) => asset.name === name) + + const to = INSTALL_DIR + '/autorestic' + w.replaceLn('Downloading binary... 🌎') + await downloadFile(dl.browser_download_url, to) + + chmodSync(to, 0o755) } - - const name = `autorestic_${platformMap[process.platform] || process.platform}_${process.arch}` - const dl = json.assets.find((asset: any) => asset.name === name) - - const to = INSTALL_DIR + '/autorestic' - w.replaceLn('Downloading binary... 🌎') - await downloadFile(dl.browser_download_url, to) - - chmodSync(to, 0o755) } w.done('All up to date! 🚀') diff --git a/src/index.ts b/src/index.ts index 0936d47..7b6d003 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,7 +17,7 @@ import install from './handlers/install' import { uninstall } from './handlers/uninstall' import { upgrade } from './handlers/upgrade' -export const VERSION = '0.27' +export const VERSION = '0.28' export const INSTALL_DIR = '/usr/local/bin' let requireConfig: boolean = true