UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

130 lines 4.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkForNewVersion = exports.getLatestPackageVersion = exports.About = void 0; const fs_1 = require("fs"); const path = require("path"); const semver = require("semver"); const axios_1 = require("axios"); const PACKAGE_NAME = '@controlplane/cli'; const VERSION_FILE_NAME = 'latestVersion.json'; const ONE_DAY = 1000 * 60 * 60 * 24; exports.About = { epoch: -1, timestamp: 'dev', version: 'dev', build: 'dev', npm: 'dev', }; try { exports.About = require('./about.json'); } catch (_a) { } /** * * @param v1 first version * @param v2 second version * @returns 1 if v1 > v2, 0 if v1 == v2, -1 if v1 < v2 * */ function compareVersions(v1, v2) { if (!v1 && v2) return -1; if (!v2 && v1) return 1; if (!v1 && !v2) return 0; try { if (semver.gt(v1, v2)) { return 1; } else if (semver.lt(v1, v2)) { return -1; } } catch (e) { return undefined; } return 0; } async function getLatestPackageVersion() { var _a; try { const npmPackageInfo = await axios_1.default.get(`http://registry.npmjs.org/${PACKAGE_NAME}`); return (_a = npmPackageInfo.data['dist-tags']) === null || _a === void 0 ? void 0 : _a.latest; } catch (e) { // Do nothing return undefined; } } exports.getLatestPackageVersion = getLatestPackageVersion; async function checkForNewVersion(homePath) { // Ignore version checking if npm version is dev or if an env is set if (exports.About.npm === 'dev' || Boolean(process.env['CPLN_SKIP_UPDATE_CHECK'])) { return ''; } let output = ''; let currentVersion = exports.About.npm; let latestVersion; let latestVersionInfo = {}; const ONE_DAY_AGO = Date.now() - ONE_DAY; const latestVersionInfoFilePath = path.join(homePath, VERSION_FILE_NAME); // Read verion info from the file. If the file does not exist, then version info should be empty try { latestVersionInfo = JSON.parse((0, fs_1.readFileSync)(latestVersionInfoFilePath, 'utf-8')); latestVersionInfo.outdated = false; // Assume current version is the latest latestVersion = latestVersionInfo.version; } catch (e) { // Ignore error reading file } // Fetch latest version from npm if it has been more than a day OR if latest version was undefined if ((latestVersionInfo.lastChecked && latestVersionInfo.lastChecked < ONE_DAY_AGO) || !latestVersion) { latestVersion = await getLatestPackageVersion(); // Update last checked to be now latestVersionInfo.lastChecked = Date.now(); } // Compare current version with latest version const versionCompare = compareVersions(latestVersion, currentVersion); // If compare failed, notify user if (versionCompare === undefined) { return 'Unable to check for updates. Please ensure you are on the latest version of cpln.'; } switch (versionCompare) { case 1: if (latestVersionInfo.lastNotifiedOfUpgrade && latestVersionInfo.lastNotifiedOfUpgrade >= ONE_DAY_AGO) { break; } // latest version is greater than current version and if last notified of upgrade was a day ago latestVersionInfo.outdated = true; latestVersionInfo.version = latestVersion; latestVersionInfo.lastNotifiedOfUpgrade = Date.now(); output = `A new version of cpln is now available: ${latestVersion}\nYour current version is: ${currentVersion}\nFeel free to upgrade at your convenience.`; break; case -1: // Current version is greater than latest version. Must have been a release in last day or we couldn't fetch latest version from npm latestVersionInfo.outdated = false; latestVersionInfo.version = currentVersion; break; default: // Current version is equal to latest version latestVersionInfo.outdated = false; latestVersionInfo.version = latestVersion; break; } // Set lastChecked if it was undefined if (!latestVersionInfo.lastChecked) { latestVersionInfo.lastChecked = Date.now(); } // Create new file with all gathered data try { (0, fs_1.writeFileSync)(latestVersionInfoFilePath, JSON.stringify(latestVersionInfo)); } catch (e) { // Ignore error writing file } return output; } exports.checkForNewVersion = checkForNewVersion; //# sourceMappingURL=version.js.map