@hemia/lume 0.0.5 → 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 +107 -87
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -477,102 +477,123 @@ ${result}`;
477
477
  }
478
478
  return result;
479
479
  }
480
- function extractColors(templateConfig) {
480
+ function parseTailwindTheme(config) {
481
481
  const colors = /* @__PURE__ */ new Map();
482
- const colorsMatch = templateConfig.match(/colors:\s*\{([\s\S]*?)\n\s*\}/);
483
- if (!colorsMatch) return colors;
484
- const colorsBlock = colorsMatch[1];
485
- const colorRegex = /([a-zA-Z0-9-]+):\s*\{([^}]+)\}/g;
486
- let match;
487
- while ((match = colorRegex.exec(colorsBlock)) !== null) {
488
- const colorName = match[1];
489
- const colorValue = match[2].trim();
490
- colors.set(colorName, colorValue);
491
- }
492
- return colors;
493
- }
494
- function extractBorderRadius(templateConfig) {
495
- const radius = /* @__PURE__ */ new Map();
496
- const radiusMatch = templateConfig.match(/borderRadius:\s*\{([\s\S]*?)\n\s*\}/);
497
- if (!radiusMatch) return radius;
498
- const radiusBlock = radiusMatch[1];
499
- const radiusRegex = /([a-zA-Z0-9-]+):\s*['"`]([^'"`]+)['"`]/g;
500
- let match;
501
- while ((match = radiusRegex.exec(radiusBlock)) !== null) {
502
- radius.set(match[1], match[2]);
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
+ );
503
566
  }
504
- return radius;
567
+ return result;
505
568
  }
506
569
  function mergeTailwindConfig(existingConfig, templateConfig) {
507
- const templateColors = extractColors(templateConfig);
508
- const templateRadius = extractBorderRadius(templateConfig);
570
+ const existing = parseTailwindTheme(existingConfig);
571
+ const template = parseTailwindTheme(templateConfig);
509
572
  let result = existingConfig;
510
573
  if (!result.includes("theme:")) {
511
574
  return templateConfig;
512
575
  }
513
- if (templateColors.size > 0) {
514
- for (const [colorName, colorValue] of templateColors) {
515
- if (!result.includes(`${colorName}:`)) {
516
- const themeMatch = result.match(/theme:\s*\{/);
517
- if (themeMatch) {
518
- if (result.includes("extend:")) {
519
- result = result.replace(
520
- /extend:\s*\{/,
521
- `extend: {
522
- colors: {
523
- ${colorName}: ${colorValue},
524
- },`
525
- );
526
- } else {
527
- result = result.replace(
528
- /theme:\s*\{/,
529
- `theme: {
530
- extend: {
531
- colors: {
532
- ${colorName}: ${colorValue},
533
- },`
534
- );
535
- }
536
- }
537
- }
538
- }
576
+ const mergedColors = /* @__PURE__ */ new Map();
577
+ for (const [name, value] of existing.colors) {
578
+ mergedColors.set(name, value);
539
579
  }
540
- if (templateRadius.size > 0) {
541
- for (const [radiusName, radiusValue] of templateRadius) {
542
- if (!result.includes(`${radiusName}:`)) {
543
- const themeMatch = result.match(/theme:\s*\{/);
544
- if (themeMatch) {
545
- if (result.includes("extend:")) {
546
- result = result.replace(
547
- /extend:\s*\{/,
548
- `extend: {
549
- borderRadius: {
550
- ${radiusName}: '${radiusValue}',
551
- },`
552
- );
553
- } else {
554
- result = result.replace(
555
- /theme:\s*\{/,
556
- `theme: {
557
- extend: {
558
- borderRadius: {
559
- ${radiusName}: '${radiusValue}',
560
- },`
561
- );
562
- }
563
- }
564
- }
565
- }
580
+ for (const [name, value] of template.colors) {
581
+ mergedColors.set(name, value);
566
582
  }
567
- if (!result.includes("darkMode:") && templateConfig.includes("darkMode:")) {
568
- const darkModeMatch = templateConfig.match(/darkMode:\s*\[[^\]]+\]/);
569
- if (darkModeMatch) {
570
- result = result.replace(
571
- /export default \{/,
572
- `export default {
573
- ${darkModeMatch[0]},`
574
- );
575
- }
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
+ );
576
597
  }
577
598
  return result;
578
599
  }
@@ -644,7 +665,6 @@ Options:
644
665
  process.exit(1);
645
666
  }
646
667
  const framework = detectFramework(cwd);
647
- const templateConfig = getTemplateConfig(framework);
648
668
  const cssPath = path4.resolve(cwd, configPaths.globalsCssPath);
649
669
  const tailwindPath = path4.resolve(cwd, configPaths.tailwindConfigPath);
650
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.5",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "files": ["dist"],
6
6
  "bin": {