shellquest
Version:
Terminal-based procedurally generated dungeon crawler
57 lines (46 loc) • 1.61 kB
text/typescript
import {spawn, spawnSync} from 'bun';
import packageJson from '../../package.json';
const PACKAGE_NAME = packageJson.name;
const CURRENT_VERSION = packageJson.version;
async function getLatestVersion(): Promise<string | null> {
try {
const response = await fetch(`https://registry.npmjs.org/${PACKAGE_NAME}/latest`);
if (!response.ok) return null;
const data = await response.json();
return data.version;
} catch {
return null;
}
}
function compareVersions(current: string, latest: string): number {
const currentParts = current.split('.').map(Number);
const latestParts = latest.split('.').map(Number);
for (let i = 0; i < 3; i++) {
const c = currentParts[i] || 0;
const l = latestParts[i] || 0;
if (l > c) return 1;
if (l < c) return -1;
}
return 0;
}
export async function checkAndUpdate(): Promise<boolean> {
// Skip version check in development
if (import.meta.dir.includes('node_modules') === false) {
return true;
}
const latestVersion = await getLatestVersion();
if (!latestVersion) {
return true;
}
if (compareVersions(latestVersion, CURRENT_VERSION) < 0) {
console.log(`\x1b[33mA new version of ${PACKAGE_NAME} is available: ${latestVersion} (current: ${CURRENT_VERSION})\x1b[0m`);
console.log('\x1b[36mUpdating automatically...\x1b[0m');
const proc = spawnSync(['bunx', '--bun', `${PACKAGE_NAME}@latest`], {
stdout: 'inherit',
stderr: 'inherit',
});
process.exit(proc.exitCode);
return false;
}
return true
}