UNPKG

@lark-project/cli

Version:

飞书项目插件开发工具

79 lines (78 loc) 3.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkLatestVersion = void 0; const semver_1 = __importDefault(require("semver")); const execa_1 = __importDefault(require("execa")); const env_1 = require("./env"); const logger_1 = require("./logger"); const with_loading_spinner_1 = require("./with-loading-spinner"); const settings_1 = require("./settings"); const shelljs_1 = __importDefault(require("shelljs")); /** A confirmed "already latest" result stays fresh for this long. */ const LATEST_TTL_MS = 60 * 60 * 1000; // 1 hour /** A failed check (network error) is retried no sooner than this. */ const FAILED_TTL_MS = 10 * 60 * 1000; // 10 minutes /** Persist a timestamp without ever letting a disk/permission error crash the command. */ function recordCheck(update) { try { (0, settings_1.saveSettings)(update); } catch (_a) { // Version check is best-effort; a settings write failure must never break the command. } } async function checkLatestVersion() { // Throttle: skip silently inside the freshness window unless explicitly forced. if (!env_1.env.forceLatestValid) { const now = Date.now(); const { lastLatestCheckAt, lastLatestCheckFailedAt } = (0, settings_1.loadSettings)(); const freshSuccess = lastLatestCheckAt != null && now - lastLatestCheckAt < LATEST_TTL_MS; const freshFailure = lastLatestCheckFailedAt != null && now - lastLatestCheckFailedAt < FAILED_TTL_MS; if (freshSuccess || freshFailure) { return; } } logger_1.logger.info('start check @lark-project/cli latest version...'); const packageJson = require('../../package.json'); const { name, version } = packageJson; try { const { stdout: latestVersion } = await (0, with_loading_spinner_1.withLoadingSpinner)(execa_1.default, 'Check whether the cli version is the latest...', 'npm', ['view', name, 'version']); const fallback = () => { logger_1.logger.error(`Your Lark Project CLI version ${version} is out of date.\r\nWe recommend you update to the latest version ${latestVersion} to get the latest features and bug fixes.`); }; if (semver_1.default.gt(latestVersion, version)) { // Pending update: do NOT record a success timestamp, so a rerun (incl. after a // failed auto-install) keeps re-checking and re-attempting the upgrade rather // than getting silently throttled into staying on the old version. logger_1.logger.info('try to fetch the latest version...'); try { const result = shelljs_1.default.exec('npm install -g @lark-project/cli@latest'); if (result.code !== 0) { fallback(); } else { logger_1.logger.info(`We have helped you update to the latest version ${latestVersion} to get the latest features and bug fixes.\r\nYou can restart the command`); } } catch (e) { // 理论不会走到这里的异常 fallback(); } process.exit(1); } else { recordCheck({ lastLatestCheckAt: Date.now() }); logger_1.logger.info('The Lark Project CLI version is the latest'); } } catch (e) { // Network/registry failure: short negative-cache so an offline run doesn't pay the // `npm view` timeout on every single command, while still retrying soon. recordCheck({ lastLatestCheckFailedAt: Date.now() }); logger_1.logger.warn('The Lark Project CLI latest version check failed.', e); } } exports.checkLatestVersion = checkLatestVersion;