microfox
Version:
Universal CLI tool for creating modern TypeScript packages with npm availability checking
172 lines (169 loc) • 7.15 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/commands/update.ts
var update_exports = {};
__export(update_exports, {
updateCommand: () => updateCommand
});
module.exports = __toCommonJS(update_exports);
var import_commander = require("commander");
// src/utils/experimental-updater.ts
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_child_process = require("child_process");
var import_chalk = __toESM(require("chalk"));
var CWD_PACKAGE_JSON = import_path.default.join(process.cwd(), "package.json");
function log(message, isError = false, isWarning = false) {
const timestamp = (/* @__PURE__ */ new Date()).toLocaleTimeString();
let prefix;
if (isError) {
prefix = import_chalk.default.red(`\u274C [update-microfox ${timestamp}]`);
} else if (isWarning) {
prefix = import_chalk.default.yellow(`\u26A0\uFE0F [update-microfox ${timestamp}]`);
} else {
prefix = import_chalk.default.blue(`\u2139\uFE0F [update-microfox ${timestamp}]`);
}
console.log(`${prefix} ${message}`);
}
function logSuccess(message) {
const timestamp = (/* @__PURE__ */ new Date()).toLocaleTimeString();
console.log(import_chalk.default.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 {
(0, import_child_process.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(import_fs.default.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(import_fs.default.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 (!import_fs.default.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 import_commander.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);
}
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
updateCommand
});
//# sourceMappingURL=update.js.map