UNPKG

microfox

Version:

Universal CLI tool for creating modern TypeScript packages with npm availability checking

139 lines (135 loc) 5.3 kB
#!/usr/bin/env node // src/commands/update.ts import { Command } from "commander"; // src/utils/experimental-updater.ts import fs from "fs"; import path from "path"; import { execSync } from "child_process"; import chalk from "chalk"; var CWD_PACKAGE_JSON = path.join(process.cwd(), "package.json"); function log(message, isError = false, isWarning = false) { const timestamp = (/* @__PURE__ */ new Date()).toLocaleTimeString(); let prefix; if (isError) { prefix = chalk.red(`\u274C [update-microfox ${timestamp}]`); } else if (isWarning) { prefix = chalk.yellow(`\u26A0\uFE0F [update-microfox ${timestamp}]`); } else { prefix = chalk.blue(`\u2139\uFE0F [update-microfox ${timestamp}]`); } console.log(`${prefix} ${message}`); } function logSuccess(message) { const timestamp = (/* @__PURE__ */ new Date()).toLocaleTimeString(); console.log(chalk.green(`\u2705 [update-microfox ${timestamp}] ${message}`)); } function logSection(title) { console.log("\n" + "=".repeat(60)); console.log(` ${title}`); console.log("=".repeat(60)); } function updatePackage(packageName, isDev) { var _a; log(`Updating package to latest: ${packageName}`); const saveFlag = isDev ? "--save-dev" : "--save"; try { execSync(`npm install ${packageName}@latest ${saveFlag}`, { cwd: process.cwd(), stdio: "pipe" }); logSuccess(`Successfully updated ${packageName} to the latest version from npm.`); return true; } catch (error) { log(`Failed to update ${packageName}: ${error.message}`, true); log(`Stderr: ${(_a = error.stderr) == null ? void 0 : _a.toString()}`, true); return false; } } function updateSpecificPackage(packageName, isDevFlag) { logSection(`Updating Specific Package: ${packageName}`); if (!packageName || typeof packageName !== "string" || !packageName.startsWith("@microfox/")) { log(`Invalid package name: "${packageName}". Must start with "@microfox/".`, true); process.exit(1); } const packageJson = JSON.parse(fs.readFileSync(CWD_PACKAGE_JSON, "utf8")); const dependencies = packageJson.dependencies || {}; const devDependencies = packageJson.devDependencies || {}; let isDev = !!devDependencies[packageName]; if (!dependencies[packageName] && !devDependencies[packageName]) { log(`Package "${packageName}" not found in dependencies.`, false, true); log(`Attempting to install it as a new package...`, false, true); isDev = isDevFlag; } else { log(`Package found in ${isDev ? "devDependencies" : "dependencies"}.`); } if (updatePackage(packageName, isDev)) { logSection(`\u2705 SUCCESS`); logSuccess(`Package ${packageName} updated successfully!`); } else { log(`Failed to update ${packageName}`, true); process.exit(1); } } function updateAllPackages() { logSection("Updating all @microfox/* packages to latest"); const packageJson = JSON.parse(fs.readFileSync(CWD_PACKAGE_JSON, "utf8")); const dependencies = packageJson.dependencies || {}; const devDependencies = packageJson.devDependencies || {}; const allDependencies = { ...dependencies, ...devDependencies }; const microfoxPackages = Object.entries(allDependencies).filter(([name]) => name.startsWith("@microfox/")).map(([name]) => name); if (microfoxPackages.length === 0) { log("\u2139\uFE0F No @microfox/* packages found in package.json."); return; } log(`Found ${microfoxPackages.length} packages to update:`); microfoxPackages.forEach((pkg) => log(` - ${pkg}`)); let successCount = 0; let failureCount = 0; for (const packageName of microfoxPackages) { const isDev = !!devDependencies[packageName]; if (updatePackage(packageName, isDev)) { successCount++; } else { failureCount++; } } log(` \u{1F4CA} Update Summary:`); log(` \u2705 Successfully updated: ${successCount} packages`); if (failureCount > 0) { log(` \u274C Failed to update: ${failureCount} packages`); } } async function runUpdate(packages, options) { try { logSection("Microfox Package Updater (Experimental)"); if (!fs.existsSync(CWD_PACKAGE_JSON)) { log(`package.json not found in ${process.cwd()}`, true); process.exit(1); } if (packages.length > 0) { log(`Updating ${packages.length} specific package(s)...`); packages.forEach((pkg) => updateSpecificPackage(pkg, !!options.dev)); logSection("\u{1F389} All specified packages processed."); } else { updateAllPackages(); } } catch (error) { log("", true); log(`An unexpected error occurred: ${error.message}`, true); if (error.stack) { log(`Stack trace: ${error.stack}`, true); } process.exit(1); } } // src/commands/update.ts var updateCommand = new Command("update").description("Update @microfox packages to the latest versions from npm.").argument("[packages...]", "Specific packages to update").option("--experimental", "Enable experimental features").option("--dev", "Update packages in devDependencies").action(async (packages, options) => { if (options.experimental) { await runUpdate(packages, options); } else { console.log("This command is only available with the --experimental flag."); process.exit(1); } }); export { updateCommand }; //# sourceMappingURL=chunk-UHWJTQKW.mjs.map