Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Apr 20, 2022
1 parent 9bafbb9 commit ce7bab5
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions src/packages/version-check/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
import { TruffleColors } from "@ganache/colors";
const chalk = require("chalk");

const reAnsiEscapes = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
const wrapWidth = Math.min(120, process.stdout.columns || 0);
const center = (str: string, width: number) => {
const mid = ((width - visibleCharacterLength(str))) / 2;
if (mid < 0) return str;

const left = Math.floor(mid);
const right = Math.ceil(mid);
return " ".repeat(left) + str + " ".repeat(right);
}
const visibleCharacterLength = (str: string) => {
// if the string contains unicode characters we need to count them,
// destructuring the string to get the characters as codePOints
return [...str.replace(reAnsiEscapes, "")].length;
}

import http2 from "http2";
import { semverRegex } from "./semver";

function getUpgradeType(current: string, update: string) {
const [_, major, minor, patch] = current.match(semverRegex).slice(1, 4).map(Number);
const [updateMajor, updateMinor, updatePatch] = update.match(semverRegex).slice(1, 4).map(Number);

return updateMajor !== major ? 'major'
: updateMinor !== minor ? 'minor'
: updatePatch !== patch ? 'patch'
: null;
}

export const logIfUpgradeRequired = (options: {
name: string,
logger: { log: any },
Expand All @@ -44,31 +16,63 @@ export const logIfUpgradeRequired = (options: {
try {
if (current === latest) return false;

function getUpgradeType(current: string, update: string) {
const [_, major, minor, patch] = current.match(semverRegex).slice(1, 4).map(Number);
const [updateMajor, updateMinor, updatePatch] = update.match(semverRegex).slice(1, 4).map(Number);

return updateMajor !== major ? 'major'
: updateMinor !== minor ? 'minor'
: updatePatch !== patch ? 'patch'
: null;
}

const chalk = require("chalk");

const reAnsiEscapes = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
const WRAP_WIDTH = Math.min(120, process.stdout.columns || 0);
const center = (str: string, width: number) => {
const mid = ((width - visibleCharacterLength(str))) / 2;
if (mid < 0) return str;

const left = Math.floor(mid);
const right = Math.ceil(mid);
return " ".repeat(left) + str + " ".repeat(right);
}
const visibleCharacterLength = (str: string) => {
// if the string contains unicode characters we need to count them,
// destructuring the string to get the characters as codePOints
return [...str.replace(reAnsiEscapes, "")].length;
}

// compare the two sem versions, if the latest version is newer than the current version
// log a message to the user
const upgradeType = getUpgradeType(current, latest);
if (!upgradeType) return false;
const line1 = chalk`New {hex("${TruffleColors.porsche}") ${upgradeType}} version of ${name} available! {red ${current}} ⇢ {green ${latest}} `;

const line1 = chalk`New {hex("${TruffleColors.porsche}") ${upgradeType}} version of ${name} available! {hex("${TruffleColors.watermelon}") ${current}} ⇢ {hex("${TruffleColors.green}") ${latest}} `;
const line2 = chalk`{hex("${TruffleColors.porsche}") Changelog:} {hex("${TruffleColors.turquoise}") https://github.com/trufflesuite/${name}/releases/v${latest}}`;
const line3 = chalk`Run {green npm install -g ${name}@${latest}} to update!`;
const line3 = chalk`Run {hex("${TruffleColors.green}") npm install -g ${name}@${latest}} to update!`;
const width = Math.max(visibleCharacterLength(line1), visibleCharacterLength(line2), visibleCharacterLength(line3)) + 4;
const vPipe = chalk`{hex("#C4A000") ║}`;
const wrapWidth = Math.max(width, WRAP_WIDTH);
const vPipe = chalk`{hex("${TruffleColors.yellow}") ║}`;
const hLines = "═".repeat(width);
const emptyLine = center(vPipe + " ".repeat(width) + vPipe, wrapWidth)
const emptyLine = center(vPipe + " ".repeat(width) + vPipe, Math.max(width, wrapWidth))
const message = [""];
message.push(chalk`{hex("#C4A000") ${center("╔" + hLines + "╗", wrapWidth)}}`);
message.push(chalk`{hex("${TruffleColors.yellow}") ${center("╔" + hLines + "╗", wrapWidth)}}`);
message.push(emptyLine);
message.push(center(vPipe + center(line1, width) + vPipe, wrapWidth));
message.push(center(vPipe + center(line2, width) + vPipe, wrapWidth));
message.push(center(vPipe + center(line3, width) + vPipe, wrapWidth));
message.push(emptyLine);
message.push(chalk`{hex("#C4A000") ${center("╚" + hLines + "╝", wrapWidth)}}`);
message.push(chalk`{hex("${TruffleColors.yellow}") ${center("╚" + hLines + "╝", wrapWidth)}}`);
message.push("");
logger.log(message.join("\n"));

return true;
} catch (e) {
console.error(e);
} catch {
// If we fail to tell the user about an update it is unfortunate, but not
// the end of the world, so swallow the error and continue.
return false;
}
} else {
return false;
Expand All @@ -80,7 +84,6 @@ export const getLatestVersionNumber = (name: "ganache" | "truffle") => {
// The `http2.connect` method creates a new session with example.com
const session = http2.connect('https://version.trufflesuite.com/');


// If there is any error in connecting, log it to the console
session.on('error', (err) => reject(err));

Expand Down

0 comments on commit ce7bab5

Please sign in to comment.