UNPKG

balena-cli

Version:

The official balena Command Line Interface

150 lines (148 loc) 6.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeprecationChecker = void 0; class DeprecationChecker { constructor(currentVersion) { this.currentVersion = currentVersion; this.majorVersionFetchIntervalDays = 7; this.expiryDays = 365; this.deprecationDays = Math.ceil(this.expiryDays / 2); this.msInDay = 24 * 60 * 60 * 1000; this.debugPrefix = 'Deprecation check'; this.cacheFile = 'cachedReleaseTimestamps'; this.now = new Date().getTime(); this.initialized = false; const semver = require('semver'); const major = semver.major(this.currentVersion, { loose: true }); this.nextMajorVersion = `${major + 1}.0.0`; } async init() { if (this.initialized) { return; } this.initialized = true; const settings = await Promise.resolve().then(() => require('balena-settings-client')); const { getStorage } = await Promise.resolve().then(() => require('balena-settings-storage')); const dataDirectory = settings.get('dataDirectory'); this.storage = getStorage({ dataDirectory }); let stored; try { stored = (await this.storage.get(this.cacheFile)); } catch (_a) { } this.cachedTimestamps = { ...stored, lastFetched: (stored === null || stored === void 0 ? void 0 : stored.lastFetched) || '1970-01-01T00:00:00.000Z', }; } getNpmUrl(version) { return `https://registry.npmjs.org/balena-cli/${version}`; } async fetchPublishedTimestampForVersion(version) { var _a, _b, _c; const { default: got } = await Promise.resolve().then(() => require('got')); const url = this.getNpmUrl(version); let response; try { response = await got(url, { responseType: 'json', retry: 0, timeout: 4000, }); } catch (e) { if (((_a = e.response) === null || _a === void 0 ? void 0 : _a.statusCode) !== 404) { throw new Error(`Failed to query "${url}":\n${e}`); } } const publishedAt = (_c = (_b = response === null || response === void 0 ? void 0 : response.body) === null || _b === void 0 ? void 0 : _b.versionist) === null || _c === void 0 ? void 0 : _c.publishedAt; if (!publishedAt && process.env.DEBUG) { console.error(`\ [debug] ${this.debugPrefix}: balena CLI next major version "${this.nextMajorVersion}" not released, \ or release date not available`); } return publishedAt; } async checkForNewReleasesIfNeeded() { if (process.env.BALENARC_UNSUPPORTED) { return; } await this.init(); if (this.cachedTimestamps[this.nextMajorVersion]) { return; } const lastFetched = new Date(this.cachedTimestamps.lastFetched).getTime(); const daysSinceLastFetch = (this.now - lastFetched) / this.msInDay; if (daysSinceLastFetch < this.majorVersionFetchIntervalDays) { if (process.env.DEBUG) { const days = daysSinceLastFetch.toFixed(5); console.error(`\ [debug] ${this.debugPrefix}: ${days} days since last npm registry query for next major version release date. [debug] Will not query the registry again until at least ${this.majorVersionFetchIntervalDays} days have passed.`); } return; } if (process.env.DEBUG) { console.error(`\ [debug] ${this.debugPrefix}: Cache miss for the balena CLI next major version release date. [debug] Will query ${this.getNpmUrl(this.nextMajorVersion)}`); } try { const publishedAt = await this.fetchPublishedTimestampForVersion(this.nextMajorVersion); if (publishedAt) { this.cachedTimestamps[this.nextMajorVersion] = publishedAt; } } catch (e) { if (process.env.DEBUG) { console.error(`[debug] ${this.debugPrefix}: ${e}`); } } this.cachedTimestamps.lastFetched = new Date(this.now).toISOString(); await this.storage.set(this.cacheFile, this.cachedTimestamps); } async warnAndAbortIfDeprecated() { if (process.env.BALENARC_UNSUPPORTED) { return; } await this.init(); const nextMajorDateStr = this.cachedTimestamps[this.nextMajorVersion]; if (!nextMajorDateStr) { return; } const nextMajorDate = new Date(nextMajorDateStr).getTime(); const daysElapsed = Math.trunc((this.now - nextMajorDate) / this.msInDay); if (daysElapsed > this.expiryDays) { const { ExpectedError } = await Promise.resolve().then(() => require('./errors')); throw new ExpectedError(this.getExpiryMsg(daysElapsed)); } else if (daysElapsed > this.deprecationDays && process.stderr.isTTY) { console.error(this.getDeprecationMsg(daysElapsed)); } } getDeprecationMsg(daysElapsed) { const { warnify } = require('./utils/messages'); return warnify(`\ CLI version ${this.nextMajorVersion} was released ${daysElapsed} days ago: please upgrade. This version of the balena CLI (${this.currentVersion}) will exit with an error message after ${this.expiryDays} days from the release of version ${this.nextMajorVersion}, as per deprecation policy: https://git.io/JRHUW#deprecation-policy The --unsupported flag may be used to bypass this deprecation check and allow the CLI to keep working beyond the deprecation period. However, note that the balenaCloud or openBalena backends may be updated in a way that is no longer compatible with this version.`); } getExpiryMsg(daysElapsed) { return ` This version of the balena CLI (${this.currentVersion}) has expired: please upgrade. ${daysElapsed} days have passed since the release of CLI version ${this.nextMajorVersion}. See deprecation policy at: https://git.io/JRHUW#deprecation-policy The --unsupported flag may be used to bypass this deprecation check and continue using this version of the CLI. However, note that the balenaCloud or openBalena backends may be updated in a way that is no longer compatible with this CLI version.`; } } exports.DeprecationChecker = DeprecationChecker; //# sourceMappingURL=deprecation.js.map