transform-to-unocss
Version:
🚀 Effortlessly transform CSS, inline styles, and preprocessors (Sass/Less/Stylus) to UnoCSS with smart conflict resolution and debug support
186 lines (183 loc) • 7.04 kB
JavaScript
"use strict";
const require_transformCode = require('./transformCode-BwjIPRT2.cjs');
const node_fs = require_transformCode.__toESM(require("node:fs"));
const node_path = require_transformCode.__toESM(require("node:path"));
const node_process = require_transformCode.__toESM(require("node:process"));
const node_timers_promises = require_transformCode.__toESM(require("node:timers/promises"));
const __simon_he_clack_prompts = require_transformCode.__toESM(require("@simon_he/clack-prompts"));
const picocolors = require_transformCode.__toESM(require("picocolors"));
const fast_glob = require_transformCode.__toESM(require("fast-glob"));
//#region node_modules/.pnpm/@simon_he+colorize@0.0.1/node_modules/@simon_he/colorize/dist/index.mjs
const RGB_LIKE_REGEX = /^(rgb|hsl|hsv|hwb)\(\s?(\d+),\s?(\d+),\s?(\d+)\s?\)$/;
const ANSI_REGEX = /^(ansi|ansi256)\(\s?(\d+)\s?\)$/;
const getMethod = (name, type) => type === "foreground" ? name : `bg${name[0].toUpperCase()}${name.slice(1)}`;
function setColor(str, color, type) {
if (!color) return str;
if (color in picocolors.default) {
const method2 = getMethod(color, type);
return picocolors.default[method2](str);
}
if (color.startsWith("#")) {
const method2 = getMethod("hex", type);
return picocolors.default[method2](color)(str);
}
if (color.startsWith("ansi")) {
const matches2 = ANSI_REGEX.exec(color);
if (!matches2) return str;
const method2 = getMethod(matches2[1], type);
const value = Number(matches2[2]);
return picocolors.default[method2](value)(str);
}
const isRgbLike = color.startsWith("rgb") || color.startsWith("hsl") || color.startsWith("hsv") || color.startsWith("hwb");
if (!isRgbLike) return str;
const matches = RGB_LIKE_REGEX.exec(color);
if (!matches) return str;
const method = getMethod(matches[1], type);
const firstValue = Number(matches[2]);
const secondValue = Number(matches[3]);
const thirdValue = Number(matches[4]);
return picocolors.default[method](firstValue, secondValue, thirdValue)(str);
}
function colorize(options) {
const { text, color, bgColor, dimmed, bold, italic, underline, strikethrough, inverse } = options;
let result = text;
if (dimmed) result = picocolors.default.dim(result);
if (color) result = setColor(result, color, "foreground");
if (bgColor) result = setColor(result, bgColor, "background");
if (bold) result = picocolors.default.bold(result);
if (italic) result = picocolors.default.italic(result);
if (underline) result = picocolors.default.underline(result);
if (strikethrough) result = picocolors.default.strikethrough(result);
if (inverse) result = picocolors.default.inverse(result);
return result;
}
//#endregion
//#region src/cli.ts
async function cli() {
console.clear();
await (0, node_timers_promises.setTimeout)(100);
const s = __simon_he_clack_prompts.spinner();
__simon_he_clack_prompts.intro(`${colorize({
text: colorize({
text: " 🚀 Transform Code To Unocss ",
color: "black"
}),
bgColor: "cyan"
})}`);
const asset = node_process.default.argv[2];
const isRevert = node_process.default.argv[3] === "-r" || node_process.default.argv[3] === "--revert";
const isForce = node_process.default.argv.includes("--force");
const isDebug = node_process.default.argv.includes("--debug") || node_process.default.argv.includes("-d");
if (!asset) {
__simon_he_clack_prompts.cancel("❌ Please specify a directory or file path to convert.");
__simon_he_clack_prompts.note(`Usage: tounocss <directory/file> [options]
Options:
-r, --revert Revert the conversion
--force Force overwrite existing files
-d, --debug Enable debug mode with detailed logging`, "Available options:");
return;
}
const fileDir = node_path.default.resolve(node_process.default.cwd(), asset);
if (node_fs.default.existsSync(fileDir) && node_fs.default.statSync(fileDir).isFile()) {
__simon_he_clack_prompts.note(`${fileDir}`, "Converting file...");
s.start("Converting...");
const suffix = fileDir.slice(fileDir.lastIndexOf(".") + 1);
const code = await node_fs.default.promises.readFile(fileDir, "utf-8");
const codeTransfer = await require_transformCode.transformCode(code, {
filepath: fileDir,
type: suffix,
debug: isDebug
});
try {
await node_fs.default.promises.writeFile(fileDir.replace(`.${suffix}`, `${require_transformCode.TRANSFER_FLAG}.${suffix}`), codeTransfer);
s.stop("Conversion completed.");
} catch (error) {
__simon_he_clack_prompts.cancel(`❌ Conversion failed: ${error}`);
}
return;
}
if (!node_fs.default.existsSync(fileDir)) {
__simon_he_clack_prompts.cancel(`Directory not found: ${fileDir}`);
return;
}
const entries = await (0, fast_glob.default)([
"**.vue",
"**.tsx",
"**.html",
"**.svelte",
"**.astro"
], { cwd: fileDir });
if (!entries.length) {
__simon_he_clack_prompts.cancel("No convertible files found in the specified directory.");
return;
}
__simon_he_clack_prompts.note(`${fileDir}`, "Converting directory...");
s.start("Converting...");
await Promise.all(entries.filter((entry) => !entry.includes(require_transformCode.TRANSFER_FLAG)).map(async (entry) => {
const filepath = node_path.default.join(fileDir, entry);
const suffix = entry.slice(entry.lastIndexOf(".") + 1);
const newfilepath = filepath.endsWith(require_transformCode.TRANSFER_FLAG) ? filepath : filepath.replace(`.${suffix}`, `${require_transformCode.TRANSFER_FLAG}.${suffix}`);
if (node_fs.default.existsSync(newfilepath)) {
if (isRevert) {
try {
await node_fs.default.promises.unlink(newfilepath);
__simon_he_clack_prompts.note(colorize({
text: newfilepath,
color: "cyan"
}), colorize({
text: "Revert succeeded",
color: "green"
}));
} catch (error) {
__simon_he_clack_prompts.note(colorize({
text: newfilepath,
color: "cyan"
}), colorize({
text: `Revert failed: ${error}`,
color: "red"
}));
}
return;
}
if (isForce) {} else {
__simon_he_clack_prompts.note(colorize({
text: newfilepath,
color: "cyan"
}), colorize({
text: "Skipped (use --force to overwrite)",
color: "yellow"
}));
return;
}
} else if (isRevert) return;
const code = await node_fs.default.promises.readFile(filepath, "utf-8");
const codeTransfer = await require_transformCode.transformCode(code, {
filepath,
type: suffix,
debug: isDebug
});
try {
await node_fs.default.promises.writeFile(newfilepath, codeTransfer);
__simon_he_clack_prompts.note(colorize({
text: newfilepath,
color: "cyan"
}), colorize({
text: `Transfer succeeded${isForce ? " (overwritten)" : ""}`,
color: "green"
}));
} catch (error) {
__simon_he_clack_prompts.note(colorize({
text: newfilepath,
color: "cyan"
}), colorize({
text: `Transfer failed: ${error}`,
color: "red"
}));
}
}));
s.stop("Conversion completed.");
__simon_he_clack_prompts.outro("✅ All files have been processed.");
}
cli();
//#endregion
exports.cli = cli