diff --git a/src/packages/version-check/src/index.ts b/src/packages/version-check/src/index.ts index 7b10ac793c..c6b94bfbc7 100644 --- a/src/packages/version-check/src/index.ts +++ b/src/packages/version-check/src/index.ts @@ -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 }, @@ -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; @@ -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));