"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const fs = require("node:fs"); const path = require("node:path"); const zod = require("zod"); const fs$1 = require("node:fs/promises"); function _interopNamespaceDefault(e) { const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } }); if (e) { for (const k in e) { if (k !== "default") { const d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: () => e[k] }); } } } n.default = e; return Object.freeze(n); } const path__namespace = /* @__PURE__ */ _interopNamespaceDefault(path); const configSchema = zod.z.object({ files: zod.z.array(zod.z.string()).optional().default([]), output: zod.z.string().optional().default("./src/cssVar.gen.ts"), fileHeader: zod.z.array(zod.z.string()).optional().default([ "/* prettier-ignore-start */", "/* eslint-disable */", "// @ts-nocheck", "// noinspection JSUnusedGlobalSymbols" ]), fileFooter: zod.z.array(zod.z.string()).optional().default(["/* prettier-ignore-end */"]), disableLogging: zod.z.boolean().optional().default(false), emoji: zod.z.boolean().optional().default(true) }); const getConfig = (noCreate, inlineConfig = {}, configDirectory) => { const configFilePathJson = path__namespace.resolve( configDirectory ?? process.cwd(), "cve.config.json" ); const exists = fs.existsSync(configFilePathJson); const env = process.env.CVE_CONFIG; const envConfig = env ? JSON.parse(env) : {}; let config; if (exists) { config = configSchema.parse({ ...JSON.parse(fs.readFileSync(configFilePathJson, "utf-8")), ...inlineConfig, ...envConfig }); } else { config = configSchema.parse({ ...inlineConfig, ...envConfig }); if (!noCreate) { fs.writeFileSync( configFilePathJson, JSON.stringify(config, null, 2), "utf-8" ); } } if (configDirectory) { if (path__namespace.isAbsolute(configDirectory)) { config.files = config.files.map( (file) => path__namespace.resolve(configDirectory, file) ); config.output = path__namespace.resolve(configDirectory, config.output); } else { config.files = config.files.map( (file) => path__namespace.resolve(process.cwd(), configDirectory, file) ); config.output = path__namespace.resolve( process.cwd(), configDirectory, config.output ); } } return config; }; const extractCssVars = (css) => { var _a, _b; const rootRegex = /([^{}]*)\{([^{}]*)}/g; const cssVars = {}; while (true) { const root = rootRegex.exec(css); if (!root) break; const condition = ((_b = (_a = root[1]) == null ? void 0 : _a.trim()) == null ? void 0 : _b.replace("\n", " ")) ?? ""; if (!root[2]) break; const cssVarRegex = /\s*(--[^;]*):([^;]*);?\n*/g; while (true) { const cssVar = cssVarRegex.exec(root[2]); if (!cssVar || !cssVar[1] || !cssVar[2]) break; const name = cssVar[1].trim(); cssVars[name] = { [condition]: cssVar[2].trim(), ...cssVars[name] }; } } return cssVars; }; const generateCode = (cssVars) => { return Object.entries(cssVars).map(([name, values]) => { const output = []; for (const [condition, value] of Object.entries(values)) { if (condition) { output.push(`// ${condition}: ${value}`); } else { output.push(`// ${value}`); } } output.push( `export const ${name.substring(2).replaceAll("-", "_")} = "var(${name})"` ); return output.join("\n"); }).join("\n\n"); }; const logging = (config) => { const icon = config.emoji ? "🍄" : "*"; return { log: (message) => { if (!config.disabled) console.log(`${icon} ${message}`); }, info: (message) => { if (!config.disabled) console.info(`${icon} ${message}`); }, warn: (message) => { if (!config.disabled) console.warn(`${icon} ${message}`); }, error: (message) => { if (!config.disabled) console.error(`${icon} ${message}`); } }; }; let isFirst = true; const generator = async (config) => { const logger = logging({ disabled: config.disableLogging, emoji: config.emoji }); if (isFirst) { logger.log("Generating css var files..."); isFirst = false; } else { logger.log("Regenerating css var files..."); } const cssVars = await Promise.all( config.files.map((file) => fs$1.readFile(file, "utf-8")).map((content) => content.then(extractCssVars)) ); if (cssVars.length) { logger.info(`Found css vars: ${cssVars.length}`); } else if (config.files.length) { logger.warn("Not found css vars."); } else { logger.warn("Files options is empty."); } const code = generateCode(Object.assign({}, ...cssVars)); await fs$1.writeFile( config.output, // biome-ignore lint/style/useTemplate: [ ...config.fileHeader, "// This file is auto-generated by css-var-extract", code ? code : [ "// !! Warning: not found css vars", "// Please check files option in cve.config.json" ].join("\n"), ...config.fileFooter ].join("\n\n") + "\n", "utf-8" ); }; exports.configSchema = configSchema; exports.extractCssVars = extractCssVars; exports.generateCode = generateCode; exports.generator = generator; exports.getConfig = getConfig; //# sourceMappingURL=index.cjs.map