Skip to content

Commit

Permalink
[DX-648] - implemented a warning if the user is using older versions …
Browse files Browse the repository at this point in the history
…of the CLI
  • Loading branch information
naman-contentstack committed Nov 21, 2024
1 parent a53b8ed commit ad337c8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
60 changes: 60 additions & 0 deletions packages/contentstack/src/hooks/prerun/latest-version-warning.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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);
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad337c8

Please sign in to comment.