@magmonium/cli 0.7.8

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 (204) hide show
  1. package/README.md +392 -0
  2. package/bin/README.md +244 -0
  3. package/bin/cli.js +55 -0
  4. package/bin/commands/assets-wc.js +249 -0
  5. package/bin/commands/assets.js +943 -0
  6. package/bin/commands/build.js +242 -0
  7. package/bin/commands/clean.js +621 -0
  8. package/bin/commands/compile-i18n.js +91 -0
  9. package/bin/commands/config-ci.js +266 -0
  10. package/bin/commands/configure-wc.js +167 -0
  11. package/bin/commands/connect.js +188 -0
  12. package/bin/commands/create-app.js +946 -0
  13. package/bin/commands/deploy.js +100 -0
  14. package/bin/commands/fix-i18n.js +40 -0
  15. package/bin/commands/generate.js +765 -0
  16. package/bin/commands/init/config-files.js +1114 -0
  17. package/bin/commands/init/mag-assets.js +541 -0
  18. package/bin/commands/init/mag-cli-config.js +120 -0
  19. package/bin/commands/init/monorepo-structure.js +735 -0
  20. package/bin/commands/init/package-json.js +308 -0
  21. package/bin/commands/init/project-structure.js +189 -0
  22. package/bin/commands/init/pwa-files.js +204 -0
  23. package/bin/commands/init/source-files.js +2596 -0
  24. package/bin/commands/init.js +591 -0
  25. package/bin/commands/lib.js +508 -0
  26. package/bin/commands/patch-wc-env.js +136 -0
  27. package/bin/commands/refractor.js +51 -0
  28. package/bin/commands/sanitize-i18n.js +41 -0
  29. package/bin/commands/translate.js +32 -0
  30. package/bin/commands/zip.js +540 -0
  31. package/package.json +53 -0
  32. package/src/compilers/button-group-compiler.d.ts +18 -0
  33. package/src/compilers/button-group-compiler.d.ts.map +1 -0
  34. package/src/compilers/button-group-compiler.js +151 -0
  35. package/src/compilers/button-group-compiler.js.map +1 -0
  36. package/src/compilers/copy-compiler.d.ts +14 -0
  37. package/src/compilers/copy-compiler.d.ts.map +1 -0
  38. package/src/compilers/copy-compiler.js +170 -0
  39. package/src/compilers/copy-compiler.js.map +1 -0
  40. package/src/compilers/field-compiler.d.ts +15 -0
  41. package/src/compilers/field-compiler.d.ts.map +1 -0
  42. package/src/compilers/field-compiler.js +153 -0
  43. package/src/compilers/field-compiler.js.map +1 -0
  44. package/src/compilers/form-compiler.d.ts +15 -0
  45. package/src/compilers/form-compiler.d.ts.map +1 -0
  46. package/src/compilers/form-compiler.js +220 -0
  47. package/src/compilers/form-compiler.js.map +1 -0
  48. package/src/compilers/logo-compiler.d.ts +14 -0
  49. package/src/compilers/logo-compiler.d.ts.map +1 -0
  50. package/src/compilers/logo-compiler.js +114 -0
  51. package/src/compilers/logo-compiler.js.map +1 -0
  52. package/src/compilers/options-compiler.d.ts +46 -0
  53. package/src/compilers/options-compiler.d.ts.map +1 -0
  54. package/src/compilers/options-compiler.js +329 -0
  55. package/src/compilers/options-compiler.js.map +1 -0
  56. package/src/compilers/pwa-build.d.ts +7 -0
  57. package/src/compilers/pwa-build.d.ts.map +1 -0
  58. package/src/compilers/pwa-build.js +62 -0
  59. package/src/compilers/pwa-build.js.map +1 -0
  60. package/src/compilers/svg-compiler.d.ts +12 -0
  61. package/src/compilers/svg-compiler.d.ts.map +1 -0
  62. package/src/compilers/svg-compiler.js +123 -0
  63. package/src/compilers/svg-compiler.js.map +1 -0
  64. package/src/compilers/tabs-compiler.d.ts +25 -0
  65. package/src/compilers/tabs-compiler.d.ts.map +1 -0
  66. package/src/compilers/tabs-compiler.js +422 -0
  67. package/src/compilers/tabs-compiler.js.map +1 -0
  68. package/src/compilers/theme-compiler.d.ts +14 -0
  69. package/src/compilers/theme-compiler.d.ts.map +1 -0
  70. package/src/compilers/theme-compiler.js +144 -0
  71. package/src/compilers/theme-compiler.js.map +1 -0
  72. package/src/compilers/translation-compiler.d.ts +17 -0
  73. package/src/compilers/translation-compiler.d.ts.map +1 -0
  74. package/src/compilers/translation-compiler.js +232 -0
  75. package/src/compilers/translation-compiler.js.map +1 -0
  76. package/src/compilers/web-component/index.d.ts +3 -0
  77. package/src/compilers/web-component/index.d.ts.map +1 -0
  78. package/src/compilers/web-component/index.js +6 -0
  79. package/src/compilers/web-component/index.js.map +1 -0
  80. package/src/compilers/web-component/lib/builder.d.ts +45 -0
  81. package/src/compilers/web-component/lib/builder.d.ts.map +1 -0
  82. package/src/compilers/web-component/lib/builder.js +341 -0
  83. package/src/compilers/web-component/lib/builder.js.map +1 -0
  84. package/src/compilers/web-component/lib/class-name.d.ts +4 -0
  85. package/src/compilers/web-component/lib/class-name.d.ts.map +1 -0
  86. package/src/compilers/web-component/lib/class-name.js +24 -0
  87. package/src/compilers/web-component/lib/class-name.js.map +1 -0
  88. package/src/compilers/web-component/lib/config-data.d.ts +9 -0
  89. package/src/compilers/web-component/lib/config-data.d.ts.map +1 -0
  90. package/src/compilers/web-component/lib/config-data.js +14 -0
  91. package/src/compilers/web-component/lib/config-data.js.map +1 -0
  92. package/src/compilers/web-component/lib/ng-build.d.ts +8 -0
  93. package/src/compilers/web-component/lib/ng-build.d.ts.map +1 -0
  94. package/src/compilers/web-component/lib/ng-build.js +685 -0
  95. package/src/compilers/web-component/lib/ng-build.js.map +1 -0
  96. package/src/compilers/web-component/lib/resource-inliner.d.ts +9 -0
  97. package/src/compilers/web-component/lib/resource-inliner.d.ts.map +1 -0
  98. package/src/compilers/web-component/lib/resource-inliner.js +355 -0
  99. package/src/compilers/web-component/lib/resource-inliner.js.map +1 -0
  100. package/src/compilers/web-component/lib/web-component-name.d.ts +3 -0
  101. package/src/compilers/web-component/lib/web-component-name.d.ts.map +1 -0
  102. package/src/compilers/web-component/lib/web-component-name.js +47 -0
  103. package/src/compilers/web-component/lib/web-component-name.js.map +1 -0
  104. package/src/compilers/web-component/lib/web-component-wrapper.d.ts +3 -0
  105. package/src/compilers/web-component/lib/web-component-wrapper.d.ts.map +1 -0
  106. package/src/compilers/web-component/lib/web-component-wrapper.js +335 -0
  107. package/src/compilers/web-component/lib/web-component-wrapper.js.map +1 -0
  108. package/src/compilers/web-component/model/type.d.ts +29 -0
  109. package/src/compilers/web-component/model/type.d.ts.map +1 -0
  110. package/src/compilers/web-component/model/type.js +3 -0
  111. package/src/compilers/web-component/model/type.js.map +1 -0
  112. package/src/compilers/worker-compiler.d.ts +12 -0
  113. package/src/compilers/worker-compiler.d.ts.map +1 -0
  114. package/src/compilers/worker-compiler.js +123 -0
  115. package/src/compilers/worker-compiler.js.map +1 -0
  116. package/src/compilers/yaml-compiler.d.ts +18 -0
  117. package/src/compilers/yaml-compiler.d.ts.map +1 -0
  118. package/src/compilers/yaml-compiler.js +139 -0
  119. package/src/compilers/yaml-compiler.js.map +1 -0
  120. package/src/core/base-compiler.d.ts +17 -0
  121. package/src/core/base-compiler.d.ts.map +1 -0
  122. package/src/core/base-compiler.js +78 -0
  123. package/src/core/base-compiler.js.map +1 -0
  124. package/src/core/config.d.ts +640 -0
  125. package/src/core/config.d.ts.map +1 -0
  126. package/src/core/config.js +432 -0
  127. package/src/core/config.js.map +1 -0
  128. package/src/core/file-watcher.d.ts +23 -0
  129. package/src/core/file-watcher.d.ts.map +1 -0
  130. package/src/core/file-watcher.js +63 -0
  131. package/src/core/file-watcher.js.map +1 -0
  132. package/src/index.d.ts +31 -0
  133. package/src/index.d.ts.map +1 -0
  134. package/src/index.js +34 -0
  135. package/src/index.js.map +1 -0
  136. package/src/types/asset-configs.d.ts +189 -0
  137. package/src/types/asset-configs.d.ts.map +1 -0
  138. package/src/types/asset-configs.js +7 -0
  139. package/src/types/asset-configs.js.map +1 -0
  140. package/src/types/theme.d.ts +67 -0
  141. package/src/types/theme.d.ts.map +1 -0
  142. package/src/types/theme.js +3 -0
  143. package/src/types/theme.js.map +1 -0
  144. package/src/utils/file-utils.d.ts +16 -0
  145. package/src/utils/file-utils.d.ts.map +1 -0
  146. package/src/utils/file-utils.js +58 -0
  147. package/src/utils/file-utils.js.map +1 -0
  148. package/src/utils/i18n-compile-validator.d.ts +20 -0
  149. package/src/utils/i18n-compile-validator.d.ts.map +1 -0
  150. package/src/utils/i18n-compile-validator.js +198 -0
  151. package/src/utils/i18n-compile-validator.js.map +1 -0
  152. package/src/utils/i18n-fixer.d.ts +39 -0
  153. package/src/utils/i18n-fixer.d.ts.map +1 -0
  154. package/src/utils/i18n-fixer.js +396 -0
  155. package/src/utils/i18n-fixer.js.map +1 -0
  156. package/src/utils/i18n-sanitizer.d.ts +17 -0
  157. package/src/utils/i18n-sanitizer.d.ts.map +1 -0
  158. package/src/utils/i18n-sanitizer.js +101 -0
  159. package/src/utils/i18n-sanitizer.js.map +1 -0
  160. package/src/utils/i18n-utils.d.ts +21 -0
  161. package/src/utils/i18n-utils.d.ts.map +1 -0
  162. package/src/utils/i18n-utils.js +89 -0
  163. package/src/utils/i18n-utils.js.map +1 -0
  164. package/src/utils/interactive-translator.d.ts +18 -0
  165. package/src/utils/interactive-translator.d.ts.map +1 -0
  166. package/src/utils/interactive-translator.js +401 -0
  167. package/src/utils/interactive-translator.js.map +1 -0
  168. package/src/utils/llama-cpp-translator.d.ts +33 -0
  169. package/src/utils/llama-cpp-translator.d.ts.map +1 -0
  170. package/src/utils/llama-cpp-translator.js +267 -0
  171. package/src/utils/llama-cpp-translator.js.map +1 -0
  172. package/src/utils/logger.d.ts +14 -0
  173. package/src/utils/logger.d.ts.map +1 -0
  174. package/src/utils/logger.js +42 -0
  175. package/src/utils/logger.js.map +1 -0
  176. package/src/utils/ollama-translator.d.ts +38 -0
  177. package/src/utils/ollama-translator.d.ts.map +1 -0
  178. package/src/utils/ollama-translator.js +371 -0
  179. package/src/utils/ollama-translator.js.map +1 -0
  180. package/src/utils/option-utils.d.ts +58 -0
  181. package/src/utils/option-utils.d.ts.map +1 -0
  182. package/src/utils/option-utils.js +123 -0
  183. package/src/utils/option-utils.js.map +1 -0
  184. package/src/utils/platform-utils.d.ts +33 -0
  185. package/src/utils/platform-utils.d.ts.map +1 -0
  186. package/src/utils/platform-utils.js +114 -0
  187. package/src/utils/platform-utils.js.map +1 -0
  188. package/src/utils/pwa-utils.d.ts +28 -0
  189. package/src/utils/pwa-utils.d.ts.map +1 -0
  190. package/src/utils/pwa-utils.js +48 -0
  191. package/src/utils/pwa-utils.js.map +1 -0
  192. package/src/utils/translation-manager.d.ts +43 -0
  193. package/src/utils/translation-manager.d.ts.map +1 -0
  194. package/src/utils/translation-manager.js +302 -0
  195. package/src/utils/translation-manager.js.map +1 -0
  196. package/src/utils/web-translation-providers.d.ts +53 -0
  197. package/src/utils/web-translation-providers.d.ts.map +1 -0
  198. package/src/utils/web-translation-providers.js +190 -0
  199. package/src/utils/web-translation-providers.js.map +1 -0
  200. package/src/utils/yaml-utils.d.ts +9 -0
  201. package/src/utils/yaml-utils.d.ts.map +1 -0
  202. package/src/utils/yaml-utils.js +26 -0
  203. package/src/utils/yaml-utils.js.map +1 -0
  204. package/templates/mag-compile.config.js +77 -0
@@ -0,0 +1,943 @@
1
+ const {
2
+ SVGCompiler,
3
+ LogoCompiler,
4
+ ThemeCompiler,
5
+ TranslationCompiler,
6
+ ButtonGroupCompiler,
7
+ FieldCompiler,
8
+ OptionsCompiler,
9
+ CopyCompiler,
10
+ WorkerCompiler,
11
+ TabsCompiler,
12
+ YamlCompiler,
13
+ generateIsolatedPaths,
14
+ DEFAULT_PATHS_CONFIG,
15
+ WebComponentBuilder,
16
+ } = require('../../src/index.js');
17
+
18
+ const fs = require('fs').promises;
19
+ const path = require('path');
20
+
21
+ /**
22
+ * Create a YamlCompiler with validation rules for a specific asset type
23
+ * @param {string} assetType - The asset type (Button, Card, etc.)
24
+ * @param {Object} config - The compiler configuration
25
+ * @param {Array} validationRules - Validation rules for the asset type
26
+ * @returns {YamlCompiler} Configured YAML compiler
27
+ */
28
+ function createYamlCompiler(
29
+ assetType,
30
+ config,
31
+ validationRules = [],
32
+ schema = undefined
33
+ ) {
34
+ const yamlConfig = {
35
+ ...config,
36
+ assetType,
37
+ validationRules,
38
+ ...(schema !== undefined && { schema }),
39
+ };
40
+ return new YamlCompiler(yamlConfig);
41
+ }
42
+
43
+ // Kinds the manifest leaves out because nothing reads them through it: glyphs
44
+ // resolve by SHARED_ICONS and messages by the translation merge, both without a
45
+ // folder listing, and both are an order of magnitude larger than all the config
46
+ // folders put together.
47
+ const MANIFEST_EXCLUDED_FOLDERS = ['icons', 'i18n'];
48
+
49
+ /**
50
+ * Writes `index.json` — every compiled config in the tree, by folder and name.
51
+ *
52
+ * The m-one-ui editor reads its OneAssets from here rather than from a folder a
53
+ * User points at (m-one-ui ADR 0016). Emitted from the compiled output rather
54
+ * than the YAML source, so what the editor offers is what `@magmonium/one`
55
+ * actually resolves at runtime: a name that failed to compile is absent from
56
+ * both.
57
+ *
58
+ * @param {string} outputDir - The compiled asset root (e.g. `libs/one/assets`)
59
+ */
60
+ async function writeAssetManifest(outputDir) {
61
+ const root = path.resolve(outputDir);
62
+ const manifest = {};
63
+
64
+ const entries = await fs.readdir(root, { withFileTypes: true });
65
+ for (const entry of entries) {
66
+ if (!entry.isDirectory()) continue;
67
+ if (MANIFEST_EXCLUDED_FOLDERS.includes(entry.name)) continue;
68
+
69
+ const folder = {};
70
+ for (const file of await fs.readdir(path.join(root, entry.name))) {
71
+ if (!file.endsWith('.json')) continue;
72
+ const raw = await fs.readFile(path.join(root, entry.name, file), 'utf8');
73
+ try {
74
+ folder[file.slice(0, -'.json'.length)] = JSON.parse(raw);
75
+ } catch {
76
+ // One unreadable config is one name missing, not a broken manifest —
77
+ // the rest of the tree stays offerable.
78
+ continue;
79
+ }
80
+ }
81
+ if (Object.keys(folder).length) manifest[entry.name] = folder;
82
+ }
83
+
84
+ const manifestPath = path.join(root, 'index.json');
85
+ await fs.writeFile(manifestPath, JSON.stringify(manifest));
86
+ console.log(
87
+ ` āœ… asset manifest → ${manifestPath} (${
88
+ Object.keys(manifest).length
89
+ } folders)`
90
+ );
91
+ }
92
+
93
+ /**
94
+ * Validation rules for different asset types
95
+ */
96
+ const VALIDATION_RULES = {
97
+ Button: [
98
+ {
99
+ field: 'text',
100
+ validator: (value, asset) => value || asset.iconOnly,
101
+ message:
102
+ "Button missing required 'text' field and is not marked as iconOnly",
103
+ },
104
+ ],
105
+ Card: [],
106
+ Chart: [],
107
+ Jumbotron: [],
108
+ Watermark: [],
109
+ Modal: [],
110
+ Remote: [
111
+ {
112
+ field: 'url',
113
+ required: true,
114
+ message: "Remote configuration missing required 'url' field",
115
+ },
116
+ ],
117
+ DatePicker: [],
118
+ Carousel: [
119
+ {
120
+ field: 'slides',
121
+ required: true,
122
+ message: "Carousel missing required 'slides' field",
123
+ },
124
+ ],
125
+ Steps: [
126
+ {
127
+ field: 'steps',
128
+ required: true,
129
+ message: "Steps missing required 'steps' field",
130
+ },
131
+ ],
132
+ Sort: [],
133
+ Graph: [],
134
+ };
135
+
136
+ /**
137
+ * Clean destination folders before asset compilation
138
+ * @param {Object} paths - Resolved paths configuration
139
+ * @param {Object} options - Command options
140
+ */
141
+ async function cleanDestinationFolders(paths, options) {
142
+ if (options.clean === false) {
143
+ console.log('šŸ”„ Skipping folder cleaning (--no-clean flag)');
144
+ return;
145
+ }
146
+
147
+ console.log('🧹 Cleaning destination folders...');
148
+
149
+ // 1. Clean Web Component temporary directories and files
150
+ const tempDirs = ['.tmp-staging-wc', '.tmp-compiled-wc'];
151
+ for (const dir of tempDirs) {
152
+ try {
153
+ const fullPath = path.resolve(process.cwd(), dir);
154
+ const stat = await fs.stat(fullPath).catch(() => null);
155
+ if (stat && stat.isDirectory()) {
156
+ if (options.verbose)
157
+ console.log(` šŸ—‘ļø Cleaning temporary directory: ${dir}`);
158
+ await fs.rm(fullPath, { recursive: true, force: true });
159
+ }
160
+ } catch {
161
+ // best-effort cleanup
162
+ }
163
+ }
164
+
165
+ // Clean temporary tsconfig files
166
+ try {
167
+ const files = await fs.readdir(process.cwd());
168
+ const tempTsConfigs = files.filter(
169
+ (f) => f.startsWith('tsconfig.wc.') && f.endsWith('.json')
170
+ );
171
+ for (const f of tempTsConfigs) {
172
+ if (options.verbose)
173
+ console.log(` šŸ—‘ļø Cleaning temporary config: ${f}`);
174
+ await fs.unlink(path.join(process.cwd(), f));
175
+ }
176
+ } catch {
177
+ // best-effort cleanup
178
+ }
179
+
180
+ // 2. Collect all unique output paths from configuration
181
+ const outputPaths = new Set();
182
+
183
+ // Extract output paths from all asset types
184
+ Object.keys(paths).forEach((assetType) => {
185
+ const assetConfig = paths[assetType];
186
+ if (assetConfig.output) {
187
+ if (Array.isArray(assetConfig.output)) {
188
+ assetConfig.output.forEach((outputPath) => outputPaths.add(outputPath));
189
+ } else {
190
+ outputPaths.add(assetConfig.output);
191
+ }
192
+ }
193
+ });
194
+
195
+ // Delete each output directory completely
196
+ for (const outputPath of outputPaths) {
197
+ try {
198
+ // Check if directory exists before attempting to delete
199
+ const stat = await fs.stat(outputPath).catch(() => null);
200
+ if (stat && stat.isDirectory()) {
201
+ if (options.verbose) {
202
+ console.log(` šŸ—‘ļø Deleting: ${outputPath}`);
203
+ }
204
+
205
+ // Delete the entire directory and its contents
206
+ await fs.rm(outputPath, { recursive: true, force: true });
207
+ } else if (options.verbose) {
208
+ console.log(` āš ļø Directory does not exist: ${outputPath}`);
209
+ }
210
+ } catch (error) {
211
+ console.warn(` āš ļø Failed to delete ${outputPath}: ${error.message}`);
212
+ // Continue with other directories instead of failing completely
213
+ }
214
+ }
215
+
216
+ console.log('āœ… Destination folders cleaned');
217
+ }
218
+
219
+ /**
220
+ * Recursively copy a directory to a destination
221
+ * @param {Object} fsPromises - fs.promises module
222
+ * @param {Object} pathModule - path module
223
+ * @param {string} src - Source directory
224
+ * @param {string} dest - Destination directory
225
+ */
226
+ async function copyDirRecursive(fsPromises, pathModule, src, dest) {
227
+ await fsPromises.mkdir(dest, { recursive: true });
228
+ const items = await fsPromises.readdir(src, { withFileTypes: true });
229
+ for (const item of items) {
230
+ const srcPath = pathModule.join(src, item.name);
231
+ const destPath = pathModule.join(dest, item.name);
232
+ if (item.isDirectory()) {
233
+ await copyDirRecursive(fsPromises, pathModule, srcPath, destPath);
234
+ } else {
235
+ await fsPromises.copyFile(srcPath, destPath);
236
+ }
237
+ }
238
+ }
239
+
240
+ function assetsCommand(program) {
241
+ program
242
+ .command('assets')
243
+ .description(
244
+ 'Clean destination folders and compile assets from mag_assets to assets directory'
245
+ )
246
+ .option('-c, --config <path>', 'Config file path', './mag-cli.config.js')
247
+ .option(
248
+ '-p, --path <path>',
249
+ 'Base path for mag_assets and assets directories (default: src/shared)'
250
+ )
251
+ .option('-w, --watch', 'Watch for changes')
252
+ .option('-v, --verbose', 'Verbose output')
253
+ .option('--copy-only', 'Only copy files, skip compilation')
254
+ .option(
255
+ '--clean',
256
+ 'Clean destination folders before compilation (default: true)'
257
+ )
258
+ .option(
259
+ '--no-clean',
260
+ 'Skip cleaning destination folders before compilation'
261
+ )
262
+ .action(async (options) => {
263
+ let config = {};
264
+
265
+ // Try to load config file
266
+ try {
267
+ config = require(require('path').resolve(options.config));
268
+ } catch {
269
+ console.log(
270
+ `No config file found at ${options.config}, using defaults`
271
+ );
272
+ }
273
+
274
+ // Function to resolve paths from config or command line options
275
+ const resolvePathsConfig = () => {
276
+ // Priority: config.paths > command line -p option > defaults
277
+ if (config.paths) {
278
+ return config.paths;
279
+ }
280
+
281
+ if (options.path) {
282
+ // Legacy support for -p option
283
+ return {
284
+ source: `${options.path}/mag_assets`,
285
+ output: `${options.path}/assets`,
286
+ };
287
+ }
288
+
289
+ return DEFAULT_PATHS_CONFIG;
290
+ };
291
+
292
+ // Get source-isolated paths for compilation
293
+ const pathsConfig = resolvePathsConfig();
294
+ const sourcePathPairs = generateIsolatedPaths(pathsConfig);
295
+
296
+ // Log multiple project info if array config is used
297
+ if (Array.isArray(pathsConfig)) {
298
+ console.log(
299
+ `šŸ“ Processing ${pathsConfig.length} projects with isolated compilation:`
300
+ );
301
+ pathsConfig.forEach((pathConfig, index) => {
302
+ console.log(
303
+ ` ${index + 1}. ${pathConfig.source} → ${pathConfig.output}`
304
+ );
305
+ });
306
+ } else {
307
+ console.log(
308
+ `šŸ“ Processing single project: ${pathsConfig.source} → ${pathsConfig.output}`
309
+ );
310
+ }
311
+
312
+ // Collect all base output directories (resolved) for cross-project asset lookup
313
+ const allBaseOutputDirs = (
314
+ Array.isArray(pathsConfig) ? pathsConfig : [pathsConfig]
315
+ ).map((p) => path.resolve(p.output));
316
+
317
+ // For cleaning, we need to collect all output paths
318
+ const allOutputPaths = new Set();
319
+ sourcePathPairs.forEach((pair) => {
320
+ Object.values(pair.paths).forEach((assetConfig) => {
321
+ if (Array.isArray(assetConfig.output)) {
322
+ assetConfig.output.forEach((path) => allOutputPaths.add(path));
323
+ } else {
324
+ allOutputPaths.add(assetConfig.output);
325
+ }
326
+ });
327
+ });
328
+
329
+ // Clean destination folders before compilation (unless --no-clean is specified)
330
+ if (!options.watch) {
331
+ // Convert output paths set to a structure that cleanDestinationFolders expects
332
+ const pathsForClean = {};
333
+ let index = 0;
334
+ allOutputPaths.forEach((outputPath) => {
335
+ pathsForClean[`output_${index++}`] = { output: outputPath };
336
+ });
337
+ await cleanDestinationFolders(pathsForClean, options);
338
+ }
339
+
340
+ // Process each source-output pair independently for isolated compilation
341
+ for (const sourcePathPair of sourcePathPairs) {
342
+ console.log(
343
+ `\nšŸ”§ Compiling assets for source: ${sourcePathPair.source}`
344
+ );
345
+
346
+ // Build config for this specific source-output pair
347
+ const PATHS = sourcePathPair.paths;
348
+ const originalPathConfig = Array.isArray(pathsConfig)
349
+ ? pathsConfig.find((p) => p.source === sourcePathPair.source)
350
+ : pathsConfig;
351
+ const buildConfig = {
352
+ svg: {
353
+ inputPaths: PATHS.svg.input,
354
+ outputPaths: PATHS.svg.output,
355
+ spriteSheet: true,
356
+ optimize: true,
357
+ ...(originalPathConfig?.tsOutputPath && { tsOutputPath: path.resolve(originalPathConfig.tsOutputPath) }),
358
+ ...config.svg,
359
+ },
360
+ theme: {
361
+ inputPaths: PATHS.theme.input,
362
+ outputPaths: PATHS.theme.output,
363
+ format: 'scss',
364
+ sassVariables: true,
365
+ ...config.theme,
366
+ },
367
+ i18n: {
368
+ inputPaths: PATHS.i18n.input,
369
+ outputPaths: PATHS.i18n.output,
370
+ defaultLocale: 'en',
371
+ ...config.i18n,
372
+ },
373
+ logo: {
374
+ inputPaths: PATHS.logo.input,
375
+ outputPaths: PATHS.logo.output,
376
+ ...config.logo,
377
+ },
378
+ forms: {
379
+ inputPaths: PATHS.forms.input,
380
+ outputPaths: PATHS.forms.output,
381
+ ...config.forms,
382
+ },
383
+ buttons: {
384
+ inputPaths: PATHS.buttons.input,
385
+ outputPaths: PATHS.buttons.output,
386
+ ...config.buttons,
387
+ },
388
+ badges: {
389
+ inputPaths: PATHS.badges.input,
390
+ outputPaths: PATHS.badges.output,
391
+ ...config.badges,
392
+ },
393
+ buttonGroups: {
394
+ inputPaths: PATHS.buttonGroups.input,
395
+ outputPaths: [
396
+ ...PATHS.buttonGroups.output,
397
+ ...allBaseOutputDirs.map((b) => path.join(b, 'button_groups')),
398
+ ],
399
+ ...config.buttonGroups,
400
+ },
401
+ fields: {
402
+ inputPaths: PATHS.fields.input,
403
+ outputPaths: [
404
+ ...PATHS.fields.output,
405
+ ...allBaseOutputDirs.map((b) => path.join(b, 'fields')),
406
+ ],
407
+ ...config.fields,
408
+ },
409
+ options: {
410
+ inputPaths: PATHS.options.input,
411
+ outputPaths: PATHS.options.output,
412
+ ...config.options,
413
+ },
414
+ nav: {
415
+ inputPaths: PATHS.navs.input,
416
+ outputPaths: [
417
+ ...PATHS.navs.output,
418
+ ...allBaseOutputDirs.map((b) => path.join(b, 'navs')),
419
+ ],
420
+ ...config.nav,
421
+ },
422
+ menu: {
423
+ inputPaths: PATHS.menu.input,
424
+ outputPaths: PATHS.menu.output,
425
+ ...config.menu,
426
+ },
427
+ headers: {
428
+ inputPaths: PATHS.headers.input,
429
+ outputPaths: PATHS.headers.output,
430
+ ...config.headers,
431
+ },
432
+ modal: {
433
+ inputPaths: PATHS.modal.input,
434
+ outputPaths: PATHS.modal.output,
435
+ ...config.modal,
436
+ },
437
+ copy: {
438
+ inputPaths: PATHS.copy.input,
439
+ outputPaths: PATHS.copy.output,
440
+ preserveStructure: true,
441
+ overwrite: true,
442
+ // Exclude files that will be compiled
443
+ exclude: [
444
+ '**/*.svg',
445
+ '**/*.scss',
446
+ '**/*.sass',
447
+ '**/*.css',
448
+ '**/*.json',
449
+ '**/*.yaml',
450
+ '**/*.yml',
451
+ '**/theme/*.ts', // Exclude TypeScript theme files since they're compiled to JSON + CSS
452
+ ],
453
+ ...config.copy,
454
+ },
455
+ workers: {
456
+ inputPaths: PATHS.workers.input,
457
+ outputPaths: PATHS.workers.output,
458
+ minify: true,
459
+ target: 'es2020',
460
+ format: 'iife',
461
+ ...config.workers,
462
+ },
463
+ cards: {
464
+ inputPaths: PATHS.cards.input,
465
+ outputPaths: PATHS.cards.output,
466
+ ...config.cards,
467
+ },
468
+ charts: {
469
+ inputPaths: PATHS.charts.input,
470
+ outputPaths: PATHS.charts.output,
471
+ ...config.charts,
472
+ },
473
+ jumbotron: {
474
+ inputPaths: PATHS.jumbotron.input,
475
+ outputPaths: PATHS.jumbotron.output,
476
+ ...config.jumbotron,
477
+ },
478
+ watermark: {
479
+ inputPaths: PATHS.watermark.input,
480
+ outputPaths: PATHS.watermark.output,
481
+ ...config.watermark,
482
+ },
483
+ tabs: {
484
+ inputPaths: PATHS.tabs.input,
485
+ outputPaths: PATHS.tabs.output,
486
+ ...config.tabs,
487
+ },
488
+ modal: {
489
+ inputPaths: PATHS.modal.input,
490
+ outputPaths: PATHS.modal.output,
491
+ ...config.modal,
492
+ },
493
+ remote: {
494
+ inputPaths: PATHS.remote.input,
495
+ outputPaths: PATHS.remote.output,
496
+ ...config.remote,
497
+ },
498
+ ul: {
499
+ inputPaths: PATHS.ul.input,
500
+ outputPaths: PATHS.ul.output,
501
+ ...config.ul,
502
+ },
503
+ datePicker: {
504
+ inputPaths: PATHS.datePicker.input,
505
+ outputPaths: PATHS.datePicker.output,
506
+ ...config.datePicker,
507
+ },
508
+ stepper: {
509
+ inputPaths: PATHS.stepper.input,
510
+ outputPaths: PATHS.stepper.output,
511
+ ...config.stepper,
512
+ },
513
+ carousel: {
514
+ inputPaths: PATHS.carousel.input,
515
+ outputPaths: PATHS.carousel.output,
516
+ ...config.carousel,
517
+ },
518
+ steps: {
519
+ inputPaths: PATHS.steps.input,
520
+ outputPaths: PATHS.steps.output,
521
+ ...config.steps,
522
+ },
523
+ sort: {
524
+ inputPaths: PATHS.sort.input,
525
+ outputPaths: PATHS.sort.output,
526
+ ...config.sort,
527
+ },
528
+ pagination: {
529
+ inputPaths: PATHS.pagination.input,
530
+ outputPaths: PATHS.pagination.output,
531
+ ...config.pagination,
532
+ },
533
+ table: {
534
+ inputPaths: PATHS.table.input,
535
+ outputPaths: PATHS.table.output,
536
+ ...config.table,
537
+ },
538
+ accordions: {
539
+ inputPaths: PATHS.accordions.input,
540
+ outputPaths: PATHS.accordions.output,
541
+ ...config.accordions,
542
+ },
543
+ accordion_groups: {
544
+ inputPaths: PATHS.accordion_groups.input,
545
+ outputPaths: PATHS.accordion_groups.output,
546
+ ...config.accordion_groups,
547
+ },
548
+ graphs: {
549
+ inputPaths: PATHS.graphs.input,
550
+ outputPaths: PATHS.graphs.output,
551
+ ...config.graphs,
552
+ },
553
+ settings: {
554
+ inputPaths: PATHS.settings.input,
555
+ outputPaths: [
556
+ ...PATHS.settings.output,
557
+ ...allBaseOutputDirs.map((b) => path.join(b, 'settings')),
558
+ ],
559
+ ...config.settings,
560
+ },
561
+ };
562
+
563
+ const compilers = [];
564
+
565
+ // Only add compilers if copy-only is not specified
566
+ if (!options.copyOnly) {
567
+ // Add compilers for specific file types in dependency order
568
+ compilers.push(new SVGCompiler(buildConfig.svg));
569
+ compilers.push(new ThemeCompiler(buildConfig.theme));
570
+ compilers.push(new TranslationCompiler(buildConfig.i18n));
571
+ compilers.push(new LogoCompiler(buildConfig.logo));
572
+
573
+ // Compile basic components first (dependencies)
574
+ if (buildConfig.options) {
575
+ compilers.push(new OptionsCompiler(buildConfig.options));
576
+ }
577
+
578
+ if (buildConfig.fields) {
579
+ compilers.push(new FieldCompiler(buildConfig.fields));
580
+ }
581
+
582
+ if (buildConfig.buttons) {
583
+ compilers.push(
584
+ createYamlCompiler(
585
+ 'Button',
586
+ buildConfig.buttons,
587
+ VALIDATION_RULES.Button
588
+ )
589
+ );
590
+ }
591
+
592
+ if (buildConfig.badges) {
593
+ compilers.push(createYamlCompiler('Badge', buildConfig.badges));
594
+ }
595
+
596
+ if (buildConfig.cards) {
597
+ compilers.push(
598
+ createYamlCompiler(
599
+ 'Card',
600
+ buildConfig.cards,
601
+ VALIDATION_RULES.Card
602
+ )
603
+ );
604
+ }
605
+
606
+ if (buildConfig.charts) {
607
+ compilers.push(
608
+ createYamlCompiler(
609
+ 'Chart',
610
+ buildConfig.charts,
611
+ VALIDATION_RULES.Chart
612
+ )
613
+ );
614
+ }
615
+
616
+ if (buildConfig.jumbotron) {
617
+ compilers.push(
618
+ createYamlCompiler(
619
+ 'Jumbotron',
620
+ buildConfig.jumbotron,
621
+ VALIDATION_RULES.Jumbotron
622
+ )
623
+ );
624
+ }
625
+
626
+ if (buildConfig.watermark) {
627
+ compilers.push(
628
+ createYamlCompiler(
629
+ 'Watermark',
630
+ buildConfig.watermark,
631
+ VALIDATION_RULES.Watermark
632
+ )
633
+ );
634
+ }
635
+
636
+ if (buildConfig.tabs) {
637
+ compilers.push(new TabsCompiler(buildConfig.tabs));
638
+ }
639
+
640
+ if (buildConfig.modal) {
641
+ compilers.push(
642
+ createYamlCompiler(
643
+ 'Modal',
644
+ buildConfig.modal,
645
+ VALIDATION_RULES.Modal
646
+ )
647
+ );
648
+ }
649
+
650
+ if (buildConfig.ul) {
651
+ compilers.push(
652
+ createYamlCompiler('Ul', buildConfig.ul, VALIDATION_RULES.ul)
653
+ );
654
+ }
655
+
656
+ if (buildConfig.datePicker) {
657
+ compilers.push(
658
+ createYamlCompiler(
659
+ 'DatePicker',
660
+ buildConfig.datePicker,
661
+ VALIDATION_RULES.DatePicker
662
+ )
663
+ );
664
+ }
665
+
666
+ if (buildConfig.menu) {
667
+ compilers.push(createYamlCompiler('menu', buildConfig.menu));
668
+ }
669
+
670
+ if (buildConfig.headers) {
671
+ compilers.push(createYamlCompiler('menu', buildConfig.headers));
672
+ }
673
+
674
+ if (buildConfig.modal) {
675
+ compilers.push(createYamlCompiler('modal', buildConfig.modal));
676
+ }
677
+
678
+ if (buildConfig.stepper) {
679
+ compilers.push(createYamlCompiler('Stepper', buildConfig.stepper));
680
+ }
681
+
682
+ if (buildConfig.forms) {
683
+ compilers.push(
684
+ createYamlCompiler(
685
+ 'Form',
686
+ buildConfig.forms,
687
+ VALIDATION_RULES.Form
688
+ )
689
+ );
690
+ }
691
+
692
+ if (buildConfig.buttonGroups) {
693
+ compilers.push(new ButtonGroupCompiler(buildConfig.buttonGroups));
694
+ }
695
+
696
+ if (buildConfig.nav) {
697
+ compilers.push(
698
+ createYamlCompiler(
699
+ 'Nav',
700
+ buildConfig.nav,
701
+ [],
702
+ 'libs/cli/schemas/nav.schema.json'
703
+ )
704
+ );
705
+ }
706
+
707
+ if (buildConfig.remote) {
708
+ compilers.push(
709
+ new WebComponentBuilder(
710
+ buildConfig.remote.inputPaths,
711
+ buildConfig.remote.outputPaths
712
+ )
713
+ );
714
+ }
715
+
716
+ if (buildConfig.carousel) {
717
+ compilers.push(
718
+ createYamlCompiler(
719
+ 'Carousel',
720
+ buildConfig.carousel,
721
+ VALIDATION_RULES.Carousel
722
+ )
723
+ );
724
+ }
725
+
726
+ if (buildConfig.steps) {
727
+ compilers.push(
728
+ createYamlCompiler(
729
+ 'Steps',
730
+ buildConfig.steps,
731
+ VALIDATION_RULES.Steps
732
+ )
733
+ );
734
+ }
735
+
736
+ if (buildConfig.sort) {
737
+ compilers.push(
738
+ createYamlCompiler(
739
+ 'Sort',
740
+ buildConfig.sort,
741
+ VALIDATION_RULES.Sort
742
+ )
743
+ );
744
+ }
745
+
746
+ if (buildConfig.pagination) {
747
+ compilers.push(
748
+ createYamlCompiler('Pagination', buildConfig.pagination)
749
+ );
750
+ }
751
+
752
+ if (buildConfig.table) {
753
+ compilers.push(createYamlCompiler('Table', buildConfig.table));
754
+ }
755
+
756
+ if (buildConfig.accordions) {
757
+ compilers.push(
758
+ createYamlCompiler('Accordion', buildConfig.accordions)
759
+ );
760
+ }
761
+
762
+ if (buildConfig.accordion_groups) {
763
+ compilers.push(
764
+ createYamlCompiler('AccordionGroup', buildConfig.accordion_groups)
765
+ );
766
+ }
767
+
768
+ if (buildConfig.graphs) {
769
+ compilers.push(
770
+ createYamlCompiler(
771
+ 'Graph',
772
+ buildConfig.graphs,
773
+ VALIDATION_RULES.Graph
774
+ )
775
+ );
776
+ }
777
+
778
+ if (buildConfig.settings) {
779
+ compilers.push(
780
+ createYamlCompiler('Settings', buildConfig.settings)
781
+ );
782
+ }
783
+
784
+ // Compile workers
785
+ if (buildConfig.workers) {
786
+ compilers.push(new WorkerCompiler(buildConfig.workers));
787
+ }
788
+ }
789
+
790
+ // Always add copy compiler for remaining files
791
+ compilers.push(new CopyCompiler(buildConfig.copy));
792
+
793
+ // Run all compilers for this source-output pair
794
+ for (const compiler of compilers) {
795
+ if (options.watch) {
796
+ await compiler.watch();
797
+ } else {
798
+ await compiler.compile();
799
+ }
800
+ }
801
+
802
+ // After the compilers, never before: the manifest lists what they
803
+ // actually wrote. Skipped under --watch, where a compiler holds the
804
+ // process open and the output is never finished.
805
+ if (originalPathConfig?.assetManifest && !options.watch) {
806
+ await writeAssetManifest(originalPathConfig.output);
807
+ }
808
+
809
+ console.log(
810
+ `āœ… Completed compilation for source: ${sourcePathPair.source}`
811
+ );
812
+ }
813
+
814
+ // After all compilations, aggregate remote app configs into the host app
815
+ if (!options.watch && Array.isArray(pathsConfig)) {
816
+ const hostConfig = pathsConfig.find((p) => p.isHost);
817
+ const remoteConfigs = pathsConfig.filter((p) => p.isRemote);
818
+
819
+ if (hostConfig && remoteConfigs.length > 0) {
820
+ console.log(
821
+ '\nšŸ”— Aggregating remote app configs and assets to host...'
822
+ );
823
+ const hostRemoteDir = path.resolve(hostConfig.output, 'remote');
824
+ const hostOutputDir = path.resolve(hostConfig.output);
825
+
826
+ for (const remoteConfig of remoteConfigs) {
827
+ const remoteOutputDir = path.resolve(remoteConfig.output, 'remote');
828
+ const remoteBaseOutputDir = path.resolve(remoteConfig.output);
829
+
830
+ try {
831
+ const files = await fs.readdir(remoteOutputDir);
832
+ const wcJsonFiles = files.filter((f) => f.endsWith('.json'));
833
+
834
+ const migratedApps = new Set();
835
+
836
+ for (const jsonFile of wcJsonFiles) {
837
+ // Only process app-wc.json
838
+ if (jsonFile !== 'app-wc.json') continue;
839
+
840
+ const jsonPath = path.join(remoteOutputDir, jsonFile);
841
+ const content = await fs.readFile(jsonPath, 'utf8');
842
+ const wcConfig = JSON.parse(content);
843
+
844
+ if (wcConfig.app) {
845
+ // Copy WC config JSON to host remote dir: remote/{app}/app-wc.json
846
+ const destDir = path.join(hostRemoteDir, wcConfig.app);
847
+ await fs.mkdir(destDir, { recursive: true });
848
+ await fs.writeFile(
849
+ path.join(destDir, 'app-wc.json'),
850
+ content
851
+ );
852
+ console.log(
853
+ ` āœ… ${remoteConfig.output}/remote/app-wc.json → host/remote/${wcConfig.app}/app-wc.json`
854
+ );
855
+
856
+ // Copy compiled remote assets (the JS bundle) to host's remote dir as well
857
+ // This ensures the Host has the script bundle at remote/{app}/{bundle}.js
858
+ try {
859
+ const bundleFile = wcConfig.componentFileName;
860
+ if (bundleFile) {
861
+ const srcBundle = path.join(remoteOutputDir, bundleFile);
862
+ const destBundle = path.join(destDir, bundleFile);
863
+ await fs.copyFile(srcBundle, destBundle);
864
+ console.log(
865
+ ` āœ… script bundle → host/remote/${wcConfig.app}/${bundleFile}`
866
+ );
867
+ }
868
+ } catch (e) {
869
+ console.warn(` āš ļø Failed to copy bundle: ${e.message}`);
870
+ }
871
+
872
+ // Copy compiled non-remote assets to host's local-dist/{app}-wc/
873
+ if (!migratedApps.has(wcConfig.app)) {
874
+ migratedApps.add(wcConfig.app);
875
+ const localDistDest = path.join(
876
+ hostOutputDir,
877
+ 'local-dist',
878
+ `${wcConfig.app}-wc`
879
+ );
880
+ try {
881
+ const baseItems = await fs.readdir(remoteBaseOutputDir, {
882
+ withFileTypes: true,
883
+ });
884
+ const assetDirs = baseItems.filter(
885
+ (item) => item.isDirectory() && item.name !== 'remote'
886
+ );
887
+ for (const dir of assetDirs) {
888
+ const srcDir = path.join(remoteBaseOutputDir, dir.name);
889
+ const destAssetDir = path.join(localDistDest, dir.name);
890
+ await copyDirRecursive(fs, path, srcDir, destAssetDir);
891
+ console.log(
892
+ ` āœ… ${remoteConfig.output}/${dir.name} → host/local-dist/${wcConfig.app}-wc/${dir.name}`
893
+ );
894
+ }
895
+ } catch (copyErr) {
896
+ if (copyErr.code !== 'ENOENT') {
897
+ console.warn(
898
+ ` āš ļø Could not copy assets for ${wcConfig.app}: ${copyErr.message}`
899
+ );
900
+ }
901
+ }
902
+ }
903
+ }
904
+ }
905
+ } catch (err) {
906
+ if (err.code !== 'ENOENT') {
907
+ console.warn(
908
+ ` āš ļø Could not aggregate from ${remoteOutputDir}: ${err.message}`
909
+ );
910
+ }
911
+ }
912
+ }
913
+
914
+ console.log('āœ… Remote config aggregation complete');
915
+ }
916
+ }
917
+
918
+ if (options.watch) {
919
+ const pathsConfig =
920
+ config.paths ||
921
+ (options.path
922
+ ? { source: `${options.path}/mag_assets` }
923
+ : DEFAULT_PATHS_CONFIG);
924
+
925
+ if (Array.isArray(pathsConfig)) {
926
+ console.log(
927
+ `šŸ‘€ Watching for changes in ${pathsConfig.length} projects:`
928
+ );
929
+ pathsConfig.forEach((pathConfig, index) => {
930
+ console.log(` ${index + 1}. ${pathConfig.source}/`);
931
+ });
932
+ } else {
933
+ const sourcePath = pathsConfig.source;
934
+ console.log(`šŸ‘€ Watching for changes in ${sourcePath}/...`);
935
+ }
936
+
937
+ // Keep the process running
938
+ process.stdin.resume();
939
+ }
940
+ });
941
+ }
942
+
943
+ module.exports = assetsCommand;