@iota-uz/sdk 0.4.21 → 0.4.22

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.
@@ -1,4 +1,8 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
1
4
  const sdkTheme = require("./sdk-theme.cjs");
5
+ const { resolveIotaSdkTailwindContent } = require("./sdk-content.cjs");
2
6
 
3
7
  function isPlainObject(value) {
4
8
  return (
@@ -21,17 +25,61 @@ function mergeDeep(base, extra) {
21
25
  return out;
22
26
  }
23
27
 
28
+ function uniqueStrings(values) {
29
+ return Array.from(new Set(values.filter(Boolean)));
30
+ }
31
+
32
+ function normalizeStringArray(value) {
33
+ if (Array.isArray(value)) {
34
+ return value.filter((item) => typeof item === "string" && item.trim() !== "");
35
+ }
36
+ if (typeof value === "string" && value.trim() !== "") {
37
+ return [value];
38
+ }
39
+ return [];
40
+ }
41
+
42
+ function shouldIncludeSdkTemplates(includeSdkTemplates, rootDir) {
43
+ if (typeof includeSdkTemplates === "boolean") {
44
+ return includeSdkTemplates;
45
+ }
46
+ return fs.existsSync(path.join(rootDir, "go.mod"));
47
+ }
48
+
24
49
  /**
25
50
  * Creates a Tailwind config that extends the SDK theme (design tokens from iota.css).
26
- * @param { { content?: string[], extend?: Record<string, unknown>, plugins?: unknown[] } } options
51
+ * @param { {
52
+ * content?: string[],
53
+ * extend?: Record<string, unknown>,
54
+ * plugins?: unknown[],
55
+ * includeSdkTemplates?: boolean,
56
+ * rootDir?: string,
57
+ * sdkContentOptions?: Record<string, unknown>
58
+ * } } options
27
59
  * @returns { import('tailwindcss').Config }
28
60
  */
29
61
  function createIotaTailwindConfig(options = {}) {
30
62
  const { content = [], extend = {}, plugins = [] } = options;
63
+ const rootDir = path.resolve(options.rootDir ?? process.cwd());
64
+ const includeSdkTemplates = shouldIncludeSdkTemplates(
65
+ options.includeSdkTemplates,
66
+ rootDir
67
+ );
68
+ const sdkTemplateContent = includeSdkTemplates
69
+ ? resolveIotaSdkTailwindContent({
70
+ rootDir,
71
+ ...(options.sdkContentOptions ?? {}),
72
+ })
73
+ : [];
31
74
  const mergedExtend = mergeDeep(sdkTheme, extend);
75
+ const mergedContent = uniqueStrings([
76
+ ...normalizeStringArray(content),
77
+ ...normalizeStringArray(sdkTemplateContent),
78
+ ]);
79
+
32
80
  return {
33
81
  darkMode: "class",
34
- content,
82
+ content: mergedContent,
35
83
  theme: {
36
84
  extend: mergedExtend,
37
85
  },
@@ -0,0 +1,177 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { execSync } = require("child_process");
4
+
5
+ function directoryExists(dirPath) {
6
+ try {
7
+ return Boolean(dirPath) && fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory();
8
+ } catch {
9
+ return false;
10
+ }
11
+ }
12
+
13
+ function fileExists(filePath) {
14
+ try {
15
+ return Boolean(filePath) && fs.existsSync(filePath) && fs.statSync(filePath).isFile();
16
+ } catch {
17
+ return false;
18
+ }
19
+ }
20
+
21
+ function uniqueSorted(items) {
22
+ return Array.from(new Set(items.filter(Boolean))).sort();
23
+ }
24
+
25
+ function normalizeRootDir(rootDir) {
26
+ if (!rootDir || typeof rootDir !== "string") {
27
+ return process.cwd();
28
+ }
29
+ return path.resolve(rootDir);
30
+ }
31
+
32
+ function resolveGoModCacheDir(env) {
33
+ if (env.GOMODCACHE) {
34
+ return env.GOMODCACHE;
35
+ }
36
+ if (env.GOPATH) {
37
+ return path.join(env.GOPATH, "pkg", "mod");
38
+ }
39
+ return "";
40
+ }
41
+
42
+ function resolveGoListModuleDir(rootDir, execSyncFn, env) {
43
+ if (!fileExists(path.join(rootDir, "go.mod"))) {
44
+ return "";
45
+ }
46
+ try {
47
+ return execSyncFn("go list -m -f '{{.Dir}}' github.com/iota-uz/iota-sdk", {
48
+ cwd: rootDir,
49
+ stdio: ["ignore", "pipe", "ignore"],
50
+ env: { ...env, GOWORK: "off" },
51
+ })
52
+ .toString()
53
+ .trim();
54
+ } catch {
55
+ return "";
56
+ }
57
+ }
58
+
59
+ function defaultMonorepoCandidates(rootDir) {
60
+ return [
61
+ path.resolve(rootDir, "..", "iota-sdk"),
62
+ path.resolve(rootDir, "..", "..", "iota-sdk"),
63
+ path.resolve(rootDir, "..", "..", "..", "iota-sdk"),
64
+ ];
65
+ }
66
+
67
+ function sdkTemplateGlobsFromDir(sdkDir) {
68
+ return [
69
+ path.join(sdkDir, "modules", "**", "templates", "**", "*.{html,js,templ}"),
70
+ path.join(sdkDir, "components", "**", "*.{html,js,templ,go}"),
71
+ ];
72
+ }
73
+
74
+ function sdkTemplateGlobsFromGoModCache(goModCacheDir) {
75
+ return [
76
+ path.join(
77
+ goModCacheDir,
78
+ "github.com",
79
+ "iota-uz",
80
+ "iota-sdk@*",
81
+ "modules",
82
+ "**",
83
+ "templates",
84
+ "**",
85
+ "*.{html,js,templ}"
86
+ ),
87
+ path.join(
88
+ goModCacheDir,
89
+ "github.com",
90
+ "iota-uz",
91
+ "iota-sdk@*",
92
+ "components",
93
+ "**",
94
+ "*.{html,js,templ,go}"
95
+ ),
96
+ ];
97
+ }
98
+
99
+ function resolveIotaSdkTailwindContentWithDebug(options = {}) {
100
+ const rootDir = normalizeRootDir(options.rootDir);
101
+ const env = { ...process.env, ...(options.env ?? {}) };
102
+ const execSyncFn = options.execSyncFn ?? execSync;
103
+ const includeGoList = options.includeGoList !== false;
104
+ const includeMonorepoFallback = options.includeMonorepoFallback !== false;
105
+ const includeGoModCache = options.includeGoModCache !== false;
106
+
107
+ const sources = {
108
+ rootDir,
109
+ goListModuleDir: "",
110
+ monorepoCandidateDirs: [],
111
+ goModCacheDir: "",
112
+ };
113
+
114
+ const sdkDirs = [];
115
+
116
+ if (typeof options.goListModuleDir === "string" && options.goListModuleDir.trim() !== "") {
117
+ sources.goListModuleDir = path.resolve(options.goListModuleDir);
118
+ } else if (includeGoList) {
119
+ sources.goListModuleDir = resolveGoListModuleDir(rootDir, execSyncFn, env);
120
+ }
121
+ if (directoryExists(sources.goListModuleDir)) {
122
+ sdkDirs.push(path.resolve(sources.goListModuleDir));
123
+ }
124
+
125
+ if (includeMonorepoFallback) {
126
+ const rawMonorepoCandidates = Array.isArray(options.monorepoCandidates) && options.monorepoCandidates.length > 0
127
+ ? options.monorepoCandidates
128
+ : defaultMonorepoCandidates(rootDir);
129
+ for (const candidate of rawMonorepoCandidates) {
130
+ if (!candidate || typeof candidate !== "string") {
131
+ continue;
132
+ }
133
+ const absCandidate = path.resolve(candidate);
134
+ if (directoryExists(absCandidate)) {
135
+ sources.monorepoCandidateDirs.push(absCandidate);
136
+ sdkDirs.push(absCandidate);
137
+ }
138
+ }
139
+ }
140
+
141
+ if (includeGoModCache) {
142
+ const rawGoModCacheDir = typeof options.goModCacheDir === "string" && options.goModCacheDir.trim() !== ""
143
+ ? options.goModCacheDir
144
+ : resolveGoModCacheDir(env);
145
+ if (directoryExists(rawGoModCacheDir)) {
146
+ sources.goModCacheDir = path.resolve(rawGoModCacheDir);
147
+ }
148
+ }
149
+
150
+ const content = [];
151
+ for (const sdkDir of uniqueSorted(sdkDirs)) {
152
+ content.push(...sdkTemplateGlobsFromDir(sdkDir));
153
+ }
154
+ if (
155
+ sources.goModCacheDir &&
156
+ directoryExists(path.join(sources.goModCacheDir, "github.com", "iota-uz"))
157
+ ) {
158
+ content.push(...sdkTemplateGlobsFromGoModCache(sources.goModCacheDir));
159
+ }
160
+
161
+ return {
162
+ content: uniqueSorted(content),
163
+ sources: {
164
+ ...sources,
165
+ monorepoCandidateDirs: uniqueSorted(sources.monorepoCandidateDirs),
166
+ },
167
+ };
168
+ }
169
+
170
+ function resolveIotaSdkTailwindContent(options = {}) {
171
+ return resolveIotaSdkTailwindContentWithDebug(options).content;
172
+ }
173
+
174
+ module.exports = {
175
+ resolveIotaSdkTailwindContent,
176
+ resolveIotaSdkTailwindContentWithDebug,
177
+ };