UNPKG

transform-to-unocss

Version:

🚀 Effortlessly transform CSS, inline styles, and preprocessors (Sass/Less/Stylus) to UnoCSS with smart conflict resolution and debug support

185 lines (182 loc) 5.79 kB
import { TRANSFER_FLAG, transformCode } from "./transformCode-BTEg47qj.js"; import fs from "node:fs"; import path from "node:path"; import process from "node:process"; import { setTimeout } from "node:timers/promises"; import * as p from "@simon_he/clack-prompts"; import pc from "picocolors"; import fg from "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 pc) { const method2 = getMethod(color, type); return pc[method2](str); } if (color.startsWith("#")) { const method2 = getMethod("hex", type); return pc[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 pc[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 pc[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 = pc.dim(result); if (color) result = setColor(result, color, "foreground"); if (bgColor) result = setColor(result, bgColor, "background"); if (bold) result = pc.bold(result); if (italic) result = pc.italic(result); if (underline) result = pc.underline(result); if (strikethrough) result = pc.strikethrough(result); if (inverse) result = pc.inverse(result); return result; } //#endregion //#region src/cli.ts async function cli() { console.clear(); await setTimeout(100); const s = p.spinner(); p.intro(`${colorize({ text: colorize({ text: " 🚀 Transform Code To Unocss ", color: "black" }), bgColor: "cyan" })}`); const asset = process.argv[2]; const isRevert = process.argv[3] === "-r" || process.argv[3] === "--revert"; const isForce = process.argv.includes("--force"); const isDebug = process.argv.includes("--debug") || process.argv.includes("-d"); if (!asset) { p.cancel("❌ Please specify a directory or file path to convert."); p.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 = path.resolve(process.cwd(), asset); if (fs.existsSync(fileDir) && fs.statSync(fileDir).isFile()) { p.note(`${fileDir}`, "Converting file..."); s.start("Converting..."); const suffix = fileDir.slice(fileDir.lastIndexOf(".") + 1); const code = await fs.promises.readFile(fileDir, "utf-8"); const codeTransfer = await transformCode(code, { filepath: fileDir, type: suffix, debug: isDebug }); try { await fs.promises.writeFile(fileDir.replace(`.${suffix}`, `${TRANSFER_FLAG}.${suffix}`), codeTransfer); s.stop("Conversion completed."); } catch (error) { p.cancel(`❌ Conversion failed: ${error}`); } return; } if (!fs.existsSync(fileDir)) { p.cancel(`Directory not found: ${fileDir}`); return; } const entries = await fg([ "**.vue", "**.tsx", "**.html", "**.svelte", "**.astro" ], { cwd: fileDir }); if (!entries.length) { p.cancel("No convertible files found in the specified directory."); return; } p.note(`${fileDir}`, "Converting directory..."); s.start("Converting..."); await Promise.all(entries.filter((entry) => !entry.includes(TRANSFER_FLAG)).map(async (entry) => { const filepath = path.join(fileDir, entry); const suffix = entry.slice(entry.lastIndexOf(".") + 1); const newfilepath = filepath.endsWith(TRANSFER_FLAG) ? filepath : filepath.replace(`.${suffix}`, `${TRANSFER_FLAG}.${suffix}`); if (fs.existsSync(newfilepath)) { if (isRevert) { try { await fs.promises.unlink(newfilepath); p.note(colorize({ text: newfilepath, color: "cyan" }), colorize({ text: "Revert succeeded", color: "green" })); } catch (error) { p.note(colorize({ text: newfilepath, color: "cyan" }), colorize({ text: `Revert failed: ${error}`, color: "red" })); } return; } if (isForce) {} else { p.note(colorize({ text: newfilepath, color: "cyan" }), colorize({ text: "Skipped (use --force to overwrite)", color: "yellow" })); return; } } else if (isRevert) return; const code = await fs.promises.readFile(filepath, "utf-8"); const codeTransfer = await transformCode(code, { filepath, type: suffix, debug: isDebug }); try { await fs.promises.writeFile(newfilepath, codeTransfer); p.note(colorize({ text: newfilepath, color: "cyan" }), colorize({ text: `Transfer succeeded${isForce ? " (overwritten)" : ""}`, color: "green" })); } catch (error) { p.note(colorize({ text: newfilepath, color: "cyan" }), colorize({ text: `Transfer failed: ${error}`, color: "red" })); } })); s.stop("Conversion completed."); p.outro("✅ All files have been processed."); } cli(); //#endregion export { cli };