transform-to-tailwindcss
Version:
🎨 Revolutionary CSS-to-TailwindCSS migration tool with surgical precision. Transform legacy stylesheets to utility-first classes instantly across Vue, React, Svelte, and Astro projects.
191 lines (187 loc) • 7.07 kB
JavaScript
import { TRANSFER_FLAG, __commonJS, __toESM, transformCode } from "./transformCode-Bi_lvCJI.js";
import fs from "node:fs";
import path from "node:path";
import process$1 from "node:process";
import fg from "fast-glob";
//#region node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js
var require_picocolors = __commonJS({ "node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js"(exports, module) {
let p = process || {}, argv = p.argv || [], env = p.env || {};
let isColorSupported = !(!!env.NO_COLOR || argv.includes("--no-color")) && (!!env.FORCE_COLOR || argv.includes("--color") || p.platform === "win32" || (p.stdout || {}).isTTY && env.TERM !== "dumb" || !!env.CI);
let formatter = (open, close, replace = open) => (input) => {
let string = "" + input, index = string.indexOf(close, open.length);
return ~index ? open + replaceClose(string, close, replace, index) + close : open + string + close;
};
let replaceClose = (string, close, replace, index) => {
let result = "", cursor = 0;
do {
result += string.substring(cursor, index) + replace;
cursor = index + close.length;
index = string.indexOf(close, cursor);
} while (~index);
return result + string.substring(cursor);
};
let createColors = (enabled = isColorSupported) => {
let f = enabled ? formatter : () => String;
return {
isColorSupported: enabled,
reset: f("\x1B[0m", "\x1B[0m"),
bold: f("\x1B[1m", "\x1B[22m", "\x1B[22m\x1B[1m"),
dim: f("\x1B[2m", "\x1B[22m", "\x1B[22m\x1B[2m"),
italic: f("\x1B[3m", "\x1B[23m"),
underline: f("\x1B[4m", "\x1B[24m"),
inverse: f("\x1B[7m", "\x1B[27m"),
hidden: f("\x1B[8m", "\x1B[28m"),
strikethrough: f("\x1B[9m", "\x1B[29m"),
black: f("\x1B[30m", "\x1B[39m"),
red: f("\x1B[31m", "\x1B[39m"),
green: f("\x1B[32m", "\x1B[39m"),
yellow: f("\x1B[33m", "\x1B[39m"),
blue: f("\x1B[34m", "\x1B[39m"),
magenta: f("\x1B[35m", "\x1B[39m"),
cyan: f("\x1B[36m", "\x1B[39m"),
white: f("\x1B[37m", "\x1B[39m"),
gray: f("\x1B[90m", "\x1B[39m"),
bgBlack: f("\x1B[40m", "\x1B[49m"),
bgRed: f("\x1B[41m", "\x1B[49m"),
bgGreen: f("\x1B[42m", "\x1B[49m"),
bgYellow: f("\x1B[43m", "\x1B[49m"),
bgBlue: f("\x1B[44m", "\x1B[49m"),
bgMagenta: f("\x1B[45m", "\x1B[49m"),
bgCyan: f("\x1B[46m", "\x1B[49m"),
bgWhite: f("\x1B[47m", "\x1B[49m"),
blackBright: f("\x1B[90m", "\x1B[39m"),
redBright: f("\x1B[91m", "\x1B[39m"),
greenBright: f("\x1B[92m", "\x1B[39m"),
yellowBright: f("\x1B[93m", "\x1B[39m"),
blueBright: f("\x1B[94m", "\x1B[39m"),
magentaBright: f("\x1B[95m", "\x1B[39m"),
cyanBright: f("\x1B[96m", "\x1B[39m"),
whiteBright: f("\x1B[97m", "\x1B[39m"),
bgBlackBright: f("\x1B[100m", "\x1B[49m"),
bgRedBright: f("\x1B[101m", "\x1B[49m"),
bgGreenBright: f("\x1B[102m", "\x1B[49m"),
bgYellowBright: f("\x1B[103m", "\x1B[49m"),
bgBlueBright: f("\x1B[104m", "\x1B[49m"),
bgMagentaBright: f("\x1B[105m", "\x1B[49m"),
bgCyanBright: f("\x1B[106m", "\x1B[49m"),
bgWhiteBright: f("\x1B[107m", "\x1B[49m")
};
};
module.exports = createColors();
module.exports.createColors = createColors;
} });
var import_picocolors = __toESM(require_picocolors(), 1);
//#endregion
//#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 import_picocolors.default) {
const method2 = getMethod(color, type);
return import_picocolors.default[method2](str);
}
if (color.startsWith("#")) {
const method2 = getMethod("hex", type);
return import_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 import_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 import_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 = import_picocolors.default.dim(result);
if (color) result = setColor(result, color, "foreground");
if (bgColor) result = setColor(result, bgColor, "background");
if (bold) result = import_picocolors.default.bold(result);
if (italic) result = import_picocolors.default.italic(result);
if (underline) result = import_picocolors.default.underline(result);
if (strikethrough) result = import_picocolors.default.strikethrough(result);
if (inverse) result = import_picocolors.default.inverse(result);
return result;
}
//#endregion
//#region src/cli.ts
const log = console.log;
async function cli() {
const asset = process$1.argv[2];
if (!asset) {
log(colorize({
text: "需要指定一个目录",
color: "red"
}));
return;
}
const fileDir = path.resolve(process$1.cwd(), asset);
const isRevert = process$1.argv[3] === "-r" || process$1.argv[3] === "--revert";
const entries = await fg([
"**.vue",
"**.tsx",
"**.html",
"**.svelte",
"**.astro"
], { cwd: fileDir });
entries.filter((entry) => !entry.endsWith(TRANSFER_FLAG)).forEach(async (entry) => {
const filepath = `${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);
log(colorize({
text: `${newfilepath} already revert`,
color: "green"
}));
} catch (error) {
log(colorize({
text: `revert failed: ${error}`,
color: "red"
}));
}
return;
}
log(colorize({
text: `${newfilepath} has transferred`,
color: "yellow"
}));
return;
} else if (isRevert) return;
const code = await fs.promises.readFile(filepath, "utf-8");
const codeTransfer = await transformCode(code, {
filepath,
type: suffix
});
try {
await fs.promises.writeFile(newfilepath, codeTransfer);
log(colorize({
text: `${newfilepath} transfer succeed`,
color: "green"
}));
} catch (error) {
log(colorize({
text: `${newfilepath} transfer failed: ${error}`,
color: "red"
}));
}
});
}
cli();
//#endregion
export { cli };