@astryxdesign/cli 0.6.0 → 0.6.1

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 (219) hide show
  1. package/CHANGELOG.md +48 -1
  2. package/README.md +76 -67
  3. package/api/build/build.test.mjs +37 -0
  4. package/api/build/build.type.mjs +1 -1
  5. package/api/build/kit/kit.mjs +31 -1
  6. package/api/component/_adapter.mjs +14 -28
  7. package/api/component/integration-import-agreement.test.mjs +318 -0
  8. package/api/doctor/doctor.d.mts +32 -3
  9. package/api/doctor/doctor.doc.mjs +2 -1
  10. package/api/doctor/doctor.mjs +97 -4
  11. package/api/doctor/doctor.test.mjs +90 -1
  12. package/api/gap-report/gap-report-worker.d.mts +4 -0
  13. package/api/gap-report/gap-report-worker.mjs +73 -0
  14. package/api/gap-report/gap-report.d.mts +81 -0
  15. package/api/gap-report/gap-report.doc.d.mts +9 -0
  16. package/api/gap-report/gap-report.doc.mjs +127 -0
  17. package/api/gap-report/gap-report.mjs +707 -0
  18. package/api/gap-report/gap-report.test.mjs +1210 -0
  19. package/api/gap-report/gap-report.type.d.mts +88 -0
  20. package/api/gap-report/gap-report.type.mjs +69 -0
  21. package/api/index.d.mts +8 -1
  22. package/api/index.mjs +24 -1
  23. package/api/integration/add-contribution.d.mts +44 -0
  24. package/api/integration/add-contribution.mjs +1160 -0
  25. package/api/integration/add-contribution.test.mjs +733 -0
  26. package/api/integration/add-helpers.d.mts +80 -0
  27. package/api/integration/add-helpers.mjs +333 -0
  28. package/api/integration/add-helpers.test.mjs +161 -0
  29. package/api/integration/add-theme.d.mts +10 -0
  30. package/api/integration/add-theme.mjs +332 -0
  31. package/api/integration/add-theme.test.mjs +194 -0
  32. package/api/integration/integration-authoring.type.d.mts +55 -0
  33. package/api/integration/integration-authoring.type.mjs +76 -0
  34. package/api/integration/integration-block-exports.test.mjs +4 -0
  35. package/api/integration/integrationAdd.doc.d.mts +5 -0
  36. package/api/integration/integrationAdd.doc.mjs +100 -0
  37. package/api/integration/integrationAddAgentDoc.doc.d.mts +5 -0
  38. package/api/integration/integrationAddAgentDoc.doc.mjs +49 -0
  39. package/api/integration/integrationAddCodemod.doc.d.mts +5 -0
  40. package/api/integration/integrationAddCodemod.doc.mjs +55 -0
  41. package/api/integration/integrationAddComponent.doc.d.mts +5 -0
  42. package/api/integration/integrationAddComponent.doc.mjs +49 -0
  43. package/api/integration/integrationAddDoc.doc.d.mts +5 -0
  44. package/api/integration/integrationAddDoc.doc.mjs +59 -0
  45. package/api/integration/integrationAddTemplate.doc.d.mts +5 -0
  46. package/api/integration/integrationAddTemplate.doc.mjs +55 -0
  47. package/api/integration/integrationAddTheme.doc.d.mts +5 -0
  48. package/api/integration/integrationAddTheme.doc.mjs +85 -0
  49. package/api/integration/integrationPackCheck.doc.d.mts +5 -0
  50. package/api/integration/integrationPackCheck.doc.mjs +35 -0
  51. package/api/integration/pack-check.d.mts +27 -0
  52. package/api/integration/pack-check.mjs +795 -0
  53. package/api/integration/pack-check.test.mjs +569 -0
  54. package/api/integration/pack-check.type.d.mts +45 -0
  55. package/api/integration/pack-check.type.mjs +56 -0
  56. package/api/integration/public-api.test.mjs +21 -0
  57. package/api/integration/validate-integration.mjs +221 -11
  58. package/api/integration/validate-integration.test.mjs +189 -5
  59. package/api/integration/validateIntegration.doc.mjs +2 -2
  60. package/api/json/index.ts +1 -0
  61. package/api/search/search.mjs +19 -1
  62. package/api/theme/_adapter.d.mts +26 -28
  63. package/api/theme/_adapter.mjs +101 -38
  64. package/api/theme/add/add.d.mts +2 -1
  65. package/api/theme/add/add.mjs +56 -20
  66. package/api/theme/add/add.test.mjs +1 -0
  67. package/api/theme/build/build.d.mts +25 -0
  68. package/api/theme/build/build.mjs +101 -70
  69. package/api/theme/build/build.test.mjs +77 -44
  70. package/api/theme/integration-themes.test.mjs +183 -0
  71. package/api/theme/list/list.d.mts +10 -2
  72. package/api/theme/list/list.mjs +31 -10
  73. package/api/theme/list/list.test.mjs +102 -23
  74. package/api/theme/listThemes.doc.d.mts +1 -5
  75. package/api/theme/listThemes.doc.mjs +6 -13
  76. package/api/theme/targets/targets.test.mjs +22 -8
  77. package/api/theme/theme.d.mts +1 -1
  78. package/api/theme/theme.mjs +1 -1
  79. package/api/theme/theme.type.d.mts +9 -0
  80. package/api/theme/theme.type.mjs +3 -1
  81. package/api/theme/themeAdd.doc.d.mts +1 -4
  82. package/api/theme/themeAdd.doc.mjs +31 -31
  83. package/api/theme/themeBuild.doc.mjs +1 -1
  84. package/api/theme/themeList.doc.d.mts +1 -4
  85. package/api/theme/themeList.doc.mjs +7 -15
  86. package/api/theme/themeListAvailable.doc.d.mts +8 -0
  87. package/api/theme/themeListAvailable.doc.mjs +57 -0
  88. package/api/theme/themeTargets.doc.mjs +21 -7
  89. package/api/upgrade/_adapter.d.mts +2 -0
  90. package/api/upgrade/_adapter.mjs +1 -0
  91. package/api/upgrade/registry/registry.d.mts +40 -0
  92. package/api/upgrade/registry/registry.mjs +1154 -0
  93. package/api/upgrade/registry/registry.test.mjs +826 -0
  94. package/api/upgrade/run/run.mjs +49 -8
  95. package/api/upgrade/status/status.d.mts +6 -4
  96. package/api/upgrade/status/status.mjs +26 -8
  97. package/api/upgrade/upgrade.d.mts +2 -2
  98. package/api/upgrade/upgrade.doc.mjs +33 -9
  99. package/api/upgrade/upgrade.mjs +34 -7
  100. package/api/upgrade/upgrade.test.mjs +198 -8
  101. package/api/upgrade/upgrade.type.d.mts +90 -1
  102. package/api/upgrade/upgrade.type.mjs +51 -2
  103. package/assets/docs/cli-integrations.doc.mjs +152 -28
  104. package/assets/docs/getting-started.doc.mjs +1 -1
  105. package/assets/docs/{shadcn-compatibility.doc.mjs → shadcn-compatibility.doc.draft.mjs} +23 -13
  106. package/assets/docs/styling.doc.mjs +3 -3
  107. package/assets/docs/theme.doc.dense.mjs +2 -2
  108. package/assets/docs/theme.doc.mjs +23 -13
  109. package/assets/docs/theme.doc.zh.mjs +1 -1
  110. package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadCustomSearch.doc.mjs +2 -1
  111. package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadCustomSearch.tsx +38 -12
  112. package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadShowcase.doc.mjs +15 -0
  113. package/assets/templates/blocks/components/BaseTypeahead/BaseTypeaheadShowcase.tsx +87 -0
  114. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.doc.mjs +23 -0
  115. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.tsx +75 -0
  116. package/assets/templates/blocks/components/BreadcrumbItem/BreadcrumbItemShowcase.tsx +5 -19
  117. package/assets/templates/blocks/components/Center/CenterHorizontal.doc.mjs +3 -2
  118. package/assets/templates/blocks/components/Center/CenterHorizontal.tsx +4 -5
  119. package/assets/templates/blocks/components/Toast/ToastAction.tsx +2 -1
  120. package/assets/templates/blocks/components/Toast/ToastDeduplication.tsx +2 -1
  121. package/assets/templates/blocks/components/Toast/ToastDismiss.tsx +2 -1
  122. package/assets/templates/blocks/components/Toast/ToastShowcase.tsx +2 -1
  123. package/assets/templates/blocks/components/Toast/ToastStacking.tsx +2 -1
  124. package/assets/templates/blocks/components/Toast/ToastTypes.tsx +2 -1
  125. package/assets/templates/pages/dashboard/template.doc.mjs +2 -2
  126. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +3 -2
  127. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +3 -2
  128. package/assets/templates/pages/dashboard-comparison/template.doc.mjs +3 -3
  129. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -2
  130. package/assets/templates/pages/dashboard-progress/template.doc.mjs +2 -2
  131. package/assets/templates/pages/dashboard-scorecard/template.doc.mjs +2 -2
  132. package/assets/templates/themes/butter/butterTheme.ts +3 -3
  133. package/assets/templates/themes/neutral/neutralTheme.ts +2 -2
  134. package/assets/templates/themes/stone/stoneTheme.ts +3 -3
  135. package/assets/theme.template.ts +4 -7
  136. package/authoring/config/config.doc.mjs +9 -1
  137. package/authoring/config/parse.d.mts +2 -0
  138. package/authoring/config/parse.mjs +28 -3
  139. package/authoring/config/parse.test.mjs +25 -3
  140. package/authoring/config/type.ts +3 -0
  141. package/authoring/doctypes/_schema.d.mts +3 -0
  142. package/authoring/doctypes/_schema.mjs +6 -1
  143. package/authoring/doctypes/base/type.ts +6 -6
  144. package/authoring/doctypes/component/component.doc.mjs +6 -0
  145. package/authoring/doctypes/component/type.ts +2 -0
  146. package/authoring/gap-report/parse.d.mts +63 -0
  147. package/authoring/gap-report/parse.mjs +121 -0
  148. package/authoring/gap-report/parse.test.mjs +172 -0
  149. package/authoring/gap-report/type.ts +107 -0
  150. package/authoring/index.d.mts +1 -0
  151. package/authoring/index.d.ts +12 -0
  152. package/authoring/index.mjs +4 -0
  153. package/authoring/integration/integration.doc.mjs +14 -2
  154. package/authoring/integration/parse.test.mjs +9 -4
  155. package/authoring/integration/schema.d.mts +2 -0
  156. package/authoring/integration/schema.mjs +1 -0
  157. package/authoring/integration/type.ts +23 -6
  158. package/authoring/shadcn/receipt.d.mts +219 -0
  159. package/authoring/shadcn/receipt.mjs +248 -0
  160. package/authoring/shadcn/receipt.test.mjs +187 -0
  161. package/authoring/shadcn/source-variants.d.mts +28 -0
  162. package/authoring/shadcn/source-variants.mjs +176 -0
  163. package/authoring/shadcn/source-variants.test.mjs +120 -0
  164. package/clients/cli/commands/build-theme.mjs +54 -19
  165. package/clients/cli/commands/build-theme.variants.test.mjs +15 -12
  166. package/clients/cli/commands/component/index.mjs +1 -1
  167. package/clients/cli/commands/docs.test.mjs +1 -0
  168. package/clients/cli/commands/doctor-integration.doc.mjs +1 -1
  169. package/clients/cli/commands/gap-report.doc.mjs +73 -0
  170. package/clients/cli/commands/gap-report.mjs +114 -0
  171. package/clients/cli/commands/gap-report.test.mjs +198 -0
  172. package/clients/cli/commands/import-hint-correctness.test.mjs +88 -0
  173. package/clients/cli/commands/integration-add.doc.mjs +88 -0
  174. package/clients/cli/commands/integration-authoring.test.mjs +177 -0
  175. package/clients/cli/commands/integration-pack.doc.mjs +34 -0
  176. package/clients/cli/commands/integration-real-world.test.mjs +618 -0
  177. package/clients/cli/commands/integration.doc.mjs +28 -0
  178. package/clients/cli/commands/integration.mjs +153 -0
  179. package/clients/cli/commands/theme-add.doc.mjs +11 -6
  180. package/clients/cli/commands/theme-list.doc.mjs +21 -5
  181. package/clients/cli/commands/theme-targets.behavior.test.mjs +19 -2
  182. package/clients/cli/commands/theme.doc.mjs +2 -2
  183. package/clients/cli/commands/upgrade.doc.mjs +17 -4
  184. package/clients/cli/commands/upgrade.mjs +12 -1
  185. package/clients/cli/commands/upgrade.test.mjs +41 -0
  186. package/clients/cli/index.mjs +5 -0
  187. package/clients/cli/lib/component-format.mjs +42 -26
  188. package/clients/cli/lib/component-format.test.mjs +34 -3
  189. package/clients/cli/lib/manifest.mjs +12 -0
  190. package/foundation/config/project-themes.test.mjs +127 -0
  191. package/foundation/config/project.d.mts +23 -4
  192. package/foundation/config/project.mjs +96 -9
  193. package/foundation/config/project.test.mjs +132 -6
  194. package/foundation/discovery/component-discovery.d.mts +36 -0
  195. package/foundation/discovery/component-discovery.mjs +96 -0
  196. package/foundation/discovery/theme-discovery.d.mts +39 -0
  197. package/foundation/discovery/theme-discovery.mjs +650 -0
  198. package/foundation/discovery/theme-discovery.test.mjs +375 -0
  199. package/foundation/discovery/theming-targets.d.mts +16 -0
  200. package/foundation/discovery/theming-targets.mjs +39 -3
  201. package/foundation/discovery/theming-targets.test.mjs +139 -0
  202. package/foundation/integrations/autolink.d.mts +96 -0
  203. package/foundation/integrations/autolink.mjs +265 -0
  204. package/foundation/integrations/autolink.test.mjs +430 -0
  205. package/foundation/integrations/contribution-inventory.d.mts +152 -0
  206. package/foundation/integrations/contribution-inventory.mjs +497 -0
  207. package/foundation/integrations/contribution-inventory.test.mjs +361 -0
  208. package/foundation/integrations/integrations.d.mts +73 -9
  209. package/foundation/integrations/integrations.mjs +158 -8
  210. package/foundation/integrations/integrations.test.mjs +152 -29
  211. package/foundation/integrations/manifest-writer.d.mts +56 -0
  212. package/foundation/integrations/manifest-writer.mjs +328 -0
  213. package/foundation/integrations/manifest-writer.test.mjs +229 -0
  214. package/foundation/integrations/validate-contributions.mjs +81 -6
  215. package/foundation/response/error-codes.d.mts +4 -1
  216. package/foundation/response/error-codes.doc.mjs +16 -1
  217. package/foundation/response/error-codes.mjs +10 -1
  218. package/foundation/response/response-types.doc.mjs +23 -2
  219. package/package.json +8 -1
@@ -0,0 +1,650 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Theme catalog discovery shared by Project, theme list/add, and
5
+ * integration validation.
6
+ *
7
+ * A theme root uses the same layout as the CLI's generated bundle:
8
+ * `manifest.json` beside one directory per slug. Manifest `entry` and `files`
9
+ * paths are relative to that slug directory and are confined there before any
10
+ * caller reads or copies them.
11
+ *
12
+ * @input a bundled or integration-owned theme root
13
+ * @output validated source-theme records with package ownership
14
+ * @position packages/cli/foundation/discovery — shared theme discovery
15
+ */
16
+
17
+ import * as fs from 'node:fs';
18
+ import * as path from 'node:path';
19
+ import {CLI_ROOT} from '../fs/paths.mjs';
20
+ import {assertWithin, PathSafetyError} from '../fs/path-safety.mjs';
21
+
22
+ export const BUNDLED_THEME_PACKAGE = '@astryxdesign/cli';
23
+ export const THEMES_DIR = path.join(CLI_ROOT, 'assets', 'templates', 'themes');
24
+ export const THEME_MANIFEST_BASENAME = 'manifest.json';
25
+ export const MANIFEST_PATH = path.join(THEMES_DIR, THEME_MANIFEST_BASENAME);
26
+
27
+ /**
28
+ * @typedef {object} DiscoveredTheme
29
+ * @property {string} slug
30
+ * @property {string} displayName
31
+ * @property {string} description
32
+ * @property {boolean} maintained
33
+ * @property {string} entry
34
+ * @property {string} exportName
35
+ * @property {string[]} files
36
+ * @property {string} package
37
+ * @property {string} sourceDir absolute directory holding this theme's files
38
+ * @property {boolean} bundled
39
+ */
40
+
41
+ /** @param {unknown} value @param {string} field @param {string} owner */
42
+ function requiredString(value, field, owner) {
43
+ if (typeof value !== 'string' || value.trim().length === 0) {
44
+ throw new Error(
45
+ `Theme catalog for ${owner} has an invalid ${field}; expected a non-empty string.`,
46
+ );
47
+ }
48
+ return value;
49
+ }
50
+
51
+ /**
52
+ * Resolve one authored relative path without allowing POSIX or Windows escape
53
+ * syntax, even when discovery runs on the other platform.
54
+ * @param {string} value
55
+ * @param {string} root
56
+ * @param {string} label
57
+ */
58
+ function resolveThemePath(value, root, label) {
59
+ if (
60
+ path.posix.isAbsolute(value) ||
61
+ path.win32.isAbsolute(value) ||
62
+ value.split(/[\\/]/u).some(segment => segment === '..' || segment === '.')
63
+ ) {
64
+ throw new Error(
65
+ `Invalid ${label} "${value}": it must stay inside the theme directory.`,
66
+ );
67
+ }
68
+ try {
69
+ return assertWithin(value, root, {label});
70
+ } catch (error) {
71
+ if (error instanceof PathSafetyError) {
72
+ throw new Error(error.message, {cause: error});
73
+ }
74
+ throw error;
75
+ }
76
+ }
77
+
78
+ const THEME_MODULE_EXTENSIONS = ['.mjs', '.js', '.mts', '.ts', '.tsx', '.jsx'];
79
+
80
+ /**
81
+ * Resolve a local theme module only when the target is a listed file confined
82
+ * to the theme directory.
83
+ * @param {unknown} specifier
84
+ * @param {string} fromFile
85
+ * @param {string} themeDir
86
+ * @param {Set<string>} allowedFiles
87
+ */
88
+ function resolveLocalThemeModule(specifier, fromFile, themeDir, allowedFiles) {
89
+ if (typeof specifier !== 'string' || !specifier.startsWith('.')) return null;
90
+ const base = path.resolve(path.dirname(fromFile), specifier);
91
+ const candidates = [
92
+ base,
93
+ ...THEME_MODULE_EXTENSIONS.map(extension => `${base}${extension}`),
94
+ ...THEME_MODULE_EXTENSIONS.map(extension =>
95
+ path.join(base, `index${extension}`),
96
+ ),
97
+ ];
98
+ for (const candidate of candidates) {
99
+ try {
100
+ if (!fs.statSync(candidate).isFile()) continue;
101
+ const confined = assertWithin(candidate, themeDir, {
102
+ allowAbsolute: true,
103
+ label: 'theme module',
104
+ });
105
+ if (allowedFiles.has(confined)) return confined;
106
+ } catch {
107
+ // Missing, non-file, escaped, and unlisted candidates are not reachable.
108
+ }
109
+ }
110
+ return null;
111
+ }
112
+
113
+ class ThemeModuleReferenceError extends Error {}
114
+
115
+ /**
116
+ * Validate that every local static dependency is copied with the theme.
117
+ * @param {string} file
118
+ * @param {any} jscodeshift
119
+ * @param {string} themeDir
120
+ * @param {Set<string>} allowedFiles
121
+ * @param {string} owner
122
+ * @param {string} entry
123
+ * @param {Set<string>} [seen]
124
+ */
125
+ function validateThemeModuleGraph(
126
+ file,
127
+ jscodeshift,
128
+ themeDir,
129
+ allowedFiles,
130
+ owner,
131
+ entry,
132
+ seen = new Set(),
133
+ ) {
134
+ if (seen.has(file)) return;
135
+ seen.add(file);
136
+
137
+ const parser = /\.(?:ts|tsx|mts)$/u.test(file) ? 'tsx' : 'babel';
138
+ const j = jscodeshift.withParser(parser);
139
+ const root = j(fs.readFileSync(file, 'utf-8'));
140
+ /** @type {string[]} */
141
+ const specifiers = [];
142
+ root.find(j.ImportDeclaration).forEach((/** @type {any} */ importPath) => {
143
+ if (typeof importPath.node.source?.value === 'string') {
144
+ specifiers.push(importPath.node.source.value);
145
+ }
146
+ });
147
+ root
148
+ .find(j.ExportNamedDeclaration)
149
+ .forEach((/** @type {any} */ exportPath) => {
150
+ if (typeof exportPath.node.source?.value === 'string') {
151
+ specifiers.push(exportPath.node.source.value);
152
+ }
153
+ });
154
+ root.find(j.ExportAllDeclaration).forEach((/** @type {any} */ exportPath) => {
155
+ if (typeof exportPath.node.source?.value === 'string') {
156
+ specifiers.push(exportPath.node.source.value);
157
+ }
158
+ });
159
+ root.find(j.CallExpression).forEach((/** @type {any} */ callPath) => {
160
+ if (
161
+ callPath.node.callee?.type === 'Import' &&
162
+ typeof callPath.node.arguments?.[0]?.value === 'string'
163
+ ) {
164
+ specifiers.push(callPath.node.arguments[0].value);
165
+ }
166
+ });
167
+ root.find(j.ImportExpression).forEach((/** @type {any} */ importPath) => {
168
+ if (typeof importPath.node.source?.value === 'string') {
169
+ specifiers.push(importPath.node.source.value);
170
+ }
171
+ });
172
+
173
+ for (const specifier of specifiers) {
174
+ if (!specifier.startsWith('.')) continue;
175
+ const target = resolveLocalThemeModule(
176
+ specifier,
177
+ file,
178
+ themeDir,
179
+ allowedFiles,
180
+ );
181
+ if (!target) {
182
+ throw new ThemeModuleReferenceError(
183
+ `Theme catalog for ${owner} entry "${entry}" references local module "${specifier}" that must resolve to a listed file inside the theme directory.`,
184
+ );
185
+ }
186
+ if (THEME_MODULE_EXTENSIONS.includes(path.extname(target))) {
187
+ validateThemeModuleGraph(
188
+ target,
189
+ jscodeshift,
190
+ themeDir,
191
+ allowedFiles,
192
+ owner,
193
+ entry,
194
+ seen,
195
+ );
196
+ }
197
+ }
198
+ }
199
+
200
+ /** @param {any} declaration @param {string} exportName */
201
+ function declarationExportsName(declaration, exportName) {
202
+ if (!declaration || declaration.declare === true) return false;
203
+ if (
204
+ (declaration.type === 'FunctionDeclaration' ||
205
+ declaration.type === 'ClassDeclaration') &&
206
+ declaration.id?.name === exportName
207
+ ) {
208
+ return true;
209
+ }
210
+ return (
211
+ declaration.type === 'VariableDeclaration' &&
212
+ declaration.declarations.some(
213
+ (/** @type {any} */ declarationItem) =>
214
+ declarationItem.id?.type === 'Identifier' &&
215
+ declarationItem.id.name === exportName,
216
+ )
217
+ );
218
+ }
219
+
220
+ /**
221
+ * Resolve a source-less export specifier to a real top-level runtime binding.
222
+ * @param {any[]} statements
223
+ * @param {string} localName
224
+ * @param {string} file
225
+ * @param {any} jscodeshift
226
+ * @param {string} themeDir
227
+ * @param {Set<string>} allowedFiles
228
+ * @param {Set<string>} seen
229
+ */
230
+ function hasRuntimeBinding(
231
+ statements,
232
+ localName,
233
+ file,
234
+ jscodeshift,
235
+ themeDir,
236
+ allowedFiles,
237
+ seen,
238
+ ) {
239
+ if (
240
+ statements.some((/** @type {any} */ statement) =>
241
+ declarationExportsName(statement, localName),
242
+ )
243
+ ) {
244
+ return true;
245
+ }
246
+
247
+ for (const statement of statements) {
248
+ if (
249
+ statement.type !== 'ImportDeclaration' ||
250
+ statement.importKind === 'type'
251
+ ) {
252
+ continue;
253
+ }
254
+ for (const specifier of statement.specifiers ?? []) {
255
+ if (
256
+ specifier.local?.name !== localName ||
257
+ specifier.importKind === 'type'
258
+ ) {
259
+ continue;
260
+ }
261
+ const source = statement.source?.value;
262
+ if (typeof source !== 'string') return false;
263
+ if (!source.startsWith('.')) return true;
264
+ const target = resolveLocalThemeModule(
265
+ source,
266
+ file,
267
+ themeDir,
268
+ allowedFiles,
269
+ );
270
+ if (!target) return false;
271
+ if (specifier.type === 'ImportNamespaceSpecifier') return true;
272
+ const importedName =
273
+ specifier.type === 'ImportDefaultSpecifier'
274
+ ? 'default'
275
+ : (specifier.imported?.name ?? specifier.imported?.value);
276
+ return (
277
+ typeof importedName === 'string' &&
278
+ moduleExportsName(
279
+ target,
280
+ importedName,
281
+ jscodeshift,
282
+ themeDir,
283
+ allowedFiles,
284
+ seen,
285
+ )
286
+ );
287
+ }
288
+ }
289
+ return false;
290
+ }
291
+
292
+ /** @param {any} declaration */
293
+ function isRuntimeDefaultDeclaration(declaration) {
294
+ return (
295
+ declaration != null &&
296
+ declaration.declare !== true &&
297
+ ![
298
+ 'TSDeclareFunction',
299
+ 'TSInterfaceDeclaration',
300
+ 'TSTypeAliasDeclaration',
301
+ ].includes(declaration.type)
302
+ );
303
+ }
304
+
305
+ /**
306
+ * Prove a named runtime export without executing the module. Local ESM
307
+ * re-exports are followed recursively.
308
+ * @param {string} file
309
+ * @param {string} exportName
310
+ * @param {any} jscodeshift
311
+ * @param {string} themeDir
312
+ * @param {Set<string>} allowedFiles
313
+ * @param {Set<string>} [seen]
314
+ */
315
+ function moduleExportsName(
316
+ file,
317
+ exportName,
318
+ jscodeshift,
319
+ themeDir,
320
+ allowedFiles,
321
+ seen = new Set(),
322
+ ) {
323
+ const identity = `${file}\0${exportName}`;
324
+ if (seen.has(identity) || !allowedFiles.has(file) || !fs.existsSync(file)) {
325
+ return false;
326
+ }
327
+ seen.add(identity);
328
+
329
+ const parser = /\.(?:ts|tsx|mts)$/u.test(file) ? 'tsx' : 'babel';
330
+ const j = jscodeshift.withParser(parser);
331
+ const root = j(fs.readFileSync(file, 'utf-8'));
332
+ const statements = root.find(j.Program).nodes()[0]?.body ?? [];
333
+ let found = false;
334
+ root
335
+ .find(j.ExportNamedDeclaration)
336
+ .forEach((/** @type {any} */ exportPath) => {
337
+ if (found || exportPath.node.exportKind === 'type') return;
338
+ if (declarationExportsName(exportPath.node.declaration, exportName)) {
339
+ found = true;
340
+ return;
341
+ }
342
+ for (const specifier of exportPath.node.specifiers ?? []) {
343
+ if (
344
+ specifier.type !== 'ExportSpecifier' ||
345
+ specifier.exportKind === 'type' ||
346
+ (specifier.exported?.name ?? specifier.exported?.value) !== exportName
347
+ ) {
348
+ continue;
349
+ }
350
+ if (!exportPath.node.source) {
351
+ const localName = specifier.local?.name ?? specifier.local?.value;
352
+ if (
353
+ typeof localName === 'string' &&
354
+ hasRuntimeBinding(
355
+ statements,
356
+ localName,
357
+ file,
358
+ jscodeshift,
359
+ themeDir,
360
+ allowedFiles,
361
+ seen,
362
+ )
363
+ ) {
364
+ found = true;
365
+ return;
366
+ }
367
+ continue;
368
+ }
369
+ const target = resolveLocalThemeModule(
370
+ exportPath.node.source.value,
371
+ file,
372
+ themeDir,
373
+ allowedFiles,
374
+ );
375
+ const imported =
376
+ specifier.local?.name ?? specifier.local?.value ?? exportName;
377
+ if (
378
+ target &&
379
+ moduleExportsName(
380
+ target,
381
+ imported,
382
+ jscodeshift,
383
+ themeDir,
384
+ allowedFiles,
385
+ seen,
386
+ )
387
+ ) {
388
+ found = true;
389
+ return;
390
+ }
391
+ }
392
+ });
393
+ if (found) return true;
394
+ if (
395
+ exportName === 'default' &&
396
+ statements.some(
397
+ (/** @type {any} */ statement) =>
398
+ statement.type === 'ExportDefaultDeclaration' &&
399
+ isRuntimeDefaultDeclaration(statement.declaration),
400
+ )
401
+ ) {
402
+ return true;
403
+ }
404
+
405
+ root.find(j.ExportAllDeclaration).forEach((/** @type {any} */ exportPath) => {
406
+ if (found || exportPath.node.exportKind === 'type') return;
407
+ const target = resolveLocalThemeModule(
408
+ exportPath.node.source?.value,
409
+ file,
410
+ themeDir,
411
+ allowedFiles,
412
+ );
413
+ if (
414
+ target &&
415
+ moduleExportsName(
416
+ target,
417
+ exportName,
418
+ jscodeshift,
419
+ themeDir,
420
+ allowedFiles,
421
+ seen,
422
+ )
423
+ ) {
424
+ found = true;
425
+ }
426
+ });
427
+ return found;
428
+ }
429
+
430
+ /**
431
+ * Read and validate one theme catalog.
432
+ * @param {string} themeRoot absolute catalog root
433
+ * @param {string} owner package that owns the catalog
434
+ * @param {{bundled?: boolean}} [options]
435
+ * @returns {DiscoveredTheme[]}
436
+ */
437
+ export function discoverThemeCatalog(themeRoot, owner, {bundled = false} = {}) {
438
+ if (!fs.existsSync(themeRoot) || !fs.statSync(themeRoot).isDirectory()) {
439
+ throw new Error(
440
+ `Declared themes root does not exist on disk: ${themeRoot}`,
441
+ );
442
+ }
443
+
444
+ const manifestPath = resolveThemePath(
445
+ THEME_MANIFEST_BASENAME,
446
+ themeRoot,
447
+ 'theme catalog manifest',
448
+ );
449
+ if (!fs.existsSync(manifestPath) || !fs.statSync(manifestPath).isFile()) {
450
+ throw new Error(
451
+ `Theme root for ${owner} must contain ${THEME_MANIFEST_BASENAME}.`,
452
+ );
453
+ }
454
+
455
+ /** @type {unknown} */
456
+ let parsed;
457
+ try {
458
+ parsed = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
459
+ } catch (error) {
460
+ const message = error instanceof Error ? error.message : String(error);
461
+ throw new Error(`Theme catalog for ${owner} is unreadable: ${message}`, {
462
+ cause: error,
463
+ });
464
+ }
465
+
466
+ if (parsed == null || typeof parsed !== 'object' || Array.isArray(parsed)) {
467
+ throw new Error(`Theme catalog for ${owner} must be a JSON object.`);
468
+ }
469
+ const catalog = /** @type {{version?: unknown, themes?: unknown}} */ (parsed);
470
+ if (catalog.version !== 1) {
471
+ throw new Error(`Theme catalog for ${owner} must use version 1.`);
472
+ }
473
+ if (!Array.isArray(catalog.themes)) {
474
+ throw new Error(`Theme catalog for ${owner} must contain a themes array.`);
475
+ }
476
+
477
+ /** @type {DiscoveredTheme[]} */
478
+ const themes = [];
479
+ const slugs = new Set();
480
+ for (const [index, raw] of catalog.themes.entries()) {
481
+ if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) {
482
+ throw new Error(
483
+ `Theme catalog for ${owner} has an invalid entry at index ${index}.`,
484
+ );
485
+ }
486
+ const entry = /** @type {Record<string, unknown>} */ (raw);
487
+ const slug = requiredString(entry.slug, `themes[${index}].slug`, owner);
488
+ if (!/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/u.test(slug)) {
489
+ throw new Error(
490
+ `Theme catalog for ${owner} has invalid slug "${slug}"; use lowercase kebab-case starting with a letter.`,
491
+ );
492
+ }
493
+ const normalizedSlug = slug.toLowerCase();
494
+ if (slugs.has(normalizedSlug)) {
495
+ throw new Error(
496
+ `Theme catalog for ${owner} declares duplicate slug "${slug}".`,
497
+ );
498
+ }
499
+ slugs.add(normalizedSlug);
500
+
501
+ const displayName = requiredString(
502
+ entry.displayName,
503
+ `theme "${slug}" displayName`,
504
+ owner,
505
+ );
506
+ if (typeof entry.description !== 'string') {
507
+ throw new Error(
508
+ `Theme catalog for ${owner} has an invalid description for "${slug}".`,
509
+ );
510
+ }
511
+ if (typeof entry.maintained !== 'boolean') {
512
+ throw new Error(
513
+ `Theme catalog for ${owner} has an invalid maintained flag for "${slug}".`,
514
+ );
515
+ }
516
+ const entryFile = requiredString(
517
+ entry.entry,
518
+ `theme "${slug}" entry`,
519
+ owner,
520
+ );
521
+ const exportName = requiredString(
522
+ entry.exportName,
523
+ `theme "${slug}" exportName`,
524
+ owner,
525
+ );
526
+ if (!/^[$A-Z_a-z][$\w]*$/u.test(exportName)) {
527
+ throw new Error(
528
+ `Theme catalog for ${owner} has invalid exportName "${exportName}".`,
529
+ );
530
+ }
531
+ if (!Array.isArray(entry.files) || entry.files.length === 0) {
532
+ throw new Error(
533
+ `Theme catalog for ${owner} theme "${slug}" must list at least one file.`,
534
+ );
535
+ }
536
+
537
+ const files = entry.files.map((file, fileIndex) =>
538
+ requiredString(file, `theme "${slug}" files[${fileIndex}]`, owner),
539
+ );
540
+ if (new Set(files).size !== files.length) {
541
+ throw new Error(
542
+ `Theme catalog for ${owner} theme "${slug}" lists a file more than once.`,
543
+ );
544
+ }
545
+ if (!files.includes(entryFile)) {
546
+ throw new Error(
547
+ `Theme catalog for ${owner} theme "${slug}" must include entry "${entryFile}" in files.`,
548
+ );
549
+ }
550
+ if (!/\.(?:ts|tsx|mjs|js)$/u.test(entryFile)) {
551
+ throw new Error(
552
+ `Theme catalog for ${owner} theme "${slug}" entry must be source code.`,
553
+ );
554
+ }
555
+
556
+ const sourceDir = resolveThemePath(
557
+ slug,
558
+ themeRoot,
559
+ `theme "${slug}" directory`,
560
+ );
561
+ if (!fs.existsSync(sourceDir) || !fs.statSync(sourceDir).isDirectory()) {
562
+ throw new Error(
563
+ `Theme catalog for ${owner} is missing directory "${slug}".`,
564
+ );
565
+ }
566
+ for (const file of files) {
567
+ const source = resolveThemePath(file, sourceDir, `theme "${slug}" file`);
568
+ if (!fs.existsSync(source) || !fs.statSync(source).isFile()) {
569
+ throw new Error(
570
+ `Theme catalog for ${owner} theme "${slug}" is missing file "${file}".`,
571
+ );
572
+ }
573
+ }
574
+
575
+ themes.push({
576
+ slug,
577
+ displayName,
578
+ description: entry.description,
579
+ maintained: entry.maintained,
580
+ entry: entryFile,
581
+ exportName,
582
+ files,
583
+ package: owner,
584
+ sourceDir,
585
+ bundled,
586
+ });
587
+ }
588
+
589
+ return themes;
590
+ }
591
+
592
+ /** @returns {DiscoveredTheme[]} */
593
+ export function discoverBundledThemes() {
594
+ return discoverThemeCatalog(THEMES_DIR, BUNDLED_THEME_PACKAGE, {
595
+ bundled: true,
596
+ });
597
+ }
598
+
599
+ /**
600
+ * @param {import('../integrations/integrations.mjs').LoadedIntegration} integration
601
+ * @returns {Promise<DiscoveredTheme[]>}
602
+ */
603
+ export async function discoverIntegrationThemes(integration) {
604
+ if (!integration.themes) return [];
605
+ const themes = discoverThemeCatalog(integration.themes, integration.name);
606
+ const jscodeshift = (await import('jscodeshift')).default;
607
+ for (const theme of themes) {
608
+ const allowedFiles = new Set(
609
+ theme.files.map(file =>
610
+ resolveThemePath(file, theme.sourceDir, `theme "${theme.slug}" file`),
611
+ ),
612
+ );
613
+ const entryPath = resolveThemePath(
614
+ theme.entry,
615
+ theme.sourceDir,
616
+ `theme "${theme.slug}" entry`,
617
+ );
618
+ let exportsName;
619
+ try {
620
+ validateThemeModuleGraph(
621
+ entryPath,
622
+ jscodeshift,
623
+ theme.sourceDir,
624
+ allowedFiles,
625
+ theme.package,
626
+ theme.entry,
627
+ );
628
+ exportsName = moduleExportsName(
629
+ entryPath,
630
+ theme.exportName,
631
+ jscodeshift,
632
+ theme.sourceDir,
633
+ allowedFiles,
634
+ );
635
+ } catch (error) {
636
+ if (error instanceof ThemeModuleReferenceError) throw error;
637
+ const message = error instanceof Error ? error.message : String(error);
638
+ throw new Error(
639
+ `Theme catalog for ${theme.package} entry "${theme.entry}" could not be parsed: ${message}`,
640
+ {cause: error},
641
+ );
642
+ }
643
+ if (!exportsName) {
644
+ throw new Error(
645
+ `Theme catalog for ${theme.package} entry "${theme.entry}" does not export "${theme.exportName}".`,
646
+ );
647
+ }
648
+ }
649
+ return themes;
650
+ }