@hemia/lume 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +121 -36
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -477,37 +477,123 @@ ${result}`;
477
477
  }
478
478
  return result;
479
479
  }
480
+ function parseTailwindTheme(config) {
481
+ const colors = /* @__PURE__ */ new Map();
482
+ const borderRadius = /* @__PURE__ */ new Map();
483
+ let darkMode = null;
484
+ const darkModeMatch = config.match(/darkMode:\s*([^\n,]+)/);
485
+ if (darkModeMatch) {
486
+ darkMode = darkModeMatch[1].trim();
487
+ }
488
+ const colorsMatch = config.match(/colors:\s*\{([^}]+(?:\{[^}]+\})?[^}]*)\}/);
489
+ if (colorsMatch) {
490
+ const colorsBlock = colorsMatch[1];
491
+ const simpleColorRegex = /([a-zA-Z0-9-]+):\s*["']([^"']+)["']/g;
492
+ let match;
493
+ while ((match = simpleColorRegex.exec(colorsBlock)) !== null) {
494
+ colors.set(match[1], `"${match[2]}"`);
495
+ }
496
+ const nestedColorRegex = /([a-zA-Z0-9-]+):\s*\{[^}]*DEFAULT:\s*["']([^"']+)["']/g;
497
+ while ((match = nestedColorRegex.exec(colorsBlock)) !== null) {
498
+ colors.set(match[1], `{ DEFAULT: "${match[2]}" }`);
499
+ }
500
+ }
501
+ const radiusMatch = config.match(/borderRadius:\s*\{([^}]+)\}/);
502
+ if (radiusMatch) {
503
+ const radiusBlock = radiusMatch[1];
504
+ const radiusRegex = /([a-zA-Z0-9-]+):\s*["']([^"']+)["']/g;
505
+ let match;
506
+ while ((match = radiusRegex.exec(radiusBlock)) !== null) {
507
+ borderRadius.set(match[1], `"${match[2]}"`);
508
+ }
509
+ }
510
+ return { colors, borderRadius, darkMode };
511
+ }
512
+ function rebuildThemeExtend(existingConfig, mergedColors, mergedRadius) {
513
+ let result = existingConfig;
514
+ const colorsEntries = Array.from(mergedColors.entries()).map(([name, value]) => {
515
+ const needsQuotes = name.includes("-");
516
+ return needsQuotes ? ` "${name}": ${value},` : ` ${name}: ${value},`;
517
+ }).join("\n");
518
+ const radiusEntries = Array.from(mergedRadius.entries()).map(([name, value]) => ` ${name}: ${value},`).join("\n");
519
+ const hasColors = result.includes("colors:");
520
+ const hasRadius = result.includes("borderRadius:");
521
+ const hasExtend = result.includes("extend:");
522
+ if (!hasExtend) {
523
+ const extendBlock = `theme: {
524
+ extend: {
525
+ colors: {
526
+ ${colorsEntries}
527
+ },
528
+ borderRadius: {
529
+ ${radiusEntries}
530
+ },
531
+ },
532
+ }`;
533
+ return result.replace(/theme:\s*\{/, extendBlock);
534
+ }
535
+ if (hasColors) {
536
+ result = result.replace(
537
+ /colors:\s*\{[\s\S]*?\n\s{4}\}/,
538
+ `colors: {
539
+ ${colorsEntries}
540
+ }`
541
+ );
542
+ } else {
543
+ result = result.replace(
544
+ /extend:\s*\{/,
545
+ `extend: {
546
+ colors: {
547
+ ${colorsEntries}
548
+ },`
549
+ );
550
+ }
551
+ if (hasRadius) {
552
+ result = result.replace(
553
+ /borderRadius:\s*\{[\s\S]*?\n\s{4}\}/,
554
+ `borderRadius: {
555
+ ${radiusEntries}
556
+ }`
557
+ );
558
+ } else {
559
+ result = result.replace(
560
+ /extend:\s*\{/,
561
+ `extend: {
562
+ borderRadius: {
563
+ ${radiusEntries}
564
+ },`
565
+ );
566
+ }
567
+ return result;
568
+ }
480
569
  function mergeTailwindConfig(existingConfig, templateConfig) {
481
- const colorMatches = templateConfig.match(/colors:\s*\{([\s\S]*?)\}/);
482
- const radiusMatches = templateConfig.match(/borderRadius:\s*\{([\s\S]*?)\}/);
570
+ const existing = parseTailwindTheme(existingConfig);
571
+ const template = parseTailwindTheme(templateConfig);
483
572
  let result = existingConfig;
484
573
  if (!result.includes("theme:")) {
485
574
  return templateConfig;
486
575
  }
487
- if (colorMatches) {
488
- const colorsBlock = colorMatches[0];
489
- if (!result.includes("colors:")) {
490
- result = result.replace(
491
- /theme:\s*\{/,
492
- `theme: {
493
- extend: {
494
- colors: {${colorsBlock.replace("colors:", "")}`
495
- );
496
- }
576
+ const mergedColors = /* @__PURE__ */ new Map();
577
+ for (const [name, value] of existing.colors) {
578
+ mergedColors.set(name, value);
497
579
  }
498
- if (radiusMatches) {
499
- const radiusBlock = radiusMatches[0];
500
- if (!result.includes("borderRadius:")) {
501
- result = result.replace(
502
- /theme:\s*\{/,
503
- `theme: {
504
- extend: {
505
- borderRadius: {${radiusBlock.replace("borderRadius:", "")}`
506
- );
507
- }
580
+ for (const [name, value] of template.colors) {
581
+ mergedColors.set(name, value);
508
582
  }
509
- if (!result.includes("colors:") && colorMatches) {
510
- return templateConfig;
583
+ const mergedRadius = /* @__PURE__ */ new Map();
584
+ for (const [name, value] of existing.borderRadius) {
585
+ mergedRadius.set(name, value);
586
+ }
587
+ for (const [name, value] of template.borderRadius) {
588
+ mergedRadius.set(name, value);
589
+ }
590
+ result = rebuildThemeExtend(result, mergedColors, mergedRadius);
591
+ if (!result.includes("darkMode:") && template.darkMode) {
592
+ result = result.replace(
593
+ /export default \{/,
594
+ `export default {
595
+ darkMode: ${template.darkMode},`
596
+ );
511
597
  }
512
598
  return result;
513
599
  }
@@ -530,6 +616,15 @@ function detectFramework(cwd) {
530
616
  return "vue";
531
617
  }
532
618
  }
619
+ function detectTailwindConfigExtension(cwd) {
620
+ const extensions = ["js", "ts", "mjs", "cjs"];
621
+ for (const ext of extensions) {
622
+ if (fs4.existsSync(path4.resolve(cwd, `tailwind.config.${ext}`))) {
623
+ return ext;
624
+ }
625
+ }
626
+ return "js";
627
+ }
533
628
  async function addStyles(_args = [], options = {}) {
534
629
  if (options.silent === void 0 && process.argv.includes("--help") || process.argv.includes("-h")) {
535
630
  console.log(`
@@ -548,15 +643,8 @@ Options:
548
643
  return;
549
644
  }
550
645
  const cwd = options.cwd ? path4.resolve(options.cwd) : process.cwd();
551
- function detectTailwindConfigExtension(cwd2) {
552
- const extensions = ["js", "ts", "mjs", "cjs"];
553
- for (const ext of extensions) {
554
- if (fs4.existsSync(path4.resolve(cwd2, `tailwind.config.${ext}`))) {
555
- return ext;
556
- }
557
- }
558
- return "js";
559
- }
646
+ const tailwindExt = detectTailwindConfigExtension(cwd);
647
+ const tailwindTemplate = tailwindExt === "js" ? TAILWIND_CONFIG_TEMPLATE_JS : TAILWIND_CONFIG_TEMPLATE;
560
648
  const configPath = path4.resolve(cwd, "lume.config.json");
561
649
  if (!await fs4.pathExists(configPath)) {
562
650
  const cssPath2 = path4.resolve(cwd, "src/assets/globals.css");
@@ -577,9 +665,6 @@ Options:
577
665
  process.exit(1);
578
666
  }
579
667
  const framework = detectFramework(cwd);
580
- const templateConfig = getTemplateConfig(framework);
581
- const tailwindExt = detectTailwindConfigExtension(cwd);
582
- const tailwindTemplate = tailwindExt === "js" ? TAILWIND_CONFIG_TEMPLATE_JS : tailwindTemplate;
583
668
  const cssPath = path4.resolve(cwd, configPaths.globalsCssPath);
584
669
  const tailwindPath = path4.resolve(cwd, configPaths.tailwindConfigPath);
585
670
  const cssExists = await fs4.pathExists(cssPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/lume",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "files": ["dist"],
6
6
  "bin": {