import chalk from 'chalk'; import packageJson from '../../package.json'; import { CLIError, ExitCodes } from '../errors/platform-error'; const oldestSupportNodeMajorVersion = 20; // True only when `current` is strictly behind `latest` (semver numeric compare). // A local/unreleased build that is ahead of (or equal to) the published latest is // NOT out of date — strict `!==` wrongly blocked devs running a newer local link. // Unparseable versions can't be compared, so we never block on them. export const isWorkbenchOutdated = (current: string, latest: string): boolean => { const c = current.split('.').map((n) => Number.parseInt(n, 10)); const l = latest.split('.').map((n) => Number.parseInt(n, 10)); if ([...c, ...l].some(Number.isNaN)) return false; for (let i = 0; i < Math.max(c.length, l.length); i += 1) { const a = c[i] ?? 0; const b = l[i] ?? 0; if (a !== b) return a < b; } return false; }; export const checkPackageAndNodeVersion = async (shouldLog = true) => { try { // Check Node.js version const nodeMajorVersion = Number.parseInt(process.version.replace('v', '').split('.')[0], 10); const nodeOutdated = nodeMajorVersion < oldestSupportNodeMajorVersion; if (nodeOutdated && shouldLog) { throw new CLIError( `Your Node.js version is no longer supporter by Root Workbench.\n\n` + `Current version: ${process.version}\n` + `Oldest supported version: v${oldestSupportNodeMajorVersion}\n` + `Please install version ${oldestSupportNodeMajorVersion} or later. See https://nodejs.org/ for more.\n\n` + `Contact support@root.co.za for assistance.`, ExitCodes.VERSION_MISMATCH, ); } // Fetch latest from NPM registry const response = await fetch('https://registry.npmjs.org/@rootplatform/cli'); const data = (await response.json()) as Record; const latestVersion = data['dist-tags'].latest as string; // Read local version const currentVersion = packageJson.version; const workbenchOutdated = isWorkbenchOutdated(currentVersion, latestVersion); if (workbenchOutdated && shouldLog) { throw new CLIError( `Your Root Workbench version is out of date.\n\n` + `Current version: ${currentVersion}\n` + `Latest version: ${latestVersion}\n\n` + `Please install the latest version using ${chalk.bold('npm i -g @rootplatform/cli')}\n\n` + `Contact support@root.co.za for assistance.`, ExitCodes.VERSION_MISMATCH, ); } return { latest: latestVersion, current: currentVersion, }; } catch (error) { console.log( chalk.red( 'Version check failed. Please update your npm package to the latest version or contact support@root.co.za.', ), ); throw error; } };