import { SpinnerLog, SuccessLog, ErrorLog, addLog, addErrorLog, clearLogs, getLatestClientVersion, downloadTargetMint, getClientVersion, } from '@mintlify/previewing'; import { assertValidPackageName, execFileAsync, getLatestCliVersion, getVersions, detectPackageManager, windowsCompatibleSpawnOptions, } from './helpers.js'; export const update = async ({ packageName }: { packageName: string }) => { assertValidPackageName(packageName); addLog(); const { cli: existingCliVersion } = getVersions(packageName); const latestCliVersion = getLatestCliVersion(packageName); const isUpToDate = existingCliVersion && latestCliVersion && latestCliVersion.trim() === existingCliVersion.trim(); if (!isUpToDate && existingCliVersion && latestCliVersion.trim() !== existingCliVersion.trim()) { const packageManager = await detectPackageManager({ packageName }); try { clearLogs(); addLog(); if (packageManager === 'pnpm') { await execFileAsync( 'pnpm', ['install', '-g', `${packageName}@latest`, '--silent'], windowsCompatibleSpawnOptions ); } else { await execFileAsync( 'npm', ['install', '-g', `${packageName}@latest`, '--silent'], windowsCompatibleSpawnOptions ); } } catch (err) { const errorMessage = err instanceof Error ? err.message : String(err); addErrorLog( ); return; } } clearLogs(); addLog(); let clientUpdated = false; const latestClientVersion = await getLatestClientVersion(); if (latestClientVersion) { const existingClientVersion = getClientVersion().trim(); if (existingClientVersion !== latestClientVersion.trim()) { try { await downloadTargetMint({ targetVersion: latestClientVersion, existingVersion: existingClientVersion === 'none' ? null : existingClientVersion, }); clientUpdated = true; } catch (err) { const errorMessage = err instanceof Error ? err.message : String(err); addErrorLog(); return; } } } clearLogs(); if (isUpToDate && !clientUpdated) { addLog(); } else { addLog( ); } };