@hanzogui/generate-themes 2.0.0

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 (34) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/generate-themes.cjs +144 -0
  3. package/dist/cjs/generate-themes.native.js +172 -0
  4. package/dist/cjs/generate-themes.native.js.map +1 -0
  5. package/dist/cjs/index.cjs +19 -0
  6. package/dist/cjs/index.native.js +22 -0
  7. package/dist/cjs/index.native.js.map +1 -0
  8. package/dist/cjs/write-generate-themes.cjs +47 -0
  9. package/dist/cjs/write-generate-themes.native.js +54 -0
  10. package/dist/cjs/write-generate-themes.native.js.map +1 -0
  11. package/dist/esm/generate-themes.mjs +121 -0
  12. package/dist/esm/generate-themes.mjs.map +1 -0
  13. package/dist/esm/generate-themes.native.js +146 -0
  14. package/dist/esm/generate-themes.native.js.map +1 -0
  15. package/dist/esm/index.js +3 -0
  16. package/dist/esm/index.js.map +1 -0
  17. package/dist/esm/index.mjs +3 -0
  18. package/dist/esm/index.mjs.map +1 -0
  19. package/dist/esm/index.native.js +3 -0
  20. package/dist/esm/index.native.js.map +1 -0
  21. package/dist/esm/write-generate-themes.mjs +13 -0
  22. package/dist/esm/write-generate-themes.mjs.map +1 -0
  23. package/dist/esm/write-generate-themes.native.js +17 -0
  24. package/dist/esm/write-generate-themes.native.js.map +1 -0
  25. package/package.json +46 -0
  26. package/src/generate-themes.ts +225 -0
  27. package/src/index.ts +2 -0
  28. package/src/write-generate-themes.ts +27 -0
  29. package/types/generate-themes.d.ts +4 -0
  30. package/types/generate-themes.d.ts.map +1 -0
  31. package/types/index.d.ts +3 -0
  32. package/types/index.d.ts.map +1 -0
  33. package/types/write-generate-themes.d.ts +3 -0
  34. package/types/write-generate-themes.d.ts.map +1 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Nate Wienert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,144 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: !0
9
+ });
10
+ },
11
+ __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ return to;
17
+ };
18
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
19
+ value: !0
20
+ }), mod);
21
+ var generate_themes_exports = {};
22
+ __export(generate_themes_exports, {
23
+ generateThemes: () => generateThemes
24
+ });
25
+ module.exports = __toCommonJS(generate_themes_exports);
26
+ var import_node_path = require("node:path");
27
+ let didRegisterOnce = !1;
28
+ async function generateThemes(inputFile) {
29
+ const inputFilePath = inputFile[0] === "." ? (0, import_node_path.join)(process.cwd(), inputFile) : inputFile;
30
+ didRegisterOnce ? purgeCache(inputFilePath) : (didRegisterOnce = !0, require("esbuild-register/dist/node").register({
31
+ hookIgnoreNodeModules: !1
32
+ }));
33
+ let og = process.env.HANZO_GUI_KEEP_THEMES;
34
+ process.env.HANZO_GUI_KEEP_THEMES = "1", process.env.HANZO_GUI_RUN_THEMEBUILDER = "1";
35
+ try {
36
+ const requiredThemes = require(inputFilePath),
37
+ themes = requiredThemes.default || requiredThemes.themes || requiredThemes[Object.keys(requiredThemes)[0]];
38
+ return {
39
+ generated: generatedThemesToTypescript(themes)
40
+ };
41
+ } catch (err) {
42
+ console.warn(` \u26A0\uFE0F Error running theme builder:
43
+ `, err?.stack || err);
44
+ } finally {
45
+ process.env.HANZO_GUI_KEEP_THEMES = og;
46
+ }
47
+ }
48
+ const dedupedTokens = /* @__PURE__ */new Map();
49
+ function generatedThemesToTypescript(themes) {
50
+ const dedupedThemes = /* @__PURE__ */new Map(),
51
+ dedupedThemeToNames = /* @__PURE__ */new Map();
52
+ let i = 0;
53
+ for (const name in themes) {
54
+ i++;
55
+ const theme = themes[name];
56
+ let j = 0;
57
+ for (const [key2, value] of Object.entries(theme)) {
58
+ i++;
59
+ const uniqueKey = `t${i}${j}`;
60
+ dedupedTokens.has(value) || dedupedTokens.set(value, uniqueKey);
61
+ }
62
+ const key = JSON.stringify(theme);
63
+ dedupedThemes.has(key) ? dedupedThemeToNames.set(key, [...dedupedThemeToNames.get(key), name]) : (dedupedThemes.set(key, theme), dedupedThemeToNames.set(key, [name]));
64
+ }
65
+ if (!themes) throw new Error("Didn't find any themes exported or returned");
66
+ const baseKeys = Object.entries(themes.light || themes[Object.keys(themes)[0]]);
67
+ let out = `${`export type Theme = {
68
+ ${baseKeys.map(([k]) => ` ${k}: string;
69
+ `).join("")}
70
+ }`}
71
+ `;
72
+ out += `
73
+ function t(a: [number, number][]) {
74
+ let res: Record<string,string> = {}
75
+ for (const [ki, vi] of a) {
76
+ res[ks[ki] as string] = colors[vi] as string
77
+ }
78
+ return res as Theme
79
+ }
80
+ `, out += `export const colors = [
81
+ `;
82
+ let index = 0;
83
+ const valueToIndex = {};
84
+ dedupedTokens.forEach((name, value) => {
85
+ valueToIndex[value] = index, index++, out += ` '${value}',
86
+ `;
87
+ }), out += `]
88
+
89
+ `;
90
+ const keys = baseKeys.map(([k]) => k);
91
+ out += `const ks = [
92
+ `, out += keys.map(k => `'${k}'`).join(`,
93
+ `), out += `]
94
+
95
+ `;
96
+ let nameI = 0,
97
+ themeTypes = "export type ThemeNames =",
98
+ exported = `export type Themes = Record<ThemeNames, Theme>
99
+
100
+ export const themes: Themes = {`;
101
+ return dedupedThemes.forEach(theme => {
102
+ nameI++;
103
+ const key = JSON.stringify(theme),
104
+ names = dedupedThemeToNames.get(key),
105
+ name = `n${nameI}`,
106
+ baseTheme = `const ${name} = ${objectToJsString(theme, keys, valueToIndex)}`;
107
+ out += `
108
+ ${baseTheme}`, names.forEach(n => {
109
+ exported += `
110
+ ${n}: ${name},`, n.toLowerCase() === n && (themeTypes += `
111
+ | '${n}'`);
112
+ });
113
+ }), out += `
114
+
115
+ ${themeTypes}
116
+
117
+ ${exported}
118
+ }
119
+ `, out;
120
+ }
121
+ function objectToJsString(obj, keys, valueToIndex) {
122
+ let arrItems = [];
123
+ for (const key in obj) {
124
+ const ki = keys.indexOf(key),
125
+ vi = valueToIndex[obj[key]];
126
+ arrItems.push(`[${ki}, ${vi}]`);
127
+ }
128
+ return `t([${arrItems.join(",")}])`;
129
+ }
130
+ function purgeCache(moduleName) {
131
+ searchCache(moduleName, mod => {
132
+ delete require.cache[mod.id];
133
+ }), !(!module.constructor || !module.constructor._pathCache) && Object.keys(module.constructor._pathCache).forEach(cacheKey => {
134
+ cacheKey.indexOf(moduleName) > 0 && delete module.constructor._pathCache[cacheKey];
135
+ });
136
+ }
137
+ function searchCache(moduleName, callback) {
138
+ let mod = require.resolve(moduleName);
139
+ mod && (mod = require.cache[mod]) !== void 0 && function traverse(mod2, depth = 0) {
140
+ depth > 10 || (mod2.children.forEach(child => {
141
+ traverse(child, depth + 1);
142
+ }), callback(mod2));
143
+ }(mod);
144
+ }
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
+ value: !0
22
+ }), mod);
23
+ var generate_themes_exports = {};
24
+ __export(generate_themes_exports, {
25
+ generateThemes: () => generateThemes
26
+ });
27
+ module.exports = __toCommonJS(generate_themes_exports);
28
+ var import_path = require("path"),
29
+ didRegisterOnce = !1;
30
+ async function generateThemes(inputFile) {
31
+ var inputFilePath = inputFile[0] === "." ? (0, import_path.join)(process.cwd(), inputFile) : inputFile;
32
+ didRegisterOnce ? purgeCache(inputFilePath) : (didRegisterOnce = !0, require("esbuild-register/dist/node").register({
33
+ hookIgnoreNodeModules: !1
34
+ }));
35
+ var og = process.env.HANZO_GUI_KEEP_THEMES;
36
+ process.env.HANZO_GUI_KEEP_THEMES = "1", process.env.HANZO_GUI_RUN_THEMEBUILDER = "1";
37
+ try {
38
+ var requiredThemes = require(inputFilePath),
39
+ themes = requiredThemes.default || requiredThemes.themes || requiredThemes[Object.keys(requiredThemes)[0]],
40
+ generatedThemes = generatedThemesToTypescript(themes);
41
+ return {
42
+ generated: generatedThemes
43
+ };
44
+ } catch (err) {
45
+ console.warn(` \u26A0\uFE0F Error running theme builder:
46
+ `, err?.stack || err);
47
+ } finally {
48
+ process.env.HANZO_GUI_KEEP_THEMES = og;
49
+ }
50
+ }
51
+ var dedupedTokens = /* @__PURE__ */new Map();
52
+ function generatedThemesToTypescript(themes) {
53
+ var dedupedThemes = /* @__PURE__ */new Map(),
54
+ dedupedThemeToNames = /* @__PURE__ */new Map(),
55
+ i = 0;
56
+ for (var name in themes) {
57
+ i++;
58
+ var theme = themes[name],
59
+ j = 0,
60
+ _iteratorNormalCompletion = !0,
61
+ _didIteratorError = !1,
62
+ _iteratorError = void 0;
63
+ try {
64
+ for (var _iterator = Object.entries(theme)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
65
+ var [key, value] = _step.value;
66
+ i++;
67
+ var uniqueKey = `t${i}${j}`;
68
+ dedupedTokens.has(value) || dedupedTokens.set(value, uniqueKey);
69
+ }
70
+ } catch (err) {
71
+ _didIteratorError = !0, _iteratorError = err;
72
+ } finally {
73
+ try {
74
+ !_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
75
+ } finally {
76
+ if (_didIteratorError) throw _iteratorError;
77
+ }
78
+ }
79
+ var key1 = JSON.stringify(theme);
80
+ dedupedThemes.has(key1) ? dedupedThemeToNames.set(key1, [...dedupedThemeToNames.get(key1), name]) : (dedupedThemes.set(key1, theme), dedupedThemeToNames.set(key1, [name]));
81
+ }
82
+ if (!themes) throw new Error("Didn't find any themes exported or returned");
83
+ var baseKeys = Object.entries(themes.light || themes[Object.keys(themes)[0]]),
84
+ baseTypeString = `export type Theme = {
85
+ ${baseKeys.map(function (param) {
86
+ var [k] = param;
87
+ return ` ${k}: string;
88
+ `;
89
+ }).join("")}
90
+ }`,
91
+ out = `${baseTypeString}
92
+ `;
93
+ out += `
94
+ function t(a: [number, number][]) {
95
+ let res: Record<string,string> = {}
96
+ for (const [ki, vi] of a) {
97
+ res[ks[ki] as string] = colors[vi] as string
98
+ }
99
+ return res as Theme
100
+ }
101
+ `, out += `export const colors = [
102
+ `;
103
+ var index = 0,
104
+ valueToIndex = {};
105
+ dedupedTokens.forEach(function (name2, value2) {
106
+ valueToIndex[value2] = index, index++, out += ` '${value2}',
107
+ `;
108
+ }), out += `]
109
+
110
+ `;
111
+ var keys = baseKeys.map(function (param) {
112
+ var [k] = param;
113
+ return k;
114
+ });
115
+ out += `const ks = [
116
+ `, out += keys.map(function (k) {
117
+ return `'${k}'`;
118
+ }).join(`,
119
+ `), out += `]
120
+
121
+ `;
122
+ var nameI = 0,
123
+ themeTypes = "export type ThemeNames =",
124
+ exported = `export type Themes = Record<ThemeNames, Theme>
125
+
126
+ export const themes: Themes = {`;
127
+ return dedupedThemes.forEach(function (theme2) {
128
+ nameI++;
129
+ var key2 = JSON.stringify(theme2),
130
+ names = dedupedThemeToNames.get(key2),
131
+ name2 = `n${nameI}`,
132
+ baseTheme = `const ${name2} = ${objectToJsString(theme2, keys, valueToIndex)}`;
133
+ out += `
134
+ ${baseTheme}`, names.forEach(function (n) {
135
+ exported += `
136
+ ${n}: ${name2},`, n.toLowerCase() === n && (themeTypes += `
137
+ | '${n}'`);
138
+ });
139
+ }), out += `
140
+
141
+ ${themeTypes}
142
+
143
+ ${exported}
144
+ }
145
+ `, out;
146
+ }
147
+ function objectToJsString(obj, keys, valueToIndex) {
148
+ var arrItems = [];
149
+ for (var key in obj) {
150
+ var ki = keys.indexOf(key),
151
+ vi = valueToIndex[obj[key]];
152
+ arrItems.push(`[${ki}, ${vi}]`);
153
+ }
154
+ return `t([${arrItems.join(",")}])`;
155
+ }
156
+ function purgeCache(moduleName) {
157
+ searchCache(moduleName, function (mod) {
158
+ delete require.cache[mod.id];
159
+ }), !(!module.constructor || !module.constructor._pathCache) && Object.keys(module.constructor._pathCache).forEach(function (cacheKey) {
160
+ cacheKey.indexOf(moduleName) > 0 && delete module.constructor._pathCache[cacheKey];
161
+ });
162
+ }
163
+ function searchCache(moduleName, callback) {
164
+ var mod = require.resolve(moduleName);
165
+ mod && (mod = require.cache[mod]) !== void 0 && function traverse(mod2) {
166
+ var depth = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0;
167
+ depth > 10 || (mod2.children.forEach(function (child) {
168
+ traverse(child, depth + 1);
169
+ }), callback(mod2));
170
+ }(mod);
171
+ }
172
+ //# sourceMappingURL=generate-themes.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","generate_themes_exports","__export","generateThemes","module","exports","import_path","require","didRegisterOnce","inputFile","inputFilePath","join","process","cwd","purgeCache","register","hookIgnoreNodeModules","og","env","HANZO_GUI_KEEP_THEMES","HANZO_GUI_RUN_THEMEBUILDER","requiredThemes","themes","default","Object","keys","generatedThemes","generatedThemesToTypescript","generated","err","console","warn","stack","dedupedTokens","Map","dedupedThemes","dedupedThemeToNames","i","name","theme","j","_iteratorNormalCompletion","_didIteratorError","_iteratorError","_iterator","entries","Symbol","iterator","_step","next","done","key","uniqueKey","has","set","return","key1","JSON","stringify","get","Error","baseKeys","light","baseTypeString","map","param","k","out","index","valueToIndex","forEach","name2","value2","nameI","themeTypes","exported","theme2","key2","names","baseTheme","objectToJsString","n","toLowerCase","obj","arrItems"],"sources":["../../src/generate-themes.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,uBAAA;AAAAC,QAAA,CAAAD,uBAAA;EAAAE,cAAA,EAAAA,CAAA,KAAAA;AAAA;AACAC,MAAA,CAAAC,OAAA,GAAAT,YAAqB,CAAAK,uBAAA;AAMr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AAV,KAAO,KAAQU,CAAA,CAAAC,WAAY,OAAAD,CAAA,KAAAP,UAAA;AAAA,MAIzBO,CAAA,GAAC;IASC;EAIN,CAAC,GAAAd,GAAA;AACH;AAMA,EAAAO,UAAS;AAEP;AAKA,EAAIC,QAAQ;AAIR;AAME,GAAAR,GAAA;AAAyB;AAKf,SACRa,iBAAAG,GAAA,EAAA1D,IAAA,EAAA4C,YAAA;EAEV,IAAAe,QAAA","ignoreList":[]}
@@ -0,0 +1,19 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
7
+ get: () => from[key],
8
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
9
+ });
10
+ return to;
11
+ },
12
+ __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
13
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
14
+ value: !0
15
+ }), mod);
16
+ var index_exports = {};
17
+ module.exports = __toCommonJS(index_exports);
18
+ __reExport(index_exports, require("./generate-themes.cjs"), module.exports);
19
+ __reExport(index_exports, require("./write-generate-themes.cjs"), module.exports);
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
9
+ get: () => from[key],
10
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
11
+ });
12
+ return to;
13
+ },
14
+ __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
16
+ value: !0
17
+ }), mod);
18
+ var index_exports = {};
19
+ module.exports = __toCommonJS(index_exports);
20
+ __reExport(index_exports, require("./generate-themes.native.js"), module.exports);
21
+ __reExport(index_exports, require("./write-generate-themes.native.js"), module.exports);
22
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","module","exports","__reExport","require"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAP,YAAc,CAAAK,aAAA;AACdG,UAAA,CAAAH,aAAA,EAAcI,OAAA,iCAAAH,MADd,CAAAC,OAAA","ignoreList":[]}
@@ -0,0 +1,47 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf,
6
+ __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
+ value: mod,
27
+ enumerable: !0
28
+ }) : target, mod)),
29
+ __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
+ value: !0
31
+ }), mod);
32
+ var write_generate_themes_exports = {};
33
+ __export(write_generate_themes_exports, {
34
+ writeGeneratedThemes: () => writeGeneratedThemes
35
+ });
36
+ module.exports = __toCommonJS(write_generate_themes_exports);
37
+ var fs = __toESM(require("fs-extra"), 1);
38
+ async function writeGeneratedThemes(guiDotDir, outPath, generatedOutput) {
39
+ if (!generatedOutput) return;
40
+ const {
41
+ generated
42
+ } = generatedOutput;
43
+ process.env.DEBUG === "@hanzo/gui" && console.info("Generated themes:", JSON.stringify(generatedOutput, null, 2));
44
+ const newContent = `// @ts-nocheck
45
+ ` + generated;
46
+ (await fs.readFile(outPath, "utf-8").catch(() => null)) !== newContent && (await fs.writeFile(outPath, newContent));
47
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf,
8
+ __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all) __defProp(target, name, {
11
+ get: all[name],
12
+ enumerable: !0
13
+ });
14
+ },
15
+ __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
17
+ get: () => from[key],
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: !0
30
+ }) : target, mod)),
31
+ __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: !0
33
+ }), mod);
34
+ var write_generate_themes_exports = {};
35
+ __export(write_generate_themes_exports, {
36
+ writeGeneratedThemes: () => writeGeneratedThemes
37
+ });
38
+ module.exports = __toCommonJS(write_generate_themes_exports);
39
+ var fs = __toESM(require("fs-extra"), 1);
40
+ async function writeGeneratedThemes(guiDotDir, outPath, generatedOutput) {
41
+ if (generatedOutput) {
42
+ var {
43
+ generated
44
+ } = generatedOutput;
45
+ process.env.DEBUG === "@hanzo/gui" && console.info("Generated themes:", JSON.stringify(generatedOutput, null, 2));
46
+ var newContent = `// @ts-nocheck
47
+ ` + generated,
48
+ existingContent = await fs.readFile(outPath, "utf-8").catch(function () {
49
+ return null;
50
+ });
51
+ existingContent !== newContent && (await fs.writeFile(outPath, newContent));
52
+ }
53
+ }
54
+ //# sourceMappingURL=write-generate-themes.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","write_generate_themes_exports","__export","writeGeneratedThemes","module","exports","fs","__toESM","require","guiDotDir","outPath","generatedOutput","generated","process","env","DEBUG","console","info","JSON","stringify","newContent","existingContent","readFile","catch"],"sources":["../../src/write-generate-themes.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAAA;EAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;IAAAC,KAAA;EAAA,IAAAH,GAAA;AAAA,IAAAI,6BAAA;AAAAC,QAAA,CAAAD,6BAAA;EAAAE,oBAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAoB,GAAAT,YAAA,CAAAK,6BAAA;AAIpB,IAAAK,EAAA,GAAAC,OAAsB,CAAAC,OAAA,YACpB;AAIA,eAAKL,oBAAiBA,CAAAM,SAAA,EAAAC,OAAA,EAAAC,eAAA;EAEtB,IAAAA,eAAkB;IAEd;MAAQC;IAAI,IAAUD,eAAA;IAI1BE,OAAM,CAAAC,GAAA,CAAAC,KAAa,qBAAAC,OAAA,CAAAC,IAAA,sBAAAC,IAAA,CAAAC,SAAA,CAAAR,eAAA;IAAqB,IAAAS,UAAA;AAIxC,IADwBR,SAAS;MAAAS,eAAkB,SAASf,EAAA,CAAAgB,QAAY,CAAAZ,OAAI,EACpD,SAAAa,KAIxB,aAAS;QACX","ignoreList":[]}
@@ -0,0 +1,121 @@
1
+ import { join } from "node:path";
2
+ let didRegisterOnce = !1;
3
+ async function generateThemes(inputFile) {
4
+ const inputFilePath = inputFile[0] === "." ? join(process.cwd(), inputFile) : inputFile;
5
+ didRegisterOnce ? purgeCache(inputFilePath) : (didRegisterOnce = !0, require("esbuild-register/dist/node").register({
6
+ hookIgnoreNodeModules: !1
7
+ }));
8
+ let og = process.env.HANZO_GUI_KEEP_THEMES;
9
+ process.env.HANZO_GUI_KEEP_THEMES = "1", process.env.HANZO_GUI_RUN_THEMEBUILDER = "1";
10
+ try {
11
+ const requiredThemes = require(inputFilePath),
12
+ themes = requiredThemes.default || requiredThemes.themes || requiredThemes[Object.keys(requiredThemes)[0]];
13
+ return {
14
+ generated: generatedThemesToTypescript(themes)
15
+ };
16
+ } catch (err) {
17
+ console.warn(` \u26A0\uFE0F Error running theme builder:
18
+ `, err?.stack || err);
19
+ } finally {
20
+ process.env.HANZO_GUI_KEEP_THEMES = og;
21
+ }
22
+ }
23
+ const dedupedTokens = /* @__PURE__ */new Map();
24
+ function generatedThemesToTypescript(themes) {
25
+ const dedupedThemes = /* @__PURE__ */new Map(),
26
+ dedupedThemeToNames = /* @__PURE__ */new Map();
27
+ let i = 0;
28
+ for (const name in themes) {
29
+ i++;
30
+ const theme = themes[name];
31
+ let j = 0;
32
+ for (const [key2, value] of Object.entries(theme)) {
33
+ i++;
34
+ const uniqueKey = `t${i}${j}`;
35
+ dedupedTokens.has(value) || dedupedTokens.set(value, uniqueKey);
36
+ }
37
+ const key = JSON.stringify(theme);
38
+ dedupedThemes.has(key) ? dedupedThemeToNames.set(key, [...dedupedThemeToNames.get(key), name]) : (dedupedThemes.set(key, theme), dedupedThemeToNames.set(key, [name]));
39
+ }
40
+ if (!themes) throw new Error("Didn't find any themes exported or returned");
41
+ const baseKeys = Object.entries(themes.light || themes[Object.keys(themes)[0]]);
42
+ let out = `${`export type Theme = {
43
+ ${baseKeys.map(([k]) => ` ${k}: string;
44
+ `).join("")}
45
+ }`}
46
+ `;
47
+ out += `
48
+ function t(a: [number, number][]) {
49
+ let res: Record<string,string> = {}
50
+ for (const [ki, vi] of a) {
51
+ res[ks[ki] as string] = colors[vi] as string
52
+ }
53
+ return res as Theme
54
+ }
55
+ `, out += `export const colors = [
56
+ `;
57
+ let index = 0;
58
+ const valueToIndex = {};
59
+ dedupedTokens.forEach((name, value) => {
60
+ valueToIndex[value] = index, index++, out += ` '${value}',
61
+ `;
62
+ }), out += `]
63
+
64
+ `;
65
+ const keys = baseKeys.map(([k]) => k);
66
+ out += `const ks = [
67
+ `, out += keys.map(k => `'${k}'`).join(`,
68
+ `), out += `]
69
+
70
+ `;
71
+ let nameI = 0,
72
+ themeTypes = "export type ThemeNames =",
73
+ exported = `export type Themes = Record<ThemeNames, Theme>
74
+
75
+ export const themes: Themes = {`;
76
+ return dedupedThemes.forEach(theme => {
77
+ nameI++;
78
+ const key = JSON.stringify(theme),
79
+ names = dedupedThemeToNames.get(key),
80
+ name = `n${nameI}`,
81
+ baseTheme = `const ${name} = ${objectToJsString(theme, keys, valueToIndex)}`;
82
+ out += `
83
+ ${baseTheme}`, names.forEach(n => {
84
+ exported += `
85
+ ${n}: ${name},`, n.toLowerCase() === n && (themeTypes += `
86
+ | '${n}'`);
87
+ });
88
+ }), out += `
89
+
90
+ ${themeTypes}
91
+
92
+ ${exported}
93
+ }
94
+ `, out;
95
+ }
96
+ function objectToJsString(obj, keys, valueToIndex) {
97
+ let arrItems = [];
98
+ for (const key in obj) {
99
+ const ki = keys.indexOf(key),
100
+ vi = valueToIndex[obj[key]];
101
+ arrItems.push(`[${ki}, ${vi}]`);
102
+ }
103
+ return `t([${arrItems.join(",")}])`;
104
+ }
105
+ function purgeCache(moduleName) {
106
+ searchCache(moduleName, mod => {
107
+ delete require.cache[mod.id];
108
+ }), !(!module.constructor || !module.constructor._pathCache) && Object.keys(module.constructor._pathCache).forEach(cacheKey => {
109
+ cacheKey.indexOf(moduleName) > 0 && delete module.constructor._pathCache[cacheKey];
110
+ });
111
+ }
112
+ function searchCache(moduleName, callback) {
113
+ let mod = require.resolve(moduleName);
114
+ mod && (mod = require.cache[mod]) !== void 0 && function traverse(mod2, depth = 0) {
115
+ depth > 10 || (mod2.children.forEach(child => {
116
+ traverse(child, depth + 1);
117
+ }), callback(mod2));
118
+ }(mod);
119
+ }
120
+ export { generateThemes };
121
+ //# sourceMappingURL=generate-themes.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["join","didRegisterOnce","generateThemes","inputFile","inputFilePath","process","cwd","purgeCache","require","register","hookIgnoreNodeModules","og","env","HANZO_GUI_KEEP_THEMES","HANZO_GUI_RUN_THEMEBUILDER","requiredThemes","themes","default","Object","keys","generated","generatedThemesToTypescript","err","console","warn","stack","dedupedTokens","Map","dedupedThemes","dedupedThemeToNames","i","name","theme","j","key2","value","entries","uniqueKey","has","set","key","JSON","stringify","get","Error","baseKeys","light","out","map","k","index","valueToIndex","forEach","nameI","themeTypes","exported","names","baseTheme","objectToJsString","n","toLowerCase","obj","arrItems","ki","indexOf","vi","push","moduleName","searchCache","mod","cache","id","module","constructor","_pathCache","cacheKey","callback","resolve","traverse","mod2","depth","children","child"],"sources":["../../src/generate-themes.ts"],"sourcesContent":[null],"mappings":"AACA,SAASA,IAAA,QAAY;AAMrB,IAAIC,eAAA,GAAkB;AAEtB,eAAsBC,eAAeC,SAAA,EAAmB;EACtD,MAAMC,aAAA,GAAgBD,SAAA,CAAU,CAAC,MAAM,MAAMH,IAAA,CAAKK,OAAA,CAAQC,GAAA,CAAI,GAAGH,SAAS,IAAIA,SAAA;EAEzEF,eAAA,GAOHM,UAAA,CAAWH,aAAa,KANxBH,eAAA,GAAkB,IAElBO,OAAA,CAAQ,4BAA4B,EAAEC,QAAA,CAAS;IAC7CC,qBAAA,EAAuB;EACzB,CAAC;EAKH,IAAIC,EAAA,GAAKN,OAAA,CAAQO,GAAA,CAAIC,qBAAA;EACrBR,OAAA,CAAQO,GAAA,CAAIC,qBAAA,GAAwB,KACpCR,OAAA,CAAQO,GAAA,CAAIE,0BAAA,GAA6B;EAEzC,IAAI;IACF,MAAMC,cAAA,GAAiBP,OAAA,CAAQJ,aAAa;MAEtCY,MAAA,GACJD,cAAA,CAAeE,OAAA,IACfF,cAAA,CAAeC,MAAA,IACfD,cAAA,CAAeG,MAAA,CAAOC,IAAA,CAAKJ,cAAc,EAAE,CAAC,CAAC;IAI/C,OAAO;MACLK,SAAA,EAHsBC,2BAAA,CAA4BL,MAAM;IAI1D;EACF,SAASM,GAAA,EAAK;IACZC,OAAA,CAAQC,IAAA,CAAK;AAAA,GAAsCF,GAAA,EAAMG,KAAA,IAAYH,GAAG;EAC1E,UAAE;IACAjB,OAAA,CAAQO,GAAA,CAAIC,qBAAA,GAAwBF,EAAA;EACtC;AACF;AAKA,MAAMe,aAAA,GAAgB,mBAAIC,GAAA,CAAoB;AAE9C,SAASN,4BAA4BL,MAAA,EAA6B;EAChE,MAAMY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AA,CAAS7B,OAAA,CAAS8B,KAAA,IAAU;MAC9BJ,QAAA,CAASI,KAAA,EAAOF,KAAA,GAAQ,CAAC;IAC3B,CAAC,GAIDJ,QAAA,CAASG,IAAG;EACd,EAAGV,GAAG;AAEV","ignoreList":[]}
@@ -0,0 +1,146 @@
1
+ import { join } from "path";
2
+ var didRegisterOnce = !1;
3
+ async function generateThemes(inputFile) {
4
+ var inputFilePath = inputFile[0] === "." ? join(process.cwd(), inputFile) : inputFile;
5
+ didRegisterOnce ? purgeCache(inputFilePath) : (didRegisterOnce = !0, require("esbuild-register/dist/node").register({
6
+ hookIgnoreNodeModules: !1
7
+ }));
8
+ var og = process.env.HANZO_GUI_KEEP_THEMES;
9
+ process.env.HANZO_GUI_KEEP_THEMES = "1", process.env.HANZO_GUI_RUN_THEMEBUILDER = "1";
10
+ try {
11
+ var requiredThemes = require(inputFilePath),
12
+ themes = requiredThemes.default || requiredThemes.themes || requiredThemes[Object.keys(requiredThemes)[0]],
13
+ generatedThemes = generatedThemesToTypescript(themes);
14
+ return {
15
+ generated: generatedThemes
16
+ };
17
+ } catch (err) {
18
+ console.warn(` \u26A0\uFE0F Error running theme builder:
19
+ `, err?.stack || err);
20
+ } finally {
21
+ process.env.HANZO_GUI_KEEP_THEMES = og;
22
+ }
23
+ }
24
+ var dedupedTokens = /* @__PURE__ */new Map();
25
+ function generatedThemesToTypescript(themes) {
26
+ var dedupedThemes = /* @__PURE__ */new Map(),
27
+ dedupedThemeToNames = /* @__PURE__ */new Map(),
28
+ i = 0;
29
+ for (var name in themes) {
30
+ i++;
31
+ var theme = themes[name],
32
+ j = 0,
33
+ _iteratorNormalCompletion = !0,
34
+ _didIteratorError = !1,
35
+ _iteratorError = void 0;
36
+ try {
37
+ for (var _iterator = Object.entries(theme)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
38
+ var [key, value] = _step.value;
39
+ i++;
40
+ var uniqueKey = `t${i}${j}`;
41
+ dedupedTokens.has(value) || dedupedTokens.set(value, uniqueKey);
42
+ }
43
+ } catch (err) {
44
+ _didIteratorError = !0, _iteratorError = err;
45
+ } finally {
46
+ try {
47
+ !_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
48
+ } finally {
49
+ if (_didIteratorError) throw _iteratorError;
50
+ }
51
+ }
52
+ var key1 = JSON.stringify(theme);
53
+ dedupedThemes.has(key1) ? dedupedThemeToNames.set(key1, [...dedupedThemeToNames.get(key1), name]) : (dedupedThemes.set(key1, theme), dedupedThemeToNames.set(key1, [name]));
54
+ }
55
+ if (!themes) throw new Error("Didn't find any themes exported or returned");
56
+ var baseKeys = Object.entries(themes.light || themes[Object.keys(themes)[0]]),
57
+ baseTypeString = `export type Theme = {
58
+ ${baseKeys.map(function (param) {
59
+ var [k] = param;
60
+ return ` ${k}: string;
61
+ `;
62
+ }).join("")}
63
+ }`,
64
+ out = `${baseTypeString}
65
+ `;
66
+ out += `
67
+ function t(a: [number, number][]) {
68
+ let res: Record<string,string> = {}
69
+ for (const [ki, vi] of a) {
70
+ res[ks[ki] as string] = colors[vi] as string
71
+ }
72
+ return res as Theme
73
+ }
74
+ `, out += `export const colors = [
75
+ `;
76
+ var index = 0,
77
+ valueToIndex = {};
78
+ dedupedTokens.forEach(function (name2, value2) {
79
+ valueToIndex[value2] = index, index++, out += ` '${value2}',
80
+ `;
81
+ }), out += `]
82
+
83
+ `;
84
+ var keys = baseKeys.map(function (param) {
85
+ var [k] = param;
86
+ return k;
87
+ });
88
+ out += `const ks = [
89
+ `, out += keys.map(function (k) {
90
+ return `'${k}'`;
91
+ }).join(`,
92
+ `), out += `]
93
+
94
+ `;
95
+ var nameI = 0,
96
+ themeTypes = "export type ThemeNames =",
97
+ exported = `export type Themes = Record<ThemeNames, Theme>
98
+
99
+ export const themes: Themes = {`;
100
+ return dedupedThemes.forEach(function (theme2) {
101
+ nameI++;
102
+ var key2 = JSON.stringify(theme2),
103
+ names = dedupedThemeToNames.get(key2),
104
+ name2 = `n${nameI}`,
105
+ baseTheme = `const ${name2} = ${objectToJsString(theme2, keys, valueToIndex)}`;
106
+ out += `
107
+ ${baseTheme}`, names.forEach(function (n) {
108
+ exported += `
109
+ ${n}: ${name2},`, n.toLowerCase() === n && (themeTypes += `
110
+ | '${n}'`);
111
+ });
112
+ }), out += `
113
+
114
+ ${themeTypes}
115
+
116
+ ${exported}
117
+ }
118
+ `, out;
119
+ }
120
+ function objectToJsString(obj, keys, valueToIndex) {
121
+ var arrItems = [];
122
+ for (var key in obj) {
123
+ var ki = keys.indexOf(key),
124
+ vi = valueToIndex[obj[key]];
125
+ arrItems.push(`[${ki}, ${vi}]`);
126
+ }
127
+ return `t([${arrItems.join(",")}])`;
128
+ }
129
+ function purgeCache(moduleName) {
130
+ searchCache(moduleName, function (mod) {
131
+ delete require.cache[mod.id];
132
+ }), !(!module.constructor || !module.constructor._pathCache) && Object.keys(module.constructor._pathCache).forEach(function (cacheKey) {
133
+ cacheKey.indexOf(moduleName) > 0 && delete module.constructor._pathCache[cacheKey];
134
+ });
135
+ }
136
+ function searchCache(moduleName, callback) {
137
+ var mod = require.resolve(moduleName);
138
+ mod && (mod = require.cache[mod]) !== void 0 && function traverse(mod2) {
139
+ var depth = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0;
140
+ depth > 10 || (mod2.children.forEach(function (child) {
141
+ traverse(child, depth + 1);
142
+ }), callback(mod2));
143
+ }(mod);
144
+ }
145
+ export { generateThemes };
146
+ //# sourceMappingURL=generate-themes.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["join","didRegisterOnce","generateThemes","inputFile","inputFilePath","process","cwd","purgeCache","require","register","hookIgnoreNodeModules","og","env","HANZO_GUI_KEEP_THEMES","HANZO_GUI_RUN_THEMEBUILDER","requiredThemes","themes","default","Object","keys","generatedThemes","generatedThemesToTypescript","generated","err","console","warn","stack","dedupedTokens","Map","dedupedThemes","dedupedThemeToNames","i","name","theme","j","_iteratorNormalCompletion","_didIteratorError","_iteratorError","_iterator","entries","Symbol","iterator","_step","next","done","key","value","uniqueKey","has","set","return","key1","JSON","stringify","get","Error","baseKeys","light","baseTypeString","map","param","k","out","index","valueToIndex","forEach","name2","value2","nameI","themeTypes","exported","theme2","key2","names","baseTheme","objectToJsString","n","toLowerCase","obj","arrItems"],"sources":["../../src/generate-themes.ts"],"sourcesContent":[null],"mappings":"AACA,SAASA,IAAA,QAAY;AAMrB,IAAIC,eAAA,GAAkB;AAEtB,eAAsBC,eAAeC,SAAA,EAAmB;EACtD,IAAAC,aAAM,GAAAD,SAAgB,GAAU,KAAC,GAAM,GAAAH,IAAM,CAAAK,OAAK,CAAAC,GAAQ,IAAIH,SAAG,IAASA,SAAI;EAEzEF,eAAA,GAOHM,UAAA,CAAWH,aAAa,KANxBH,eAAA,GAAkB,IAElBO,OAAA,CAAQ,4BAA4B,EAAEC,QAAA,CAAS;IAC7CC,qBAAA,EAAuB;EACzB,CAAC;EAKH,IAAIC,EAAA,GAAKN,OAAA,CAAQO,GAAA,CAAIC,qBAAA;EACrBR,OAAA,CAAQO,GAAA,CAAIC,qBAAA,GAAwB,KACpCR,OAAA,CAAQO,GAAA,CAAIE,0BAAA,GAA6B;EAEzC,IAAI;IACF,IAAAC,cAAM,GAAAP,OAAiB,CAAAJ,aAAQ;MAAaY,MAEtC,GAAAD,cACJ,CAAAE,OAAe,IAAAF,cACf,CAAAC,MAAe,IAAAD,cACf,CAAAG,MAAe,CAAAC,IAAO,CAAAJ,cAAK,EAAc,EAAE,CAAC;MAACK,eAAA,GAAAC,2BAAA,CAAAL,MAAA;IAI/C,OAAO;MACLM,SAAA,EAHsBF;IAIxB;EACF,SAASG,GAAA,EAAK;IACZC,OAAA,CAAQC,IAAA,CAAK;AAAA,GAAsCF,GAAA,EAAMG,KAAA,IAAYH,GAAG;EAC1E,UAAE;IACAlB,OAAA,CAAQO,GAAA,CAAIC,qBAAA,GAAwBF,EAAA;EACtC;AACF;AAKA,IAAAgB,aAAM,kBAA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,CAAAkB,IAAA;MAAAN,KAAA,OAAAE,KAAA;MAAAM,SAAA,YAAAR,KAAA,MAAAS,gBAAA,CAAAJ,MAAA,EAAApD,IAAA,EAAA6C,YAAA;IACjCF,GAAA;AAKA,EAAAY,SAAS,IAAAD,KAAW,CAAAR,OAAA,WAAYW,CAAA;MAG9BN,QAAY;AACV,IAAAM,CAAA,KAAAV,KAAO,KAAQU,CAAA,CAAAC,WAAY,OAAAD,CAAA,KAAAP,UAAA;AAAA,MAIzBO,CAAA,GAAC;IASC;EAIN,CAAC,GAAAd,GAAA;AACH;AAMA,EAAAO,UAAS;AAEP;AAKA,EAAIC,QAAQ;AAIR;AAME,GAAAR,GAAA;AAAyB;AAKf,SACRa,iBAAAG,GAAA,EAAA3D,IAAA,EAAA6C,YAAA;EAEV,IAAAe,QAAA","ignoreList":[]}
@@ -0,0 +1,3 @@
1
+ export * from "./generate-themes.mjs";
2
+ export * from "./write-generate-themes.mjs";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc","ignoreList":[]}
@@ -0,0 +1,3 @@
1
+ export * from "./generate-themes.mjs";
2
+ export * from "./write-generate-themes.mjs";
3
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc","ignoreList":[]}
@@ -0,0 +1,3 @@
1
+ export * from "./generate-themes.native.js";
2
+ export * from "./write-generate-themes.native.js";
3
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,cAAc;AACd,cAAc","ignoreList":[]}
@@ -0,0 +1,13 @@
1
+ import * as fs from "fs-extra";
2
+ async function writeGeneratedThemes(guiDotDir, outPath, generatedOutput) {
3
+ if (!generatedOutput) return;
4
+ const {
5
+ generated
6
+ } = generatedOutput;
7
+ process.env.DEBUG === "@hanzo/gui" && console.info("Generated themes:", JSON.stringify(generatedOutput, null, 2));
8
+ const newContent = `// @ts-nocheck
9
+ ` + generated;
10
+ (await fs.readFile(outPath, "utf-8").catch(() => null)) !== newContent && (await fs.writeFile(outPath, newContent));
11
+ }
12
+ export { writeGeneratedThemes };
13
+ //# sourceMappingURL=write-generate-themes.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["fs","writeGeneratedThemes","guiDotDir","outPath","generatedOutput","generated","process","env","DEBUG","console","info","JSON","stringify","newContent","readFile","catch","writeFile"],"sources":["../../src/write-generate-themes.ts"],"sourcesContent":[null],"mappings":"AAAA,YAAYA,EAAA,MAAQ;AAIpB,eAAsBC,qBACpBC,SAAA,EACAC,OAAA,EACAC,eAAA,EACA;EACA,IAAI,CAACA,eAAA,EAAiB;EAEtB,MAAM;IAAEC;EAAU,IAAID,eAAA;EAElBE,OAAA,CAAQC,GAAA,CAAIC,KAAA,KAAU,gBACxBC,OAAA,CAAQC,IAAA,CAAK,qBAAqBC,IAAA,CAAKC,SAAA,CAAUR,eAAA,EAAiB,MAAM,CAAC,CAAC;EAG5E,MAAMS,UAAA,GAAa;AAAA,IAAqBR,SAAA;EAGhB,OAAML,EAAA,CAAGc,QAAA,CAASX,OAAA,EAAS,OAAO,EAAEY,KAAA,CAAM,MAAM,IAAI,OACpDF,UAAA,KAIxB,MAAMb,EAAA,CAAGgB,SAAA,CAAUb,OAAA,EAASU,UAAU;AACxC","ignoreList":[]}
@@ -0,0 +1,17 @@
1
+ import * as fs from "fs-extra";
2
+ async function writeGeneratedThemes(guiDotDir, outPath, generatedOutput) {
3
+ if (generatedOutput) {
4
+ var {
5
+ generated
6
+ } = generatedOutput;
7
+ process.env.DEBUG === "@hanzo/gui" && console.info("Generated themes:", JSON.stringify(generatedOutput, null, 2));
8
+ var newContent = `// @ts-nocheck
9
+ ` + generated,
10
+ existingContent = await fs.readFile(outPath, "utf-8").catch(function () {
11
+ return null;
12
+ });
13
+ existingContent !== newContent && (await fs.writeFile(outPath, newContent));
14
+ }
15
+ }
16
+ export { writeGeneratedThemes };
17
+ //# sourceMappingURL=write-generate-themes.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["fs","writeGeneratedThemes","guiDotDir","outPath","generatedOutput","generated","process","env","DEBUG","console","info","JSON","stringify","newContent","existingContent","readFile","catch"],"sources":["../../src/write-generate-themes.ts"],"sourcesContent":[null],"mappings":"AAAA,YAAYA,EAAA,MAAQ;AAIpB,eAAsBC,qBACpBC,SAAA,EACAC,OAAA,EACAC,eAAA,EACA;EACA,IAAIA,eAAC,EAAiB;IAEtB,IAAM;MAAEC;IAAU,IAAID,eAAA;IAElBE,OAAQ,CAAAC,GAAI,CAAAC,KAAA,KAAU,gBACxBC,OAAQ,CAAAC,IAAK,oBAAqB,EAAAC,IAAK,CAAAC,SAAU,CAAAR,eAAiB,MAAM,EAAE;IAG5E,IAAMS,UAAA,GAAa;AAAA,IAAqBR,SAAA;MAAAS,eAAA,SAAAd,EAAA,CAAAe,QAAA,CAAAZ,OAAA,WAAAa,KAAA;QAGhB,OAAS;MAMnC","ignoreList":[]}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@hanzogui/generate-themes",
3
+ "version": "2.0.0",
4
+ "source": "src/index.ts",
5
+ "files": [
6
+ "src",
7
+ "types",
8
+ "dist"
9
+ ],
10
+ "type": "module",
11
+ "sideEffects": false,
12
+ "main": "dist/cjs",
13
+ "module": "dist/esm",
14
+ "types": "./types/index.d.ts",
15
+ "exports": {
16
+ "./package.json": "./package.json",
17
+ ".": {
18
+ "types": "./types/index.d.ts",
19
+ "react-native": "./dist/esm/index.native.js",
20
+ "browser": "./dist/esm/index.mjs",
21
+ "module": "./dist/esm/index.mjs",
22
+ "import": "./dist/esm/index.mjs",
23
+ "require": "./dist/cjs/index.cjs",
24
+ "default": "./dist/esm/index.mjs"
25
+ }
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "scripts": {
31
+ "build": "hanzo-gui-build",
32
+ "watch": "bun run build --watch",
33
+ "clean": "hanzo-gui-build clean",
34
+ "clean:build": "hanzo-gui-build clean:build"
35
+ },
36
+ "dependencies": {
37
+ "@hanzogui/create-theme": "workspace:*",
38
+ "@hanzogui/theme-builder": "workspace:*",
39
+ "@hanzogui/types": "workspace:*",
40
+ "esbuild-register": "^3.6.0",
41
+ "fs-extra": "^11.2.0"
42
+ },
43
+ "devDependencies": {
44
+ "@hanzogui/build": "workspace:*"
45
+ }
46
+ }
@@ -0,0 +1,225 @@
1
+ import type { ThemeBuilder } from '@hanzogui/theme-builder'
2
+ import { join } from 'node:path'
3
+
4
+ type ThemeBuilderInterceptOpts = {
5
+ onComplete: (result: { themeBuilder: ThemeBuilder<any> }) => void
6
+ }
7
+
8
+ let didRegisterOnce = false
9
+
10
+ export async function generateThemes(inputFile: string) {
11
+ const inputFilePath = inputFile[0] === '.' ? join(process.cwd(), inputFile) : inputFile
12
+
13
+ if (!didRegisterOnce) {
14
+ didRegisterOnce = true
15
+ // the unregsiter does basically nothing and keeps a process running
16
+ require('esbuild-register/dist/node').register({
17
+ hookIgnoreNodeModules: false,
18
+ })
19
+ } else {
20
+ purgeCache(inputFilePath)
21
+ }
22
+
23
+ let og = process.env.HANZO_GUI_KEEP_THEMES
24
+ process.env.HANZO_GUI_KEEP_THEMES = '1'
25
+ process.env.HANZO_GUI_RUN_THEMEBUILDER = '1'
26
+
27
+ try {
28
+ const requiredThemes = require(inputFilePath)
29
+
30
+ const themes =
31
+ requiredThemes['default'] ||
32
+ requiredThemes['themes'] ||
33
+ requiredThemes[Object.keys(requiredThemes)[0]]
34
+
35
+ const generatedThemes = generatedThemesToTypescript(themes)
36
+
37
+ return {
38
+ generated: generatedThemes,
39
+ }
40
+ } catch (err) {
41
+ console.warn(` ⚠️ Error running theme builder:\n`, err?.['stack'] || err)
42
+ } finally {
43
+ process.env.HANZO_GUI_KEEP_THEMES = og
44
+ }
45
+ }
46
+
47
+ /**
48
+ * value -> name of variable
49
+ */
50
+ const dedupedTokens = new Map<string, string>()
51
+
52
+ function generatedThemesToTypescript(themes: Record<string, any>) {
53
+ const dedupedThemes = new Map<string, object>()
54
+ const dedupedThemeToNames = new Map<string, string[]>()
55
+
56
+ let i = 0
57
+ for (const name in themes) {
58
+ i++
59
+
60
+ const theme: Record<string, string> = themes[name]
61
+
62
+ // go through all tokens in current theme and add the new values to dedupedTokens map
63
+ let j = 0
64
+ for (const [key, value] of Object.entries(theme)) {
65
+ i++
66
+ const uniqueKey = `t${i}${j}`
67
+ if (!dedupedTokens.has(value)) {
68
+ dedupedTokens.set(value, uniqueKey)
69
+ }
70
+ }
71
+
72
+ const key = JSON.stringify(theme)
73
+ if (dedupedThemes.has(key)) {
74
+ dedupedThemeToNames.set(key, [...dedupedThemeToNames.get(key)!, name])
75
+ } else {
76
+ dedupedThemes.set(key, theme)
77
+ dedupedThemeToNames.set(key, [name])
78
+ }
79
+ }
80
+
81
+ if (!themes) {
82
+ throw new Error(`Didn't find any themes exported or returned`)
83
+ }
84
+
85
+ const baseKeys = Object.entries(themes.light || themes[Object.keys(themes)[0]]) as [
86
+ string,
87
+ string,
88
+ ][]
89
+
90
+ const baseTypeString = `export type Theme = {
91
+ ${baseKeys
92
+ .map(([k]) => {
93
+ return ` ${k}: string;\n`
94
+ })
95
+ .join('')}
96
+ }`
97
+
98
+ let out = `${baseTypeString}\n`
99
+
100
+ // add in the helper function to generate a theme:
101
+ out += `
102
+ function t(a: [number, number][]) {
103
+ let res: Record<string,string> = {}
104
+ for (const [ki, vi] of a) {
105
+ res[ks[ki] as string] = colors[vi] as string
106
+ }
107
+ return res as Theme
108
+ }
109
+ `
110
+
111
+ // add all token variables
112
+ out += `export const colors = [\n`
113
+ let index = 0
114
+ const valueToIndex = {}
115
+ dedupedTokens.forEach((name, value) => {
116
+ valueToIndex[value] = index
117
+ index++
118
+ out += ` '${value}',\n`
119
+ })
120
+ out += ']\n\n'
121
+
122
+ // add all keys array
123
+ const keys = baseKeys.map(([k]) => k)
124
+ out += `const ks = [\n`
125
+ out += keys.map((k) => `'${k}'`).join(',\n')
126
+ out += `]\n\n`
127
+
128
+ // add all themes
129
+ let nameI = 0
130
+
131
+ let themeTypes = `export type ThemeNames =`
132
+ let exported = `export type Themes = Record<ThemeNames, Theme>\n\nexport const themes: Themes = {`
133
+
134
+ dedupedThemes.forEach((theme) => {
135
+ nameI++
136
+ const key = JSON.stringify(theme)
137
+ const names = dedupedThemeToNames.get(key)!
138
+ const name = `n${nameI}`
139
+ const baseTheme = `const ${name} = ${objectToJsString(theme, keys, valueToIndex)}`
140
+ out += `\n${baseTheme}`
141
+ names.forEach((n) => {
142
+ exported += `\n ${n}: ${name},`
143
+
144
+ if (n.toLowerCase() === n) {
145
+ themeTypes += `\n | '${n}'`
146
+ }
147
+ })
148
+ })
149
+
150
+ out += `\n\n${themeTypes}\n\n${exported}\n}\n`
151
+
152
+ return out
153
+ }
154
+
155
+ function objectToJsString(
156
+ obj: object,
157
+ keys: string[],
158
+ valueToIndex: Record<string, number>
159
+ ) {
160
+ let arrItems: string[] = []
161
+ for (const key in obj) {
162
+ const ki = keys.indexOf(key)
163
+ const vi = valueToIndex[obj[key]]
164
+ arrItems.push(`[${ki}, ${vi}]`)
165
+ }
166
+ return `t([${arrItems.join(',')}])`
167
+ }
168
+
169
+ /**
170
+ * Removes a module from the cache
171
+ */
172
+ function purgeCache(moduleName) {
173
+ // Traverse the cache looking for the files
174
+ // loaded by the specified module name
175
+ searchCache(moduleName, (mod) => {
176
+ delete require.cache[mod.id]
177
+ })
178
+
179
+ // @ts-ignore
180
+ if (!module.constructor || !module.constructor._pathCache) {
181
+ // bun doesn't have this
182
+ return
183
+ }
184
+
185
+ // Remove cached paths to the module.
186
+ // Thanks to @bentael for pointing this out.
187
+ // @ts-ignore
188
+ Object.keys(module.constructor._pathCache).forEach((cacheKey) => {
189
+ if (cacheKey.indexOf(moduleName) > 0) {
190
+ // @ts-ignore
191
+ delete module.constructor._pathCache[cacheKey]
192
+ }
193
+ })
194
+ }
195
+
196
+ /**
197
+ * Traverses the cache to search for all the cached
198
+ * files of the specified module name
199
+ */
200
+ function searchCache(moduleName, callback) {
201
+ // Resolve the module identified by the specified name
202
+ let mod = require.resolve(moduleName)
203
+
204
+ // Check if the module has been resolved and found within
205
+ // the cache
206
+ // @ts-ignore
207
+ if (mod && (mod = require.cache[mod]) !== undefined) {
208
+ // Recursively go over the results
209
+ ;(function traverse(mod, depth = 0) {
210
+ // avoid recursing too much
211
+ if (depth > 10) return
212
+
213
+ // Go over each of the module's children and
214
+ // traverse them
215
+ // @ts-ignore
216
+ mod.children.forEach((child) => {
217
+ traverse(child, depth + 1)
218
+ })
219
+
220
+ // Call the specified callback providing the
221
+ // found cached module
222
+ callback(mod)
223
+ })(mod)
224
+ }
225
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './generate-themes'
2
+ export * from './write-generate-themes'
@@ -0,0 +1,27 @@
1
+ import * as fs from 'fs-extra'
2
+
3
+ import type { generateThemes } from './generate-themes'
4
+
5
+ export async function writeGeneratedThemes(
6
+ guiDotDir: string,
7
+ outPath: string,
8
+ generatedOutput: Awaited<ReturnType<typeof generateThemes>>
9
+ ) {
10
+ if (!generatedOutput) return
11
+
12
+ const { generated } = generatedOutput
13
+
14
+ if (process.env.DEBUG === '@hanzo/gui') {
15
+ console.info(`Generated themes:`, JSON.stringify(generatedOutput, null, 2))
16
+ }
17
+
18
+ const newContent = `// @ts-nocheck\n` + generated
19
+
20
+ // Skip writing if contents are unchanged
21
+ const existingContent = await fs.readFile(outPath, 'utf-8').catch(() => null)
22
+ if (existingContent === newContent) {
23
+ return
24
+ }
25
+
26
+ await fs.writeFile(outPath, newContent)
27
+ }
@@ -0,0 +1,4 @@
1
+ export declare function generateThemes(inputFile: string): Promise<{
2
+ generated: string;
3
+ } | undefined>;
4
+ //# sourceMappingURL=generate-themes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generate-themes.d.ts","sourceRoot":"","sources":["../src/generate-themes.ts"],"names":[],"mappings":"AASA,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM;;eAmCrD"}
@@ -0,0 +1,3 @@
1
+ export * from './generate-themes';
2
+ export * from './write-generate-themes';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,yBAAyB,CAAA"}
@@ -0,0 +1,3 @@
1
+ import type { generateThemes } from './generate-themes';
2
+ export declare function writeGeneratedThemes(guiDotDir: string, outPath: string, generatedOutput: Awaited<ReturnType<typeof generateThemes>>): Promise<void>;
3
+ //# sourceMappingURL=write-generate-themes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"write-generate-themes.d.ts","sourceRoot":"","sources":["../src/write-generate-themes.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAEvD,wBAAsB,oBAAoB,CACxC,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,EACf,eAAe,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,iBAmB5D"}