@yamada-ui/cli 1.0.6 → 1.1.0-dev-20240806133333

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.
@@ -30,20 +30,26 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
- // ../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.2_tsx@4.16.2_typescript@5.5.4/node_modules/tsup/assets/cjs_shims.js
33
+ // ../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.3_@swc+helpers@0.5.5__jiti@1.21.6_postcss@8.4.40_tsx@4.16.2_typescript@5.5.4_yaml@2.5.0/node_modules/tsup/assets/cjs_shims.js
34
34
  var init_cjs_shims = __esm({
35
- "../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.2_tsx@4.16.2_typescript@5.5.4/node_modules/tsup/assets/cjs_shims.js"() {
35
+ "../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.3_@swc+helpers@0.5.5__jiti@1.21.6_postcss@8.4.40_tsx@4.16.2_typescript@5.5.4_yaml@2.5.0/node_modules/tsup/assets/cjs_shims.js"() {
36
36
  "use strict";
37
37
  }
38
38
  });
39
39
 
40
- // src/command/colors/index.ts
41
- var colors_exports = {};
42
- __export(colors_exports, {
43
- actionColors: () => actionColors
40
+ // src/command/theme/index.ts
41
+ var theme_exports = {};
42
+ __export(theme_exports, {
43
+ actionTheme: () => actionTheme
44
44
  });
45
- module.exports = __toCommonJS(colors_exports);
45
+ module.exports = __toCommonJS(theme_exports);
46
46
  init_cjs_shims();
47
+ var import_dns = __toESM(require("dns"));
48
+ var import_fs2 = require("fs");
49
+ var import_promises = require("fs/promises");
50
+ var import_node_stream = require("stream");
51
+ var import_promises2 = require("stream/promises");
52
+ var import_path2 = __toESM(require("path"));
47
53
  var p = __toESM(require("@clack/prompts"));
48
54
 
49
55
  // ../../node_modules/.pnpm/chalk@5.3.0/node_modules/chalk/source/index.js
@@ -541,8 +547,9 @@ var chalk = createChalk();
541
547
  var chalkStderr = createChalk({ level: stderrColor ? stderrColor.level : 0 });
542
548
  var source_default = chalk;
543
549
 
544
- // src/command/colors/index.ts
545
- var import_color2k = require("color2k");
550
+ // src/command/theme/index.ts
551
+ var import_tar = require("tar");
552
+ var import_typescript = require("typescript");
546
553
 
547
554
  // src/utils/prettier.ts
548
555
  init_cjs_shims();
@@ -563,53 +570,217 @@ var prettier = async (content, options) => {
563
570
  }
564
571
  };
565
572
 
566
- // src/command/colors/index.ts
567
- var tones = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
568
- var generateTones = (hex) => {
569
- let [h, s, l] = (0, import_color2k.parseToHsla)(hex);
570
- l *= 100;
571
- const d = l <= 50;
572
- const x = ((!d ? 100 : 95) - l) / 5;
573
- const y = (l - (d ? 5 : 15)) / 5;
574
- return tones.reduce(
575
- (prev, tone) => {
576
- const t = tone / 100;
577
- let z;
578
- if (t <= 5) {
579
- z = l + (5 - t) * x;
573
+ // src/utils/fs.ts
574
+ init_cjs_shims();
575
+ var import_fs = __toESM(require("fs"));
576
+ var isWriteable = async (directory) => {
577
+ try {
578
+ await import_fs.default.promises.access(directory, (import_fs.default.constants || import_fs.default).W_OK);
579
+ return true;
580
+ } catch (err) {
581
+ return false;
582
+ }
583
+ };
584
+
585
+ // src/command/theme/index.ts
586
+ var ORG_NAME = "yamada-ui";
587
+ var REPO_NAME = "yamada-ui";
588
+ var DEFAULT_BRANCH_NAME = "main";
589
+ var isOnline = () => {
590
+ return new Promise((resolve) => {
591
+ import_dns.default.lookup("github.com", (err) => {
592
+ if (err && err.code === "ENOTFOUND") {
593
+ resolve(false);
580
594
  } else {
581
- z = l - (t - 5) * y;
595
+ resolve(true);
582
596
  }
583
- z /= 100;
584
- prev[tone] = (0, import_color2k.toHex)((0, import_color2k.hsla)(h, s, z, 1));
585
- return prev;
586
- },
587
- {}
597
+ });
598
+ });
599
+ };
600
+ var checkHasReact = async (cwd) => {
601
+ try {
602
+ const packageJson = await (0, import_promises.readFile)(import_path2.default.join(cwd, "package.json"), "utf-8");
603
+ const { dependencies, devDependencies } = JSON.parse(packageJson);
604
+ return !!dependencies?.["@yamada-ui/react"] || !!devDependencies?.["@yamada-ui/react"];
605
+ } catch {
606
+ return false;
607
+ }
608
+ };
609
+ var getSource = async (branch) => {
610
+ if (!await isOnline()) {
611
+ throw new Error(
612
+ "No internet connection, please check your connection and try again."
613
+ );
614
+ }
615
+ const url = `https://codeload.github.com/${ORG_NAME}/${REPO_NAME}/tar.gz/${branch}`;
616
+ const { body } = await fetch(url);
617
+ if (!body) throw new Error(`Failed to download: ${url}`);
618
+ return import_node_stream.Readable.fromWeb(body);
619
+ };
620
+ var getFileMap = async (cwd, branch) => {
621
+ const source = await getSource(branch);
622
+ const fileMap = /* @__PURE__ */ new Map();
623
+ const targetPath = `${REPO_NAME}-${branch.replace("/", "-")}/packages/theme/src`;
624
+ const filter = (path3) => {
625
+ return path3.startsWith(targetPath);
626
+ };
627
+ const onReadEntry = (entry) => {
628
+ if (entry.type !== "Directory") {
629
+ const filePath = entry.path.replace(`${targetPath}/`, "");
630
+ let content = "";
631
+ entry.on("data", (chunk) => {
632
+ content += chunk.toString();
633
+ });
634
+ entry.on("end", () => {
635
+ fileMap.set(filePath, content);
636
+ });
637
+ }
638
+ };
639
+ await (0, import_promises2.pipeline)(
640
+ source,
641
+ (0, import_tar.t)({
642
+ cwd,
643
+ filter,
644
+ onReadEntry,
645
+ strip: 4,
646
+ newer: true
647
+ })
588
648
  );
649
+ return fileMap;
589
650
  };
590
- var actionColors = async (color, { name, out }) => {
591
- p.intro(source_default.magenta(`Generating Hue colors`));
651
+ var transformSourceFile = (sourceFile) => {
652
+ const ImportDeclarations = [];
653
+ const ImportTypeDeclarations = [];
654
+ const transformer = (context) => (sourceFile2) => {
655
+ const visitor = (node) => {
656
+ if ((0, import_typescript.isImportDeclaration)(node)) {
657
+ const moduleSpecifier = node.moduleSpecifier.getText().slice(1, -1);
658
+ if (moduleSpecifier.startsWith("@yamada-ui/")) {
659
+ if (node.importClause?.isTypeOnly) {
660
+ ImportTypeDeclarations.push(node);
661
+ } else {
662
+ ImportDeclarations.push(node);
663
+ }
664
+ return import_typescript.factory.createEmptyStatement();
665
+ }
666
+ }
667
+ return (0, import_typescript.visitEachChild)(node, visitor, context);
668
+ };
669
+ return (0, import_typescript.visitNode)(sourceFile2, visitor);
670
+ };
671
+ const { transformed } = (0, import_typescript.transform)(sourceFile, [transformer]);
672
+ const transformedSourceFile = transformed[0];
673
+ return { ImportDeclarations, ImportTypeDeclarations, transformedSourceFile };
674
+ };
675
+ var createImportDeclaration = (isTypeOnly, declarations) => {
676
+ const elements = declarations.flatMap(
677
+ ({ importClause: importClause2 }) => importClause2?.namedBindings && (0, import_typescript.isNamedImports)(importClause2.namedBindings) ? importClause2.namedBindings.elements : []
678
+ );
679
+ const namedBindings = import_typescript.factory.createNamedImports(elements);
680
+ const importClause = import_typescript.factory.createImportClause(
681
+ isTypeOnly,
682
+ void 0,
683
+ namedBindings
684
+ );
685
+ return import_typescript.factory.createImportDeclaration(
686
+ void 0,
687
+ importClause,
688
+ import_typescript.factory.createStringLiteral("@yamada-ui/react")
689
+ );
690
+ };
691
+ var replaceContent = (content) => {
692
+ const sourceFile = (0, import_typescript.createSourceFile)(
693
+ "temp.ts",
694
+ content,
695
+ import_typescript.ScriptTarget.Latest,
696
+ true
697
+ );
698
+ const printer = (0, import_typescript.createPrinter)({ newLine: import_typescript.NewLineKind.LineFeed });
699
+ const { ImportDeclarations, ImportTypeDeclarations, transformedSourceFile } = transformSourceFile(sourceFile);
700
+ const statements = [...transformedSourceFile.statements];
701
+ if (ImportTypeDeclarations.length) {
702
+ const newImportTypeDeclarations = createImportDeclaration(
703
+ true,
704
+ ImportTypeDeclarations
705
+ );
706
+ statements.unshift(newImportTypeDeclarations);
707
+ }
708
+ if (ImportDeclarations.length) {
709
+ const newImportDeclarations = createImportDeclaration(
710
+ false,
711
+ ImportDeclarations
712
+ );
713
+ statements.unshift(newImportDeclarations);
714
+ }
715
+ const updatedSourceFile = import_typescript.factory.updateSourceFile(
716
+ transformedSourceFile,
717
+ statements
718
+ );
719
+ let printedContent = printer.printFile(updatedSourceFile);
720
+ printedContent = printedContent.replace(/^;$/gm, "");
721
+ printedContent = printedContent.replace(/\s\n*(?=import)/g, "");
722
+ printedContent = printedContent.replace(/(import .*\n)+(?!import)/, "$&\n");
723
+ return printedContent;
724
+ };
725
+ var replaceIndex = (content) => {
726
+ const index = content.indexOf("export const baseTheme");
727
+ content = content.slice(0, index);
728
+ content = content.replace(/export const baseTheme/, "export const theme");
729
+ content = content.replace(/\nimport { config } from ".\/config"/, "");
730
+ content += "\nexport { config } from './config'";
731
+ return content;
732
+ };
733
+ var generateTheme = async (outPath, fileMap, isReactReplace, isForceReplace) => {
734
+ if (!await isWriteable(import_path2.default.dirname(outPath))) {
735
+ throw new Error(
736
+ "The provided path is not writable, please check folder permissions and try again.\nIt is likely you do not have write permissions for this folder."
737
+ );
738
+ }
739
+ await (0, import_promises.mkdir)(outPath, { recursive: true });
740
+ await Promise.all(
741
+ [...fileMap.entries()].map(async ([filePath, _content]) => {
742
+ const targetPath = import_path2.default.join(outPath, filePath);
743
+ let content = _content;
744
+ if (filePath === "index.ts") {
745
+ content = replaceIndex(content);
746
+ } else {
747
+ if (isReactReplace) content = replaceContent(content);
748
+ }
749
+ const formattedContent = await prettier(content);
750
+ try {
751
+ if (!isForceReplace && (0, import_fs2.existsSync)(targetPath)) return;
752
+ await (0, import_promises.writeFile)(targetPath, formattedContent, "utf-8");
753
+ } catch (e) {
754
+ if (e instanceof Error && "code" in e && e.code === "ENOENT") {
755
+ const dirPath = import_path2.default.dirname(targetPath);
756
+ await (0, import_promises.mkdir)(dirPath, { recursive: true });
757
+ if (!isForceReplace && (0, import_fs2.existsSync)(targetPath)) return;
758
+ await (0, import_promises.writeFile)(targetPath, formattedContent, "utf-8");
759
+ }
760
+ }
761
+ })
762
+ );
763
+ };
764
+ var actionTheme = async (_outPath, { branch = DEFAULT_BRANCH_NAME, cwd, replace = false }) => {
765
+ p.intro(source_default.magenta(`Generating Yamada UI theme`));
592
766
  const s = p.spinner();
593
767
  try {
594
768
  const start = process.hrtime.bigint();
595
- s.start(`Computing the color`);
596
- const contents = generateTones(color);
597
- let result = contents;
598
- if (name) result = { [name]: result };
599
- let data = await prettier(JSON.stringify(result), { parser: "json" });
600
- if (name)
601
- data = data.replace(/^{/, "").replace(/}\s+$/, "").replace(/\s+}/g, "\n}");
602
- s.stop(`Computed the color`);
603
- p.note(
604
- Object.entries(contents).map(
605
- ([hue, color2]) => `- ${hue.length === 2 ? " " : ""}${hue}: ${color2}`
606
- ).join("\n"),
607
- "Generated the colors"
608
- );
769
+ cwd ??= import_path2.default.resolve();
770
+ const outPath = import_path2.default.join(cwd, _outPath);
771
+ s.start(`Checking "package.json"`);
772
+ const hasReact = await checkHasReact(cwd);
773
+ s.stop(`Checked "package.json"`);
774
+ s.start(`Downloading the theme`);
775
+ const fileMap = await getFileMap(outPath, branch);
776
+ s.stop(`Downloaded the theme`);
777
+ s.start(`Writing the theme "${outPath}"`);
778
+ await generateTheme(outPath, fileMap, hasReact, replace);
779
+ s.stop(`Wrote the theme`);
609
780
  const end = process.hrtime.bigint();
610
781
  const duration = (Number(end - start) / 1e9).toFixed(2);
611
- p.outro(source_default.green(`Done in ${duration}s${!out ? "\n" : ""}`));
612
- if (out) console.log(data.trim(), "\n");
782
+ p.outro(source_default.green(`Done in ${duration}s
783
+ `));
613
784
  } catch (e) {
614
785
  s.stop(`An error occurred`, 500);
615
786
  p.cancel(source_default.red(e instanceof Error ? e.message : "Message is missing"));
@@ -617,5 +788,5 @@ var actionColors = async (color, { name, out }) => {
617
788
  };
618
789
  // Annotate the CommonJS export names for ESM import in node:
619
790
  0 && (module.exports = {
620
- actionColors
791
+ actionTheme
621
792
  });
@@ -30,9 +30,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
- // ../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.2_tsx@4.16.2_typescript@5.5.4/node_modules/tsup/assets/cjs_shims.js
33
+ // ../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.3_@swc+helpers@0.5.5__jiti@1.21.6_postcss@8.4.40_tsx@4.16.2_typescript@5.5.4_yaml@2.5.0/node_modules/tsup/assets/cjs_shims.js
34
34
  var init_cjs_shims = __esm({
35
- "../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.2_tsx@4.16.2_typescript@5.5.4/node_modules/tsup/assets/cjs_shims.js"() {
35
+ "../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.3_@swc+helpers@0.5.5__jiti@1.21.6_postcss@8.4.40_tsx@4.16.2_typescript@5.5.4_yaml@2.5.0/node_modules/tsup/assets/cjs_shims.js"() {
36
36
  "use strict";
37
37
  }
38
38
  });
@@ -30,9 +30,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
- // ../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.2_tsx@4.16.2_typescript@5.5.4/node_modules/tsup/assets/cjs_shims.js
33
+ // ../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.3_@swc+helpers@0.5.5__jiti@1.21.6_postcss@8.4.40_tsx@4.16.2_typescript@5.5.4_yaml@2.5.0/node_modules/tsup/assets/cjs_shims.js
34
34
  var init_cjs_shims = __esm({
35
- "../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.2_tsx@4.16.2_typescript@5.5.4/node_modules/tsup/assets/cjs_shims.js"() {
35
+ "../../node_modules/.pnpm/tsup@8.2.3_@swc+core@1.7.3_@swc+helpers@0.5.5__jiti@1.21.6_postcss@8.4.40_tsx@4.16.2_typescript@5.5.4_yaml@2.5.0/node_modules/tsup/assets/cjs_shims.js"() {
36
36
  "use strict";
37
37
  }
38
38
  });
@@ -822,9 +822,9 @@ var generateThemeTypings = async ({
822
822
  p.cancel(source_default.red(e instanceof Error ? e.message : "Message is missing"));
823
823
  }
824
824
  };
825
- var actionTokens = async (themeFile, { out: outFile, watch: watchFile }) => {
825
+ var actionTokens = async (themePath2, { out: outFile, watch: watchFile }) => {
826
826
  const readFile = async () => {
827
- const filePath = import_path3.default.resolve(themeFile);
827
+ const filePath = import_path3.default.resolve(themePath2);
828
828
  const { mod: theme, dependencies } = await (0, import_bundle_n_require.bundleNRequire)(filePath);
829
829
  return { theme, dependencies };
830
830
  };