diff --git a/package-lock.json b/package-lock.json index 5823b09a78..3fdfc93723 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26652,6 +26652,7 @@ "@types/mkdirp": "^1.0.2", "@types/mocha": "^8.2.3", "@types/node": "^14.18.63", + "@types/semver": "^7.5.8", "@types/sinon": "^10.0.20", "chai": "^4.5.0", "eslint": "^8.57.1", diff --git a/packages/contentstack/package.json b/packages/contentstack/package.json index 3e43bc1d0c..22c8e7ff6a 100755 --- a/packages/contentstack/package.json +++ b/packages/contentstack/package.json @@ -61,6 +61,7 @@ "@types/mkdirp": "^1.0.2", "@types/mocha": "^8.2.3", "@types/node": "^14.18.63", + "@types/semver": "^7.5.8", "@types/sinon": "^10.0.20", "chai": "^4.5.0", "eslint": "^8.57.1", @@ -153,7 +154,8 @@ "hooks": { "prerun": [ "./lib/hooks/prerun/command-deprecation-check", - "./lib/hooks/prerun/default-rate-limit-check" + "./lib/hooks/prerun/default-rate-limit-check", + "./lib/hooks/prerun/latest-version-warning" ], "init": [ "./lib/hooks/init/context-init", diff --git a/packages/contentstack/src/hooks/prerun/latest-version-warning.ts b/packages/contentstack/src/hooks/prerun/latest-version-warning.ts new file mode 100644 index 0000000000..4bf110faf9 --- /dev/null +++ b/packages/contentstack/src/hooks/prerun/latest-version-warning.ts @@ -0,0 +1,60 @@ +import { cliux, configHandler, HttpClient, LoggerService } from '@contentstack/cli-utilities'; +import * as semver from 'semver'; +interface ICacheData { + lastChecked: number; + lastWarnedDate: string; + latestVersion: string; +} +interface IVersionUpgradeWarningFrequency { + versionSyncDuration: number; +} +const versionUpgradeWarningFrequency: IVersionUpgradeWarningFrequency = { + versionSyncDuration: 3 * 24 * 60 * 60 * 1000, +}; +export default async function (_opts): Promise { + const now = Date.now(); + const today = '2023-11-21'; + let logger!: LoggerService; + logger = new LoggerService(process.cwd(), 'cli-log'); + let cache: ICacheData = { lastChecked: 0, lastWarnedDate: '', latestVersion: '' }; + + if (!configHandler.get('versionUpgradeWarningFrequency')) { + configHandler.set('versionUpgradeWarningFrequency', versionUpgradeWarningFrequency); + } + const versionUpgradeWarningFrequencyConfig: IVersionUpgradeWarningFrequency = configHandler.get( + 'versionUpgradeWarningFrequency', + ); + + // Load cache if it exists + if (configHandler.get('versionUpgradeWarningCache')) { + cache = configHandler.get('versionUpgradeWarningCache'); + } + + // Perform update check if needed + const httpClient = new HttpClient(); + + if (now - cache.lastChecked > versionUpgradeWarningFrequencyConfig.versionSyncDuration) { + try { + const latestVersion = (await httpClient.get(`https://registry.npmjs.org/@contentstack/cli/latest`)).data.version; + cache.latestVersion = latestVersion; + cache.lastChecked = now; + + // Save updated cache + configHandler.set('versionUpgradeWarningCache', cache); + } catch (error) { + logger.error('Failed to check the latest version', error); + return; + } + } + + // Show warning if an update is available and last warning was > 3 hours ago + if (semver.gt(cache.latestVersion, this.config.version) && cache.lastWarnedDate !== today) { + cliux.print( + `You are using version ${this.config.version}, but the latest version is ${cache.latestVersion}. Please update your CLI for the best experience.`, + { color: 'yellow' }, + ); + // Update the last warned timestamp + cache.lastWarnedDate = today; + configHandler.set('versionUpgradeWarningCache', cache); + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9e0f6e86ef..a3b55f2329 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,7 @@ importers: '@types/mkdirp': ^1.0.2 '@types/mocha': ^8.2.3 '@types/node': ^14.18.63 + '@types/semver': ^7.5.8 '@types/sinon': ^10.0.20 chai: ^4.5.0 chalk: ^4.1.2 @@ -103,6 +104,7 @@ importers: '@types/mkdirp': 1.0.2 '@types/mocha': 8.2.3 '@types/node': 14.18.63 + '@types/semver': 7.5.8 '@types/sinon': 10.0.20 chai: 4.5.0 eslint: 8.57.1