UNPKG

tsc-path-fix

Version:

Zero-runtime TypeScript path resolver - converts aliases to relative paths at compile time. Fast, lightweight, with native watch mode.

186 lines 8.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkForUpdates = checkForUpdates; exports.notifyUpdates = notifyUpdates; const https = require("https"); const chalk = require("chalk"); const fs = require("fs"); const path = require("path"); const os = require("os"); const { version, name } = require('../../package.json'); const CACHE_DIR = path.join(os.homedir(), '.cache', 'tsc-path-fix'); const CACHE_FILE = path.join(CACHE_DIR, 'version-check.json'); const CHECK_INTERVAL = 24 * 60 * 60 * 1000; function ensureCacheDir() { try { if (!fs.existsSync(CACHE_DIR)) { fs.mkdirSync(CACHE_DIR, { recursive: true }); } } catch (error) { } } function readCache() { try { if (fs.existsSync(CACHE_FILE)) { const cacheData = JSON.parse(fs.readFileSync(CACHE_FILE, 'utf8')); return cacheData; } } catch (error) { } return null; } function writeCache(latestVersion) { try { ensureCacheDir(); fs.writeFileSync(CACHE_FILE, JSON.stringify({ timestamp: Date.now(), latestVersion })); } catch (error) { } } function checkForUpdates() { return __awaiter(this, arguments, void 0, function* (verbose = false, debug = false, forceCheck = false) { if (!forceCheck) { const cachedData = readCache(); if (cachedData && Date.now() - cachedData.timestamp < CHECK_INTERVAL) { if (debug) console.log(chalk.gray(`Using cached version data: ${cachedData.latestVersion}`)); if (cachedData.latestVersion.localeCompare(version, undefined, { numeric: true }) > 0) { return cachedData.latestVersion; } return null; } } return new Promise((resolve) => { const timeoutId = setTimeout(() => { if (debug) console.log(chalk.yellow('Version check timed out')); resolve(null); }, 3000); const options = { headers: { 'User-Agent': `${name}/${version} node/${process.version} (${process.platform})`, 'Accept': 'application/json' } }; const registryUrl = `https://registry.npmjs.org/${encodeURIComponent(name)}`; if (debug) console.log(chalk.gray(`Checking for updates at: ${registryUrl}`)); https .get(registryUrl, options, (res) => { if (res.statusCode === 429) { clearTimeout(timeoutId); if (debug) console.log(chalk.yellow(`Rate limited by npm registry, using cached data if available`)); const cachedData = readCache(); if (cachedData && cachedData.latestVersion.localeCompare(version, undefined, { numeric: true }) > 0) { resolve(cachedData.latestVersion); } else { resolve(null); } return; } if (res.statusCode !== 200) { clearTimeout(timeoutId); if (debug) console.log(chalk.yellow(`Failed to check for updates: HTTP ${res.statusCode}`)); resolve(null); return; } let data = ''; res.on('data', chunk => { data += chunk; }); res.on('end', () => { var _a; clearTimeout(timeoutId); try { const parsedData = JSON.parse(data); const latestVersion = (_a = parsedData['dist-tags']) === null || _a === void 0 ? void 0 : _a.latest; if (debug) console.log(chalk.gray(`Current version: ${version}, Latest version: ${latestVersion}`)); if (!latestVersion) { if (debug) console.log(chalk.yellow('Could not determine latest version')); resolve(null); return; } writeCache(latestVersion); if (latestVersion !== version) { if (latestVersion.localeCompare(version, undefined, { numeric: true }) > 0) { if (verbose || debug) console.log(chalk.green(`New version available: ${latestVersion}`)); resolve(latestVersion); return; } } if (debug) console.log(chalk.gray('No new version available')); resolve(null); } catch (e) { if (debug) console.log(chalk.yellow(`Error parsing version data: ${e.message}`)); resolve(null); } }); }) .on('error', (err) => { clearTimeout(timeoutId); if (debug) console.log(chalk.yellow(`Network error checking for updates: ${err.message}`)); const cachedData = readCache(); if (cachedData && cachedData.latestVersion.localeCompare(version, undefined, { numeric: true }) > 0) { resolve(cachedData.latestVersion); } else { resolve(null); } }); }); }); } function notifyUpdates() { return __awaiter(this, arguments, void 0, function* (verbose = false, debug = false, forceCheck = false) { try { if (verbose || debug) { console.log(chalk.gray('Checking for updates...')); } const latestVersion = yield checkForUpdates(verbose, debug, forceCheck); if (latestVersion) { const boxWidth = 42; const padding = ' '.repeat(Math.max(1, 22 - version.length - latestVersion.length)); console.log(chalk.yellow('\n┌' + '─'.repeat(boxWidth) + '┐')); console.log(chalk.yellow('│ ') + chalk.yellow(`Update available! ${version}${latestVersion}${padding}`) + chalk.yellow(' │')); console.log(chalk.yellow('│ ') + chalk.yellow(`Run `) + chalk.cyan(`npm install -g ${name}@latest`) + chalk.yellow(' to update ') + chalk.yellow(' │')); console.log(chalk.yellow('└' + '─'.repeat(boxWidth) + '┘')); } } catch (e) { if (debug) { console.log(chalk.red(`Error checking for updates: ${e.message}`)); } } }); } //# sourceMappingURL=version-check.js.map