Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/DX-648 implemented a warning message if user is using older version of cli #1687

Merged
merged 11 commits into from
Nov 26, 2024
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",
naman-contentstack marked this conversation as resolved.
Show resolved Hide resolved
"@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
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;
}
naman-contentstack marked this conversation as resolved.
Show resolved Hide resolved
const versionUpgradeWarningFrequency: IVersionUpgradeWarningFrequency = {
versionSyncDuration: 3 * 24 * 60 * 60 * 1000,
};
export default async function (_opts): Promise<void> {
const now = Date.now();
const today = new Date().toISOString().split('T')[0];
let logger!: LoggerService;
logger = new LoggerService(process.cwd(), 'cli-log');
naman-contentstack marked this conversation as resolved.
Show resolved Hide resolved
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;
naman-contentstack marked this conversation as resolved.
Show resolved Hide resolved
cache.latestVersion = latestVersion;
cache.lastChecked = now;

// Save updated cache
configHandler.set('versionUpgradeWarningCache', cache);
} catch (error) {
logger.error('Failed to check the latest version', error);
naman-contentstack marked this conversation as resolved.
Show resolved Hide resolved
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(
naman-contentstack marked this conversation as resolved.
Show resolved Hide resolved
`You are using version ${this.config.version}, but the latest version is ${cache.latestVersion}. Please update your CLI for the best experience.`,
{ color: 'yellow' },
);
naman-contentstack marked this conversation as resolved.
Show resolved Hide resolved
// 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.

Loading