@react-native-styled-system/cli 0.0.5

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.
package/bin/index.js ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+
4
+ import { run } from '../dist/index.js';
5
+
6
+ run().catch((e) => {
7
+ console.error(e);
8
+ process.exit(1);
9
+ });
@@ -0,0 +1,65 @@
1
+ import {
2
+ extractPropertyKeys
3
+ } from "./chunk-4EHPPKPJ.js";
4
+ import {
5
+ extractPropertyPaths,
6
+ printUnionMap
7
+ } from "./chunk-4SAUFT3V.js";
8
+ import {
9
+ extractSemanticTokenKeys
10
+ } from "./chunk-MSTYDDL5.js";
11
+ import {
12
+ isObject
13
+ } from "./chunk-U3IUZ4GA.js";
14
+
15
+ // src/create-theme-typings-interface.ts
16
+ function applyThemeTypingTemplate(typingContent, template) {
17
+ switch (template) {
18
+ case "augmentation":
19
+ return `// regenerate by running
20
+ // npx @chakra-ui/cli tokens path/to/your/theme.(js|ts) --template augmentation --out path/to/this/file
21
+ import { BaseThemeTypings } from "@chakra-ui/styled-system";
22
+ declare module "@chakra-ui/styled-system" {
23
+ export interface CustomThemeTypings extends BaseThemeTypings {
24
+ ${typingContent}
25
+ }
26
+ }
27
+ `;
28
+ case "default":
29
+ default:
30
+ return `// regenerate by running
31
+ // npx @chakra-ui/cli tokens path/to/your/theme.(js|ts)
32
+ import { BaseThemeTypings } from "./shared.types.js"
33
+ export interface ThemeTypings extends BaseThemeTypings {
34
+ ${typingContent}
35
+ }
36
+ `;
37
+ }
38
+ }
39
+ async function createThemeTypingsInterface(theme, { config }) {
40
+ const unions = config.reduce(
41
+ (allUnions, { key, maxScanDepth, filter = () => true, flatMap = (value) => value }) => {
42
+ const target = theme[key];
43
+ allUnions[key] = [];
44
+ if (isObject(target) || Array.isArray(target)) {
45
+ allUnions[key] = extractPropertyPaths(target, maxScanDepth).filter(filter).flatMap(flatMap);
46
+ }
47
+ if (isObject(theme.semanticTokens)) {
48
+ const semanticTokenKeys = extractSemanticTokenKeys(theme, key).filter(filter).flatMap(flatMap);
49
+ allUnions[key].push(...semanticTokenKeys);
50
+ }
51
+ return allUnions;
52
+ },
53
+ {}
54
+ );
55
+ const typography = extractPropertyKeys(theme, "typography");
56
+ const typingContent = `${printUnionMap(
57
+ { ...unions, typography },
58
+ (targetKey) => targetKey === "conditions"
59
+ )}`;
60
+ return applyThemeTypingTemplate(typingContent, "default");
61
+ }
62
+
63
+ export {
64
+ createThemeTypingsInterface
65
+ };
@@ -0,0 +1,46 @@
1
+ import {
2
+ resolveOutputPath
3
+ } from "./chunk-G4LOGXG4.js";
4
+ import {
5
+ themeKeyConfiguration
6
+ } from "./chunk-4SXWRGUG.js";
7
+ import {
8
+ createThemeTypingsInterface
9
+ } from "./chunk-2TSYFGEF.js";
10
+
11
+ // src/generate-theme-typings.ts
12
+ import { writeFileSync } from "node:fs";
13
+ async function generateThemeTypings({
14
+ theme,
15
+ out,
16
+ onError
17
+ }) {
18
+ const { default: ora } = await import("ora");
19
+ const spinner = ora("Generating Theme Typings").start();
20
+ try {
21
+ const start = process.hrtime.bigint();
22
+ const typings = await createThemeTypingsInterface(theme, {
23
+ config: themeKeyConfiguration
24
+ });
25
+ const outPath = await resolveOutputPath(out);
26
+ spinner.info();
27
+ spinner.text = `Write file "${outPath}"...`;
28
+ writeFileSync(outPath, typings, "utf8");
29
+ const end = process.hrtime.bigint();
30
+ const duration = (Number(end - start) / 1e9).toFixed(2);
31
+ spinner.succeed(`Done in ${duration}s`);
32
+ } catch (e) {
33
+ spinner.fail("An error occurred");
34
+ if (e instanceof Error) {
35
+ console.error(e.message);
36
+ }
37
+ spinner.stop();
38
+ onError?.();
39
+ } finally {
40
+ spinner.stop();
41
+ }
42
+ }
43
+
44
+ export {
45
+ generateThemeTypings
46
+ };
@@ -0,0 +1,16 @@
1
+ import {
2
+ isObject
3
+ } from "./chunk-U3IUZ4GA.js";
4
+
5
+ // src/extract-property-keys.ts
6
+ function extractPropertyKeys(theme, themePropertyName) {
7
+ const themeProperty = theme[themePropertyName];
8
+ if (!isObject(themeProperty)) {
9
+ return [];
10
+ }
11
+ return Object.keys(themeProperty);
12
+ }
13
+
14
+ export {
15
+ extractPropertyKeys
16
+ };
@@ -0,0 +1,37 @@
1
+ import {
2
+ printUnionType
3
+ } from "./chunk-CHKLAMB6.js";
4
+ import {
5
+ isObject
6
+ } from "./chunk-U3IUZ4GA.js";
7
+
8
+ // src/extract-property-paths.ts
9
+ function printUnionMap(unions, strict = false) {
10
+ return Object.entries(unions).sort(([a], [b]) => a.localeCompare(b)).map(([targetKey, union]) => {
11
+ const isStrict = typeof strict === "function" ? strict(targetKey) : strict;
12
+ return `${targetKey}: ${printUnionType(union, isStrict)};`;
13
+ }).join("\n");
14
+ }
15
+ function extractPropertyPaths(target, maxDepth = 3) {
16
+ if (!isObject(target) && !Array.isArray(target) || !maxDepth) {
17
+ return [];
18
+ }
19
+ return Object.entries(target).reduce((allPropertyPaths, [key, value]) => {
20
+ if (isObject(value)) {
21
+ extractPropertyPaths(value, maxDepth - 1).forEach(
22
+ (childKey) => (
23
+ // e.g. gray.500
24
+ allPropertyPaths.push(`${key}.${childKey}`)
25
+ )
26
+ );
27
+ } else {
28
+ allPropertyPaths.push(key);
29
+ }
30
+ return allPropertyPaths;
31
+ }, []);
32
+ }
33
+
34
+ export {
35
+ printUnionMap,
36
+ extractPropertyPaths
37
+ };
@@ -0,0 +1,11 @@
1
+ // src/config.ts
2
+ var themeKeyConfiguration = [
3
+ { key: "colors", maxScanDepth: 3 },
4
+ { key: "radii" },
5
+ { key: "sizes", maxScanDepth: 2 },
6
+ { key: "space", flatMap: (value) => [value, `-${value}`] }
7
+ ];
8
+
9
+ export {
10
+ themeKeyConfiguration
11
+ };
@@ -0,0 +1,17 @@
1
+ import {
2
+ isObject
3
+ } from "./chunk-U3IUZ4GA.js";
4
+
5
+ // src/utils/is-color-hue.ts
6
+ var colorHueKeys = ["50", "100", "200", "300", "400", "500", "600", "700", "800", "900"];
7
+ function isColorHue(value) {
8
+ if (!isObject(value)) {
9
+ return false;
10
+ }
11
+ const keys = Object.keys(value);
12
+ return colorHueKeys.every((key) => keys.includes(key));
13
+ }
14
+
15
+ export {
16
+ isColorHue
17
+ };
@@ -0,0 +1,13 @@
1
+ // src/utils/print-union-type.ts
2
+ var AutoCompleteStringType = "(string & {})";
3
+ var wrapWithQuotes = (value) => `"${value}"`;
4
+ function printUnionType(values, strict = false) {
5
+ if (!values.length) {
6
+ return strict ? "never" : AutoCompleteStringType;
7
+ }
8
+ return values.map(wrapWithQuotes).concat(strict ? [] : [AutoCompleteStringType]).join(" | ");
9
+ }
10
+
11
+ export {
12
+ printUnionType
13
+ };
@@ -0,0 +1,54 @@
1
+ // src/utils/init-cli.ts
2
+ import welcome from "cli-welcome";
3
+ var pkgJSON = {
4
+ name: "react-native-themed-styled-system-cli",
5
+ version: "0.0.4",
6
+ description: "Generate theme typings for autocomplete",
7
+ type: "module",
8
+ main: "dist/cjs/index.cjs",
9
+ module: "dist/esm/index.mjs",
10
+ types: "dist/types/index.d.ts",
11
+ scripts: {
12
+ "build": "rm -rf dist && tsup src --format esm --shims && yarn pack",
13
+ "check:type": "tsc --noEmit"
14
+ },
15
+ dependencies: {
16
+ "bundle-n-require": "^1.0.1",
17
+ "chokidar": "^3.5.3",
18
+ "cli-welcome": "^2.2.2",
19
+ "commander": "^11.0.0",
20
+ "ora": "^7.0.1",
21
+ "prettier": "^3.0.2",
22
+ "update-notifier": "^6.0.2"
23
+ },
24
+ files: ["dist", "bin"],
25
+ bin: "bin/index.js",
26
+ devDependencies: {
27
+ "@types/ora": "^3.2.0",
28
+ "@types/update-notifier": "6.0.4",
29
+ "tsup": "^8.0.2",
30
+ "typescript": "5.2.2"
31
+ },
32
+ packageManager: "yarn@4.1.1"
33
+ };
34
+ async function initCLI() {
35
+ const { default: updateNotifier } = await import("update-notifier");
36
+ welcome({
37
+ title: "React Native Styled System CLI",
38
+ tagLine: "by MJStudio",
39
+ bgColor: "#319795",
40
+ color: "#FFFFFF",
41
+ bold: true,
42
+ clear: false
43
+ });
44
+ updateNotifier({
45
+ pkg: pkgJSON,
46
+ shouldNotifyInNpmScript: true,
47
+ updateCheckInterval: 1e3 * 60 * 60 * 24 * 3
48
+ // 3 days
49
+ }).notify({ isGlobal: true, message: "New version of CLI available" });
50
+ }
51
+
52
+ export {
53
+ initCLI
54
+ };
@@ -0,0 +1,50 @@
1
+ // src/resolve-output-path.ts
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { promisify } from "node:util";
5
+ var exists = promisify(fs.exists);
6
+ var themeInterfaceDestination = [
7
+ "node_modules",
8
+ "@chakra-ui",
9
+ "styled-system",
10
+ "dist",
11
+ "types",
12
+ "theming.types.d.ts"
13
+ ];
14
+ async function resolveThemingDefinitionPath() {
15
+ const baseDir = path.join("..", "..", "..");
16
+ const cwd = process.cwd();
17
+ const pathsToTry = [
18
+ path.resolve(baseDir, "..", ...themeInterfaceDestination),
19
+ path.resolve(baseDir, "..", "..", ...themeInterfaceDestination),
20
+ path.resolve(cwd, ...themeInterfaceDestination),
21
+ path.resolve(cwd, "..", ...themeInterfaceDestination),
22
+ path.resolve(cwd, "..", "..", ...themeInterfaceDestination)
23
+ ];
24
+ const triedPaths = await Promise.all(
25
+ pathsToTry.map(async (possiblePath) => {
26
+ if (await exists(possiblePath)) {
27
+ return possiblePath;
28
+ }
29
+ return "";
30
+ })
31
+ );
32
+ return triedPaths.find(Boolean);
33
+ }
34
+ async function resolveOutputPath(overridePath) {
35
+ if (overridePath) {
36
+ return path.resolve(process.cwd(), overridePath);
37
+ }
38
+ const themingDefinitionFilePath = await resolveThemingDefinitionPath();
39
+ if (!themingDefinitionFilePath) {
40
+ throw new Error(
41
+ "Could not find @chakra-ui/styled-system in node_modules. Please provide `--out` parameter."
42
+ );
43
+ }
44
+ return themingDefinitionFilePath;
45
+ }
46
+
47
+ export {
48
+ themeInterfaceDestination,
49
+ resolveOutputPath
50
+ };
@@ -0,0 +1,35 @@
1
+ import {
2
+ isObject
3
+ } from "./chunk-U3IUZ4GA.js";
4
+
5
+ // src/extract-semantic-token-keys.ts
6
+ var hasSemanticTokens = (theme) => isObject(theme.semanticTokens);
7
+ function extractSemanticTokenKeys(theme, themePropertyName) {
8
+ if (!hasSemanticTokens(theme)) {
9
+ return [];
10
+ }
11
+ const themeProperty = theme["semanticTokens"][themePropertyName];
12
+ if (!isObject(themeProperty)) {
13
+ return [];
14
+ }
15
+ return Object.keys(flattenSemanticTokens(themeProperty));
16
+ }
17
+ function flattenSemanticTokens(target) {
18
+ if (!isObject(target) && !Array.isArray(target)) {
19
+ return target;
20
+ }
21
+ return Object.entries(target).reduce((result, [key, value]) => {
22
+ if (isObject(value) && !("default" in value) || Array.isArray(value)) {
23
+ Object.entries(flattenSemanticTokens(value)).forEach(([childKey, childValue]) => {
24
+ result[`${key}.${childKey}`] = childValue;
25
+ });
26
+ } else {
27
+ result[key] = value;
28
+ }
29
+ return result;
30
+ }, {});
31
+ }
32
+
33
+ export {
34
+ extractSemanticTokenKeys
35
+ };
@@ -0,0 +1,9 @@
1
+ // src/utils/is-object.ts
2
+ function isObject(value) {
3
+ const type = typeof value;
4
+ return value != null && (type === "object" || type === "function") && !Array.isArray(value);
5
+ }
6
+
7
+ export {
8
+ isObject
9
+ };
package/dist/config.js ADDED
@@ -0,0 +1,6 @@
1
+ import {
2
+ themeKeyConfiguration
3
+ } from "./chunk-4SXWRGUG.js";
4
+ export {
5
+ themeKeyConfiguration
6
+ };
@@ -0,0 +1,11 @@
1
+ import {
2
+ createThemeTypingsInterface
3
+ } from "./chunk-2TSYFGEF.js";
4
+ import "./chunk-4EHPPKPJ.js";
5
+ import "./chunk-4SAUFT3V.js";
6
+ import "./chunk-CHKLAMB6.js";
7
+ import "./chunk-MSTYDDL5.js";
8
+ import "./chunk-U3IUZ4GA.js";
9
+ export {
10
+ createThemeTypingsInterface
11
+ };
@@ -0,0 +1,23 @@
1
+ import {
2
+ isColorHue
3
+ } from "./chunk-5IH5T2QU.js";
4
+ import {
5
+ isObject
6
+ } from "./chunk-U3IUZ4GA.js";
7
+
8
+ // src/extract-color-schemes.ts
9
+ function extractColorSchemeTypes(theme) {
10
+ const { colors } = theme;
11
+ if (!isObject(colors)) {
12
+ return [];
13
+ }
14
+ return Object.entries(colors).reduce((acc, [colorName, colorValues]) => {
15
+ if (isColorHue(colorValues)) {
16
+ acc.push(colorName);
17
+ }
18
+ return acc;
19
+ }, []);
20
+ }
21
+ export {
22
+ extractColorSchemeTypes
23
+ };
@@ -0,0 +1,45 @@
1
+ import {
2
+ printUnionMap
3
+ } from "./chunk-4SAUFT3V.js";
4
+ import "./chunk-CHKLAMB6.js";
5
+ import {
6
+ isObject
7
+ } from "./chunk-U3IUZ4GA.js";
8
+
9
+ // src/extract-component-types.ts
10
+ function extractComponentTypes(theme) {
11
+ const components = theme.components;
12
+ if (!isObject(components)) {
13
+ return {};
14
+ }
15
+ return Object.entries(components).reduce(
16
+ (allDefs, [componentName, definition]) => {
17
+ if (definition) {
18
+ allDefs[componentName] = {
19
+ sizes: Object.keys(definition.sizes ?? {}),
20
+ variants: Object.keys(definition.variants ?? {})
21
+ };
22
+ }
23
+ return allDefs;
24
+ },
25
+ {}
26
+ );
27
+ }
28
+ function esc(name) {
29
+ return name.match(/^[a-zA-Z0-9\-_]+$/) ? name : `"${name}"`;
30
+ }
31
+ function printComponentTypes(componentTypes, strict = false) {
32
+ const types = Object.entries(componentTypes).map(
33
+ ([componentName, unions]) => `${esc(componentName)}: {
34
+ ${printUnionMap(unions, strict)}
35
+ }`
36
+ ).join("\n");
37
+ return `components: {
38
+ ${types}
39
+ }
40
+ `;
41
+ }
42
+ export {
43
+ extractComponentTypes,
44
+ printComponentTypes
45
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ extractPropertyKeys
3
+ } from "./chunk-4EHPPKPJ.js";
4
+ import "./chunk-U3IUZ4GA.js";
5
+ export {
6
+ extractPropertyKeys
7
+ };
@@ -0,0 +1,10 @@
1
+ import {
2
+ extractPropertyPaths,
3
+ printUnionMap
4
+ } from "./chunk-4SAUFT3V.js";
5
+ import "./chunk-CHKLAMB6.js";
6
+ import "./chunk-U3IUZ4GA.js";
7
+ export {
8
+ extractPropertyPaths,
9
+ printUnionMap
10
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ extractSemanticTokenKeys
3
+ } from "./chunk-MSTYDDL5.js";
4
+ import "./chunk-U3IUZ4GA.js";
5
+ export {
6
+ extractSemanticTokenKeys
7
+ };
@@ -0,0 +1,17 @@
1
+ import {
2
+ generateThemeTypings
3
+ } from "./chunk-43T6TWGS.js";
4
+ import {
5
+ themeInterfaceDestination
6
+ } from "./chunk-G4LOGXG4.js";
7
+ import "./chunk-4SXWRGUG.js";
8
+ import "./chunk-2TSYFGEF.js";
9
+ import "./chunk-4EHPPKPJ.js";
10
+ import "./chunk-4SAUFT3V.js";
11
+ import "./chunk-CHKLAMB6.js";
12
+ import "./chunk-MSTYDDL5.js";
13
+ import "./chunk-U3IUZ4GA.js";
14
+ export {
15
+ generateThemeTypings,
16
+ themeInterfaceDestination
17
+ };
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ import {
2
+ initCLI
3
+ } from "./chunk-EP566NIH.js";
4
+ import {
5
+ generateThemeTypings
6
+ } from "./chunk-43T6TWGS.js";
7
+ import "./chunk-G4LOGXG4.js";
8
+ import "./chunk-4SXWRGUG.js";
9
+ import "./chunk-2TSYFGEF.js";
10
+ import "./chunk-4EHPPKPJ.js";
11
+ import "./chunk-4SAUFT3V.js";
12
+ import "./chunk-CHKLAMB6.js";
13
+ import "./chunk-MSTYDDL5.js";
14
+ import "./chunk-U3IUZ4GA.js";
15
+
16
+ // src/index.ts
17
+ import { bundleNRequire } from "bundle-n-require";
18
+ import { program } from "commander";
19
+ import { resolve } from "node:path/posix";
20
+ async function run() {
21
+ await initCLI();
22
+ program.command("generate <source>").option("--out <path>").option("--no-format", "Disable auto formatting").action(async (themeFile, options) => {
23
+ const { out } = options;
24
+ const read = async () => {
25
+ const filePath = resolve(themeFile);
26
+ const { mod, dependencies } = await bundleNRequire(filePath);
27
+ const theme = mod.default || mod.theme || mod;
28
+ return { theme, dependencies };
29
+ };
30
+ const ctx = await read();
31
+ const build = async () => {
32
+ await generateThemeTypings({
33
+ theme: ctx.theme,
34
+ out
35
+ });
36
+ };
37
+ await build();
38
+ });
39
+ program.parse();
40
+ }
41
+ export {
42
+ run
43
+ };
@@ -0,0 +1,8 @@
1
+ import {
2
+ resolveOutputPath,
3
+ themeInterfaceDestination
4
+ } from "./chunk-G4LOGXG4.js";
5
+ export {
6
+ resolveOutputPath,
7
+ themeInterfaceDestination
8
+ };
@@ -0,0 +1,16 @@
1
+ // src/utils/format-with-prettier.ts
2
+ import { format, resolveConfig } from "prettier";
3
+ async function formatWithPrettier(content) {
4
+ const prettierConfig = await resolveConfig(process.cwd());
5
+ try {
6
+ return await format(String(content), {
7
+ ...prettierConfig,
8
+ parser: "typescript"
9
+ });
10
+ } catch {
11
+ return String(content);
12
+ }
13
+ }
14
+ export {
15
+ formatWithPrettier
16
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ initCLI
3
+ } from "../chunk-EP566NIH.js";
4
+ export {
5
+ initCLI
6
+ };
@@ -0,0 +1,7 @@
1
+ import {
2
+ isColorHue
3
+ } from "../chunk-5IH5T2QU.js";
4
+ import "../chunk-U3IUZ4GA.js";
5
+ export {
6
+ isColorHue
7
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ isObject
3
+ } from "../chunk-U3IUZ4GA.js";
4
+ export {
5
+ isObject
6
+ };
@@ -0,0 +1,6 @@
1
+ import {
2
+ printUnionType
3
+ } from "../chunk-CHKLAMB6.js";
4
+ export {
5
+ printUnionType
6
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@react-native-styled-system/cli",
3
+ "version": "0.0.5",
4
+ "description": "Generate theme typings for autocomplete",
5
+ "type": "module",
6
+ "main": "dist/cjs/index.cjs",
7
+ "module": "dist/esm/index.mjs",
8
+ "types": "dist/types/index.d.ts",
9
+ "scripts": {
10
+ "build": "rm -rf dist && tsup src --format esm --shims"
11
+ },
12
+ "dependencies": {
13
+ "bundle-n-require": "^1.0.1",
14
+ "chokidar": "^3.5.3",
15
+ "cli-welcome": "^2.2.2",
16
+ "commander": "^11.0.0",
17
+ "ora": "^7.0.1",
18
+ "prettier": "^3.0.2",
19
+ "update-notifier": "^6.0.2"
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "bin"
24
+ ],
25
+ "bin": "bin/index.js",
26
+ "devDependencies": {
27
+ "@types/ora": "^3.2.0",
28
+ "@types/update-notifier": "6.0.4",
29
+ "tsup": "^8.0.2",
30
+ "typescript": "*"
31
+ },
32
+ "gitHead": "7c09f0fe9584d8280bdfd1f39f89525133e9a65e"
33
+ }