@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,591 @@
1
+ const path = require('path');
2
+ const fs = require('fs');
3
+ const { execSync } = require('child_process');
4
+ const inquirer = require('inquirer');
5
+
6
+ // Import initialization modules
7
+ const createProjectStructure = require('./init/project-structure');
8
+ const createPackageJson = require('./init/package-json');
9
+ const createConfigFiles = require('./init/config-files');
10
+ const createSourceFiles = require('./init/source-files');
11
+ const createMagAssetsStructure = require('./init/mag-assets');
12
+ const createMagCliConfig = require('./init/mag-cli-config');
13
+ const createPwaFiles = require('./init/pwa-files');
14
+ const {
15
+ createMonorepoStructure,
16
+ addReactDependenciesToMonorepo,
17
+ addAngularDependenciesToMonorepo,
18
+ } = require('./init/monorepo-structure');
19
+
20
+ // Constants
21
+ const SUPPORTED_TECHNOLOGIES = ['angular', 'react'];
22
+ const DEFAULT_PATHS_CONFIG = {
23
+ source: 'src/shared/mag_assets',
24
+ output: 'src/shared/assets',
25
+ };
26
+ const MAG_ASSETS_SUBDIRS = [
27
+ 'buttons',
28
+ 'button_groups',
29
+ 'fields',
30
+ 'forms',
31
+ 'options',
32
+ 'nav',
33
+ 'icons',
34
+ 'pwa',
35
+ 'theme',
36
+ 'i18n',
37
+ 'images',
38
+ 'workers',
39
+ 'cards',
40
+ 'jumbotron',
41
+ 'watermark',
42
+ 'modal',
43
+ 'remote',
44
+ ];
45
+
46
+ // Helper Functions
47
+ class MonorepoDetector {
48
+ static detect() {
49
+ const currentDir = process.cwd();
50
+ let checkDir = currentDir;
51
+
52
+ while (checkDir !== path.dirname(checkDir)) {
53
+ const packageJsonPath = path.join(checkDir, 'package.json');
54
+ const appsPath = path.join(checkDir, 'apps');
55
+
56
+ if (fs.existsSync(packageJsonPath) && fs.existsSync(appsPath)) {
57
+ try {
58
+ const packageJson = JSON.parse(
59
+ fs.readFileSync(packageJsonPath, 'utf-8')
60
+ );
61
+
62
+ if (
63
+ packageJson.workspaces ||
64
+ packageJson.name?.includes('-monorepo')
65
+ ) {
66
+ return {
67
+ isMonorepo: true,
68
+ rootPath: checkDir,
69
+ packageJson,
70
+ };
71
+ }
72
+ } catch {
73
+ // Continue checking parent directories
74
+ }
75
+ }
76
+
77
+ checkDir = path.dirname(checkDir);
78
+ }
79
+
80
+ return { isMonorepo: false };
81
+ }
82
+ }
83
+
84
+ class MagAssetsManager {
85
+ static async createStructure(pathsConfig) {
86
+ if (Array.isArray(pathsConfig)) {
87
+ return this.createMultipleStructures(pathsConfig);
88
+ }
89
+
90
+ return this.createSingleStructure(pathsConfig);
91
+ }
92
+
93
+ static async createMultipleStructures(pathsConfigs) {
94
+ console.log(
95
+ `🗂️ Creating mag_assets structures for ${pathsConfigs.length} projects:`
96
+ );
97
+
98
+ for (let i = 0; i < pathsConfigs.length; i++) {
99
+ const config = pathsConfigs[i];
100
+ const sourcePath = config.source;
101
+
102
+ console.log(` ${i + 1}. Creating structure in ${sourcePath}/`);
103
+
104
+ try {
105
+ await this.createSingleStructure(sourcePath);
106
+ console.log(` ✅ Structure created in ${sourcePath}/`);
107
+ } catch (error) {
108
+ console.error(
109
+ ` ❌ Failed to create structure in ${sourcePath}/: ${error.message}`
110
+ );
111
+ }
112
+ }
113
+ }
114
+
115
+ static async createSingleStructure(pathsConfig) {
116
+ const sourcePath =
117
+ typeof pathsConfig === 'string' ? pathsConfig : pathsConfig.source;
118
+
119
+ // Create main directory
120
+ const fullSourcePath = path.resolve(sourcePath);
121
+ fs.mkdirSync(fullSourcePath, { recursive: true });
122
+
123
+ // Create subdirectories
124
+ MAG_ASSETS_SUBDIRS.forEach((subdir) => {
125
+ fs.mkdirSync(path.join(fullSourcePath, subdir), { recursive: true });
126
+ });
127
+
128
+ // Create assets structure
129
+ await createMagAssetsStructure(sourcePath);
130
+ }
131
+ }
132
+
133
+ class ProjectSetupManager {
134
+ constructor() {
135
+ this.originalCwd = process.cwd();
136
+ }
137
+
138
+ async setupProject(projectName, options, technology, monorepoInfo) {
139
+ const { companyName, isCreatingMonorepo, projectPath, workingDir } =
140
+ await this.determineProjectPaths(projectName, options, monorepoInfo);
141
+
142
+ await this.validateAndCreateProject(workingDir, () =>
143
+ this.createProjectFiles(
144
+ projectName,
145
+ technology,
146
+ companyName,
147
+ isCreatingMonorepo,
148
+ monorepoInfo,
149
+ projectPath,
150
+ workingDir
151
+ )
152
+ );
153
+
154
+ await this.setupDependenciesAndGit(
155
+ isCreatingMonorepo,
156
+ monorepoInfo,
157
+ projectPath,
158
+ workingDir
159
+ );
160
+
161
+ this.displaySuccessMessage(
162
+ projectName,
163
+ companyName,
164
+ isCreatingMonorepo,
165
+ monorepoInfo
166
+ );
167
+
168
+ return { companyName, isCreatingMonorepo, projectPath, workingDir };
169
+ }
170
+
171
+ async determineProjectPaths(projectName, options, monorepoInfo) {
172
+ let companyName = options.company;
173
+ let isCreatingMonorepo = false;
174
+ let projectPath, workingDir;
175
+
176
+ if (monorepoInfo.isMonorepo) {
177
+ console.log(`📁 Detected existing monorepo at: ${monorepoInfo.rootPath}`);
178
+ console.log(`📦 Adding app to existing monorepo...`);
179
+
180
+ projectPath = path.join(monorepoInfo.rootPath, 'apps', projectName);
181
+ workingDir = projectPath;
182
+ companyName =
183
+ monorepoInfo.packageJson.name?.replace('-monorepo', '') || 'company';
184
+ } else {
185
+ companyName = await this.getCompanyName(companyName);
186
+ isCreatingMonorepo = true;
187
+
188
+ const monorepoName = `${companyName}-monorepo`;
189
+ projectPath = path.resolve(options.path, monorepoName);
190
+ workingDir = path.join(projectPath, 'apps', projectName);
191
+
192
+ console.log(`📍 Creating monorepo: ${monorepoName}`);
193
+ console.log(`📍 App location: ${path.join('apps', projectName)}`);
194
+ }
195
+
196
+ return { companyName, isCreatingMonorepo, projectPath, workingDir };
197
+ }
198
+
199
+ async getCompanyName(providedCompanyName) {
200
+ if (providedCompanyName) return providedCompanyName;
201
+
202
+ const answers = await inquirer.prompt([
203
+ {
204
+ type: 'input',
205
+ name: 'companyName',
206
+ message: 'Enter your company name for the monorepo:',
207
+ default: 'mycompany',
208
+ validate: this.validateCompanyName,
209
+ },
210
+ ]);
211
+
212
+ return answers.companyName.trim().toLowerCase();
213
+ }
214
+
215
+ validateCompanyName(input) {
216
+ if (!input.trim()) return 'Company name is required';
217
+ if (!/^[a-z0-9-]+$/.test(input.trim())) {
218
+ return 'Company name should only contain lowercase letters, numbers, and hyphens';
219
+ }
220
+ return true;
221
+ }
222
+
223
+ async validateAndCreateProject(workingDir, createFn) {
224
+ if (fs.existsSync(workingDir)) {
225
+ throw new Error(`Directory ${workingDir} already exists!`);
226
+ }
227
+
228
+ await createFn();
229
+ }
230
+
231
+ async createProjectFiles(
232
+ projectName,
233
+ technology,
234
+ companyName,
235
+ isCreatingMonorepo,
236
+ monorepoInfo,
237
+ projectPath,
238
+ workingDir
239
+ ) {
240
+ const isMonorepoApp = monorepoInfo.isMonorepo || isCreatingMonorepo;
241
+
242
+ if (isCreatingMonorepo) {
243
+ console.log('🏗️ Creating monorepo structure...');
244
+ await createMonorepoStructure(companyName, projectPath, technology);
245
+ }
246
+
247
+ await this.addTechnologyDependencies(monorepoInfo, technology);
248
+ await this.createAppStructure(
249
+ projectName,
250
+ technology,
251
+ companyName,
252
+ isMonorepoApp,
253
+ workingDir
254
+ );
255
+ }
256
+
257
+ async addTechnologyDependencies(monorepoInfo, technology) {
258
+ if (!monorepoInfo.isMonorepo) return;
259
+
260
+ console.log(`📦 Adding ${technology} dependencies to monorepo...`);
261
+
262
+ if (technology === 'react') {
263
+ await addReactDependenciesToMonorepo(monorepoInfo.rootPath);
264
+ } else if (technology === 'angular') {
265
+ await addAngularDependenciesToMonorepo(monorepoInfo.rootPath);
266
+ }
267
+ }
268
+
269
+ async createAppStructure(
270
+ projectName,
271
+ technology,
272
+ companyName,
273
+ isMonorepoApp,
274
+ workingDir
275
+ ) {
276
+ // Create app directory and switch to it
277
+ fs.mkdirSync(workingDir, { recursive: true });
278
+ process.chdir(workingDir);
279
+
280
+ console.log('📁 Creating project structure...');
281
+ await createProjectStructure(projectName, technology);
282
+
283
+ console.log('📦 Creating package.json...');
284
+ await createPackageJson(
285
+ projectName,
286
+ isMonorepoApp ? companyName : null,
287
+ technology
288
+ );
289
+
290
+ console.log('⚙️ Creating configuration files...');
291
+ await createConfigFiles(
292
+ projectName,
293
+ isMonorepoApp,
294
+ technology,
295
+ companyName
296
+ );
297
+
298
+ console.log('📂 Creating source files...');
299
+ await createSourceFiles(
300
+ projectName,
301
+ isMonorepoApp ? companyName : null,
302
+ technology
303
+ );
304
+
305
+ console.log('🗂️ Creating mag_assets structure...');
306
+ await createMagAssetsStructure('src/shared/mag_assets');
307
+
308
+ console.log('📋 Creating mag-cli configuration...');
309
+ await createMagCliConfig();
310
+
311
+ console.log('📱 Creating PWA files...');
312
+ await createPwaFiles(projectName);
313
+ }
314
+
315
+ async setupDependenciesAndGit(
316
+ isCreatingMonorepo,
317
+ monorepoInfo,
318
+ projectPath,
319
+ workingDir
320
+ ) {
321
+ console.log('🔧 Initializing Git repository...');
322
+
323
+ if (isCreatingMonorepo) {
324
+ await this.setupNewMonorepo(projectPath);
325
+ } else if (monorepoInfo.isMonorepo) {
326
+ await this.setupExistingMonorepo(monorepoInfo.rootPath);
327
+ } else {
328
+ await this.setupStandaloneApp(workingDir);
329
+ }
330
+
331
+ // Add app-specific scripts to monorepo
332
+ if (isCreatingMonorepo || monorepoInfo.isMonorepo) {
333
+ const rootPath = isCreatingMonorepo ? projectPath : monorepoInfo.rootPath;
334
+ console.log('📝 Adding app-specific scripts to root package.json...');
335
+ await ScriptManager.addAppScriptsToMonorepo(projectName, rootPath);
336
+ }
337
+ }
338
+
339
+ async setupNewMonorepo(projectPath) {
340
+ console.log(`Changing to monorepo root: ${projectPath}`);
341
+ process.chdir(projectPath);
342
+ execSync('git init', { stdio: 'inherit' });
343
+
344
+ console.log('📥 Installing dependencies...');
345
+ execSync('npm install', { stdio: 'inherit' });
346
+ }
347
+
348
+ async setupExistingMonorepo(rootPath) {
349
+ console.log('📥 Installing dependencies...');
350
+ console.log(`Changing to monorepo root: ${rootPath}`);
351
+ process.chdir(rootPath);
352
+ execSync('npm install', { stdio: 'inherit' });
353
+ }
354
+
355
+ async setupStandaloneApp(workingDir) {
356
+ console.log(`Changing to app directory: ${workingDir}`);
357
+ process.chdir(workingDir);
358
+ execSync('git init', { stdio: 'inherit' });
359
+
360
+ console.log('📥 Installing dependencies...');
361
+ execSync('npm install', { stdio: 'inherit' });
362
+ }
363
+
364
+ displaySuccessMessage(
365
+ projectName,
366
+ companyName,
367
+ isCreatingMonorepo,
368
+ monorepoInfo
369
+ ) {
370
+ const projectDisplayName = isCreatingMonorepo
371
+ ? `${companyName}-monorepo with ${projectName} app`
372
+ : `${projectName} app in monorepo`;
373
+
374
+ console.log(`✅ ${projectDisplayName} created successfully!`);
375
+ console.log(`\n📝 Next steps:`);
376
+
377
+ this.showNavigationInstructions(
378
+ companyName,
379
+ projectName,
380
+ isCreatingMonorepo,
381
+ monorepoInfo
382
+ );
383
+ this.showUsageInstructions(isCreatingMonorepo);
384
+ }
385
+
386
+ showNavigationInstructions(
387
+ companyName,
388
+ projectName,
389
+ isCreatingMonorepo,
390
+ monorepoInfo
391
+ ) {
392
+ if (isCreatingMonorepo) {
393
+ console.log(` cd ${companyName}-monorepo`);
394
+ console.log(` cd apps/${projectName}`);
395
+ } else if (monorepoInfo.isMonorepo) {
396
+ console.log(` cd apps/${projectName}`);
397
+ } else {
398
+ console.log(` cd ${projectName}`);
399
+ }
400
+ console.log(` npm start`);
401
+ }
402
+
403
+ showUsageInstructions(isCreatingMonorepo) {
404
+ console.log(` \n💡 PWA Commands:`);
405
+ console.log(` 💡 Use 'npm run build' to build the PWA`);
406
+ console.log(` 💡 Use 'npm run serve:pwa' to test PWA features`);
407
+ console.log(` \n🔧 Asset Management:`);
408
+ console.log(` 💡 Use 'mag-cli assets' to compile assets`);
409
+ console.log(` 💡 Use 'mag-cli assets --watch' for development`);
410
+ console.log(` 💡 Use 'mag-cli build' for full build`);
411
+ console.log(` 💡 Use 'mag-cli zip' to create deployment zip`);
412
+ console.log(
413
+ ` 💡 Edit files in src/shared/mag_assets/ and run assets to compile`
414
+ );
415
+
416
+ if (isCreatingMonorepo) {
417
+ console.log(` \n🏗️ Monorepo Commands:`);
418
+ console.log(
419
+ ` 💡 Add new apps with 'mag-cli init <app-name>' from monorepo root`
420
+ );
421
+ console.log(
422
+ ` 💡 Each app has its own mag_assets and federation config`
423
+ );
424
+ console.log(` 💡 Shared dependencies are managed at monorepo level`);
425
+ }
426
+ }
427
+
428
+ cleanup() {
429
+ process.chdir(this.originalCwd);
430
+ }
431
+ }
432
+
433
+ class ScriptManager {
434
+ static async addAppScriptsToMonorepo(projectName, rootPath) {
435
+ const packageJsonPath = path.join(rootPath, 'package.json');
436
+
437
+ if (!fs.existsSync(packageJsonPath)) {
438
+ console.log('❌ Root package.json not found, skipping script addition');
439
+ return;
440
+ }
441
+
442
+ try {
443
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
444
+
445
+ const newScripts = this.createAppScripts(projectName);
446
+ packageJson.scripts = { ...packageJson.scripts, ...newScripts };
447
+
448
+ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
449
+ console.log(
450
+ `✅ Added app-specific scripts for ${projectName} to root package.json`
451
+ );
452
+ } catch (error) {
453
+ console.error(
454
+ `❌ Error adding app scripts to monorepo: ${error.message}`
455
+ );
456
+ }
457
+ }
458
+
459
+ static createAppScripts(projectName) {
460
+ return {
461
+ [`start:${projectName}`]: `npm run start --workspace=apps/${projectName}`,
462
+ [`build:${projectName}`]: `npm run build --workspace=apps/${projectName}`,
463
+ [`test:${projectName}`]: `npm run test --workspace=apps/${projectName}`,
464
+ [`lint:${projectName}`]: `npm run lint --workspace=apps/${projectName}`,
465
+ [`assets:${projectName}`]: `npm run assets --workspace=apps/${projectName}`,
466
+ };
467
+ }
468
+ }
469
+
470
+ class TechnologyValidator {
471
+ static async getTechnology(providedTechnology) {
472
+ if (providedTechnology) {
473
+ this.validate(providedTechnology);
474
+ return providedTechnology;
475
+ }
476
+
477
+ const answers = await inquirer.prompt([
478
+ {
479
+ type: 'list',
480
+ name: 'technology',
481
+ message: 'Which technology framework would you like to use?',
482
+ choices: [
483
+ {
484
+ name: 'Angular (TypeScript, Standalone Components)',
485
+ value: 'angular',
486
+ },
487
+ {
488
+ name: 'React (TypeScript, Functional Components)',
489
+ value: 'react',
490
+ },
491
+ ],
492
+ default: 'angular',
493
+ },
494
+ ]);
495
+
496
+ return answers.technology;
497
+ }
498
+
499
+ static validate(technology) {
500
+ if (!SUPPORTED_TECHNOLOGIES.includes(technology)) {
501
+ throw new Error(
502
+ `Invalid technology: ${technology}. Use 'angular' or 'react'`
503
+ );
504
+ }
505
+ }
506
+ }
507
+
508
+ // Main Command Functions
509
+ function initCommand(program) {
510
+ // Full project initialization (more specific, so it should be first)
511
+ program
512
+ .command('init <project-name>')
513
+ .description('Initialize a new PWA micro frontend project or monorepo')
514
+ .option('-p, --path <path>', 'Project path', '.')
515
+ .option('-c, --company <company>', 'Company name for monorepo')
516
+ .option('-t, --tech <technology>', 'Technology framework (angular|react)')
517
+ .action(handleProjectInit);
518
+
519
+ // Basic folder structure initialization (more general, so it should be last)
520
+ program
521
+ .command('init')
522
+ .description(
523
+ 'Initialize basic mag_assets folder structure in current directory'
524
+ )
525
+ .option('-c, --config <path>', 'Config file path', './mag-cli.config.js')
526
+ .action(handleBasicInit);
527
+ }
528
+
529
+ async function handleBasicInit(options) {
530
+ console.log('🗂️ Creating basic mag_assets folder structure...');
531
+
532
+ const pathsConfig = await loadConfigOrDefault(options.config);
533
+ await MagAssetsManager.createStructure(pathsConfig);
534
+
535
+ displayBasicInitSuccess(pathsConfig);
536
+ }
537
+
538
+ async function handleProjectInit(projectName, options) {
539
+ const setupManager = new ProjectSetupManager();
540
+
541
+ try {
542
+ console.log(`🚀 Creating new PWA micro frontend project: ${projectName}`);
543
+
544
+ const technology = await TechnologyValidator.getTechnology(options.tech);
545
+ console.log(
546
+ `🎯 Creating ${technology.charAt(0).toUpperCase() + technology.slice(1)} project...`
547
+ );
548
+
549
+ const monorepoInfo = MonorepoDetector.detect();
550
+
551
+ await setupManager.setupProject(
552
+ projectName,
553
+ options,
554
+ technology,
555
+ monorepoInfo
556
+ );
557
+ } catch (error) {
558
+ console.error(`❌ Error creating project: ${error.message}`);
559
+ process.exit(1);
560
+ } finally {
561
+ setupManager.cleanup();
562
+ }
563
+ }
564
+
565
+ async function loadConfigOrDefault(configPath) {
566
+ try {
567
+ const config = require(path.resolve(configPath));
568
+ return config.paths || DEFAULT_PATHS_CONFIG;
569
+ } catch {
570
+ console.log(`No config file found at ${configPath}, using default paths`);
571
+ return DEFAULT_PATHS_CONFIG;
572
+ }
573
+ }
574
+
575
+ function displayBasicInitSuccess(pathsConfig) {
576
+ if (Array.isArray(pathsConfig)) {
577
+ console.log(
578
+ `✅ Basic mag_assets structures created for ${pathsConfig.length} projects`
579
+ );
580
+ console.log(`💡 Run 'mag-cli assets' to compile assets from all projects`);
581
+ } else {
582
+ console.log(
583
+ `✅ Basic mag_assets structure created in ${pathsConfig.source}/`
584
+ );
585
+ console.log(
586
+ `💡 Run 'mag-cli assets' to compile assets to ${pathsConfig.output}/`
587
+ );
588
+ }
589
+ }
590
+
591
+ module.exports = initCommand;