@azure-net/cli 2.3.62 → 4.0.0-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/.prettierrc +7 -0
  2. package/bin/azure-net.js +247 -157
  3. package/bin/azure-net.ts +253 -0
  4. package/eslint.config.js +19 -0
  5. package/package.json +12 -5
  6. package/src/commands/generate/generateCrudBase.js +95 -114
  7. package/src/commands/generate/generateCrudPresenter.js +135 -142
  8. package/src/commands/generate/generateModule.js +21 -21
  9. package/src/commands/generate/generateModuleBase.js +92 -114
  10. package/src/commands/generate/generateModulePresenter.js +73 -80
  11. package/src/commands/generate/generatePresenter.js +38 -44
  12. package/src/commands/generate/generateRepository.js +41 -47
  13. package/src/commands/generate/generateSchema.js +74 -70
  14. package/src/commands/generate/generateService.js +32 -37
  15. package/src/commands/generate/index.js +0 -5
  16. package/src/generate/layers/generateRepository.js +124 -0
  17. package/src/generate/layers/generateRepositoryMethod.js +102 -0
  18. package/src/generate/layers/generateUseCases.js +50 -0
  19. package/src/generate/layers/repositoryShared.js +569 -0
  20. package/src/generate/module/generateDomain.js +146 -0
  21. package/src/generate/module/generateDomain.ts +147 -0
  22. package/src/generate/module/generateModule.js +84 -0
  23. package/src/generate/module/generateModule.ts +84 -0
  24. package/src/generate/module/generatePresenter.js +419 -0
  25. package/src/generate/module/generatePresenter.ts +442 -0
  26. package/src/generate/module/generateRepository.js +144 -0
  27. package/src/generate/module/generateRepository.ts +158 -0
  28. package/src/generate/module/generateSchemaFromType.js +320 -0
  29. package/src/generate/module/generateSchemaFromType.ts +325 -0
  30. package/src/generate/module/generateTypesJson.js +252 -0
  31. package/src/generate/module/generateTypesJson.ts +259 -0
  32. package/src/generate/module/generateUseCases.js +67 -0
  33. package/src/generate/module/generateUseCases.ts +67 -0
  34. package/src/generate/module/repositoryModuleShared.js +598 -0
  35. package/src/generate/ui/generateComponent.js +98 -0
  36. package/src/generate/ui/generateComponent.ts +100 -0
  37. package/src/generate/ui/generateDesignComponent.js +71 -0
  38. package/src/generate/ui/generateDesignComponent.ts +71 -0
  39. package/src/generate/ui/generateWidget.js +71 -0
  40. package/src/generate/ui/generateWidget.ts +71 -0
  41. package/src/init/initAliases.js +159 -0
  42. package/src/init/initAliases.ts +167 -0
  43. package/src/init/initEdges.js +74 -0
  44. package/src/init/initEdges.ts +74 -0
  45. package/src/init/initStructure.js +122 -0
  46. package/src/init/initStructure.ts +124 -0
  47. package/src/install/installFresh.js +101 -0
  48. package/src/install/installFresh.ts +101 -0
  49. package/src/plugins/addLint.js +257 -0
  50. package/src/plugins/addLint.ts +274 -0
  51. package/src/plugins/index.js +1 -0
  52. package/src/plugins/index.ts +1 -0
  53. package/src/scaffolds/createCoreMiddleware.js +27 -0
  54. package/src/scaffolds/createCoreMiddleware.ts +27 -0
  55. package/src/scaffolds/createCoreSchema.js +141 -0
  56. package/src/scaffolds/createCoreSchema.ts +142 -0
  57. package/src/scaffolds/createDatasource.js +173 -0
  58. package/src/scaffolds/createDatasource.ts +175 -0
  59. package/src/scaffolds/createDatasourceProvider.js +164 -0
  60. package/src/scaffolds/createDatasourceProvider.ts +175 -0
  61. package/src/scaffolds/createMiddleware.js +105 -0
  62. package/src/scaffolds/createMiddleware.ts +105 -0
  63. package/src/scaffolds/createPresenterFactory.js +112 -0
  64. package/src/scaffolds/createPresenterFactory.ts +114 -0
  65. package/src/scaffolds/createResponse.js +64 -0
  66. package/src/scaffolds/createResponse.ts +64 -0
  67. package/src/scaffolds/createSchemaRule.js +40 -0
  68. package/src/scaffolds/createSchemaRule.ts +42 -0
  69. package/src/scaffolds/createTranslationManager.js +78 -0
  70. package/src/scaffolds/createTranslationManager.ts +78 -0
  71. package/src/scaffolds/index.js +9 -0
  72. package/src/scaffolds/index.ts +9 -0
  73. package/src/utils/contextUtils.js +56 -49
  74. package/src/utils/fileUtils.js +72 -84
  75. package/src/utils/loadConfig.js +57 -13
  76. package/tsconfig.json +19 -0
  77. package/src/commands/generate/generateComponent.js +0 -53
  78. package/src/commands/generate/generateDatasource.js +0 -84
  79. package/src/commands/generate/generateDesignComponent.js +0 -53
  80. package/src/commands/generate/generateResponse.js +0 -41
  81. package/src/commands/generate/generateWidget.js +0 -53
  82. package/src/commands/init/index.js +0 -9
  83. package/src/commands/init/initAliases.js +0 -52
  84. package/src/commands/init/initEdges.js +0 -42
  85. package/src/commands/init/initLint.js +0 -138
  86. package/src/commands/init/initMiddleware.js +0 -21
  87. package/src/commands/init/initPresenter.js +0 -25
  88. package/src/commands/init/initSchema.js +0 -22
  89. package/src/commands/init/initStructure.js +0 -140
  90. package/src/commands/init/initTranslations.js +0 -59
  91. package/src/commands/init/migrateStructure.js +0 -144
@@ -0,0 +1,158 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import prompts from 'prompts';
4
+ import {
5
+ buildRepositoryContent,
6
+ createUseCasesForRepository,
7
+ ensureRepositoryName,
8
+ getConfigState,
9
+ getDomainTypes,
10
+ getRepositoryMeta,
11
+ promptMethodsBatch,
12
+ resolveContextAlias,
13
+ resolveRepositoriesPath,
14
+ selectContext,
15
+ selectDatasource,
16
+ selectDomain,
17
+ writeRepositoryInterface
18
+ } from './repositoryModuleShared.js';
19
+ import { updateIndexTs, writeIfNotExists } from '../../utils/fileUtils.js';
20
+
21
+ type GenerateRepositoryOptions = {
22
+ contextName?: string;
23
+ domainName?: string;
24
+ forceCreateUseCases?: boolean;
25
+ };
26
+
27
+ type GenerateRepositoryResult = {
28
+ contextName: string;
29
+ domainName: string;
30
+ repositoryName: string;
31
+ useCasesName: string;
32
+ useCasesCreated: boolean;
33
+ };
34
+
35
+ export default async function generateRepository(options: GenerateRepositoryOptions = {}): Promise<GenerateRepositoryResult | null> {
36
+ let contextName: string | null = options.contextName ? String(options.contextName) : null;
37
+ if (!contextName) {
38
+ contextName = await selectContext('Select context for repository:');
39
+ }
40
+ if (!contextName) {
41
+ process.exitCode = 1;
42
+ return null;
43
+ }
44
+
45
+ let domainName: string | null = options.domainName ? String(options.domainName) : null;
46
+ if (!domainName) {
47
+ domainName = await selectDomain(contextName);
48
+ }
49
+ if (!domainName) {
50
+ process.exitCode = 1;
51
+ return null;
52
+ }
53
+
54
+ const { repositoryNameRaw } = await prompts({
55
+ type: 'text',
56
+ name: 'repositoryNameRaw',
57
+ message: 'Repository name:',
58
+ validate: (value: string) => (String(value ?? '').trim().length > 0 ? true : 'Repository name is required')
59
+ });
60
+
61
+ const className = ensureRepositoryName(repositoryNameRaw);
62
+ const meta = getRepositoryMeta(className);
63
+
64
+ const datasource = await selectDatasource(contextName);
65
+ if (!datasource) {
66
+ process.exitCode = 1;
67
+ return null;
68
+ }
69
+
70
+ const domainTypes = await getDomainTypes(contextName, domainName);
71
+ const { methodsCountRaw } = await prompts({
72
+ type: 'number',
73
+ name: 'methodsCountRaw',
74
+ message: 'How many methods to create?',
75
+ initial: 1,
76
+ min: 0
77
+ });
78
+
79
+ const methodsCount = Number(methodsCountRaw ?? 0);
80
+ if (!Number.isFinite(methodsCount) || methodsCount < 0) {
81
+ console.error('❌ Invalid methods count.');
82
+ process.exitCode = 1;
83
+ return null;
84
+ }
85
+
86
+ const methods = await promptMethodsBatch(domainTypes, methodsCount);
87
+ if (!methods) {
88
+ process.exitCode = 1;
89
+ return null;
90
+ }
91
+
92
+ const { contexts } = await getConfigState();
93
+ const contextAlias = resolveContextAlias(contexts, contextName);
94
+ const repositoriesPath = resolveRepositoriesPath(contextName);
95
+ await fs.mkdir(repositoriesPath, { recursive: true });
96
+ const repositoryPath = path.join(repositoriesPath, `${meta.className}.ts`);
97
+
98
+ await writeRepositoryInterface({
99
+ contextName,
100
+ domainName,
101
+ meta,
102
+ methods
103
+ });
104
+
105
+ const repositoryContent = buildRepositoryContent({
106
+ meta,
107
+ contextAlias,
108
+ domainName,
109
+ datasource,
110
+ methods
111
+ });
112
+
113
+ const created = await writeIfNotExists(repositoryPath, repositoryContent);
114
+ await updateIndexTs(repositoriesPath);
115
+
116
+ if (!created) {
117
+ console.warn(`⚠️ Repository "${meta.className}" already exists. File was not overwritten.`);
118
+ process.exitCode = 1;
119
+ return null;
120
+ }
121
+
122
+ const { shouldCreateUseCases } = await prompts({
123
+ type: 'confirm',
124
+ name: 'shouldCreateUseCases',
125
+ message: 'Create UseCases for this repository?',
126
+ initial: true
127
+ });
128
+
129
+ let useCasesCreated = false;
130
+ if (shouldCreateUseCases) {
131
+ await createUseCasesForRepository({
132
+ contextName,
133
+ contextAlias,
134
+ domainName,
135
+ repositoryName: meta.className
136
+ });
137
+ useCasesCreated = true;
138
+ }
139
+
140
+ if (!shouldCreateUseCases && options.forceCreateUseCases) {
141
+ await createUseCasesForRepository({
142
+ contextName,
143
+ contextAlias,
144
+ domainName,
145
+ repositoryName: meta.className
146
+ });
147
+ useCasesCreated = true;
148
+ }
149
+
150
+ console.log(`✅ Repository generated: ${repositoryPath}`);
151
+ return {
152
+ contextName,
153
+ domainName,
154
+ repositoryName: meta.className,
155
+ useCasesName: meta.useCasesClassName,
156
+ useCasesCreated
157
+ };
158
+ }
@@ -0,0 +1,320 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import prompts from 'prompts';
4
+ import { getConfigState, resolveContextAlias, selectContext } from './repositoryModuleShared.js';
5
+
6
+ const listDirectories = async (targetPath) => {
7
+ try {
8
+ const items = await fs.readdir(targetPath, { withFileTypes: true });
9
+ return items.filter((item) => item.isDirectory()).map((item) => item.name);
10
+ } catch {
11
+ return [];
12
+ }
13
+ };
14
+
15
+ const listSchemaFiles = async (targetPath) => {
16
+ try {
17
+ const files = await fs.readdir(targetPath);
18
+ return files.filter((item) => item.endsWith('.ts') && item !== 'index.ts');
19
+ } catch {
20
+ return [];
21
+ }
22
+ };
23
+
24
+ const resolveAliasMap = async () => {
25
+ const { contexts, coreAlias } = await getConfigState();
26
+ const map = new Map();
27
+ map.set(coreAlias, path.join(process.cwd(), 'src', 'core'));
28
+
29
+ for (const context of contexts) {
30
+ const alias = resolveContextAlias(contexts, context.name);
31
+ map.set(alias, path.join(process.cwd(), 'src', 'app', context.name));
32
+ }
33
+
34
+ return map;
35
+ };
36
+
37
+ const resolveImportToFile = async (fromFile, importPath, aliasMap) => {
38
+ const rawImport = String(importPath ?? '').trim();
39
+ if (!rawImport.length) return null;
40
+
41
+ let absolute = '';
42
+ if (rawImport.startsWith('.')) {
43
+ absolute = path.resolve(path.dirname(fromFile), rawImport);
44
+ } else {
45
+ const alias = Array.from(aliasMap.keys()).find((key) => rawImport === key || rawImport.startsWith(`${key}/`));
46
+ if (!alias) return null;
47
+ const suffix = rawImport === alias ? '' : rawImport.slice(alias.length + 1);
48
+ absolute = path.join(aliasMap.get(alias), suffix);
49
+ }
50
+
51
+ const tsCandidate = absolute.endsWith('.ts') ? absolute : `${absolute}.ts`;
52
+ try {
53
+ await fs.access(tsCandidate);
54
+ return tsCandidate;
55
+ } catch {
56
+ const indexCandidate = path.join(absolute, 'index.ts');
57
+ try {
58
+ await fs.access(indexCandidate);
59
+ return indexCandidate;
60
+ } catch {
61
+ return null;
62
+ }
63
+ }
64
+ };
65
+
66
+ const extractSchemaTypeName = (content) => {
67
+ const typeMatch = content.match(/=\s*[A-Za-z0-9_]+<\s*([^>]+?)\s*>\s*\(\)/m);
68
+ if (!typeMatch) return null;
69
+ return String(typeMatch[1]).trim();
70
+ };
71
+
72
+ const findTypeImportPath = (content, typeName) => {
73
+ const importRegex = /import\s+type\s+\{([^}]+)\}\s+from\s+['"]([^'"]+)['"]/g;
74
+ let match;
75
+ while ((match = importRegex.exec(content)) !== null) {
76
+ const imported = match[1]
77
+ .split(',')
78
+ .map((item) => item.trim())
79
+ .filter(Boolean);
80
+ if (imported.includes(typeName)) {
81
+ return match[2];
82
+ }
83
+ }
84
+ return null;
85
+ };
86
+
87
+ const extractInterfaceBody = (content, interfaceName) => {
88
+ const declarationRegex = new RegExp(`export\\s+interface\\s+${interfaceName}\\s*\\{`);
89
+ const declarationMatch = content.match(declarationRegex);
90
+ if (!declarationMatch || declarationMatch.index === undefined) return null;
91
+
92
+ const openBraceIndex = content.indexOf('{', declarationMatch.index);
93
+ if (openBraceIndex === -1) return null;
94
+
95
+ let depth = 0;
96
+ let closeBraceIndex = -1;
97
+ for (let index = openBraceIndex; index < content.length; index += 1) {
98
+ const char = content[index];
99
+ if (char === '{') depth += 1;
100
+ if (char === '}') {
101
+ depth -= 1;
102
+ if (depth === 0) {
103
+ closeBraceIndex = index;
104
+ break;
105
+ }
106
+ }
107
+ }
108
+
109
+ if (closeBraceIndex === -1) return null;
110
+ return content.slice(openBraceIndex + 1, closeBraceIndex);
111
+ };
112
+
113
+ const resolveRelativeExportPath = async (fromFile, exportPath) => {
114
+ const absolute = path.resolve(path.dirname(fromFile), exportPath);
115
+ const tsCandidate = absolute.endsWith('.ts') ? absolute : `${absolute}.ts`;
116
+ try {
117
+ await fs.access(tsCandidate);
118
+ return tsCandidate;
119
+ } catch {
120
+ const indexCandidate = path.join(absolute, 'index.ts');
121
+ try {
122
+ await fs.access(indexCandidate);
123
+ return indexCandidate;
124
+ } catch {
125
+ return null;
126
+ }
127
+ }
128
+ };
129
+
130
+ const findInterfaceBodyByExportGraph = async (entryFilePath, interfaceName, visited = new Set()) => {
131
+ const resolvedPath = path.resolve(entryFilePath);
132
+ if (visited.has(resolvedPath)) return null;
133
+ visited.add(resolvedPath);
134
+
135
+ let content = '';
136
+ try {
137
+ content = await fs.readFile(resolvedPath, 'utf-8');
138
+ } catch {
139
+ return null;
140
+ }
141
+
142
+ const direct = extractInterfaceBody(content, interfaceName);
143
+ if (direct) return direct;
144
+
145
+ const exportRegex = /export\\s+(?:\\*|\\{[^}]+\\})\\s+from\\s+['\"]([^'\"]+)['\"]/g;
146
+ let match;
147
+ while ((match = exportRegex.exec(content)) !== null) {
148
+ const exportPath = match[1];
149
+ if (!exportPath || !exportPath.startsWith('.')) continue;
150
+ // eslint-disable-next-line no-await-in-loop
151
+ const nextPath = await resolveRelativeExportPath(resolvedPath, exportPath);
152
+ if (!nextPath) continue;
153
+ // eslint-disable-next-line no-await-in-loop
154
+ const nested = await findInterfaceBodyByExportGraph(nextPath, interfaceName, visited);
155
+ if (nested) return nested;
156
+ }
157
+
158
+ return null;
159
+ };
160
+
161
+ const findInterfaceBodyByScan = async (rootDir, interfaceName) => {
162
+ const queue = [rootDir];
163
+ while (queue.length) {
164
+ const current = queue.shift();
165
+ if (!current) continue;
166
+ let entries = [];
167
+ try {
168
+ entries = await fs.readdir(current, { withFileTypes: true });
169
+ } catch {
170
+ continue;
171
+ }
172
+
173
+ for (const entry of entries) {
174
+ const absolute = path.join(current, entry.name);
175
+ if (entry.isDirectory()) {
176
+ queue.push(absolute);
177
+ continue;
178
+ }
179
+ if (!entry.isFile() || !entry.name.endsWith('.ts')) continue;
180
+ // eslint-disable-next-line no-await-in-loop
181
+ const content = await fs.readFile(absolute, 'utf-8');
182
+ const body = extractInterfaceBody(content, interfaceName);
183
+ if (body) return body;
184
+ }
185
+ }
186
+ return null;
187
+ };
188
+
189
+ const extractKeys = (body) => {
190
+ const keys = [];
191
+ const keyRegex = /^\s*([A-Za-z_$][A-Za-z0-9_$]*)\??\s*:/gm;
192
+ let match;
193
+ while ((match = keyRegex.exec(body)) !== null) {
194
+ keys.push(match[1]);
195
+ }
196
+ return Array.from(new Set(keys));
197
+ };
198
+
199
+ const updateRulesObject = (schemaContent, keys) => {
200
+ const rulesRegex = /\.rules\(\(rules\)\s*=>\s*\(\{([\s\S]*?)\}\)\)/m;
201
+ const match = schemaContent.match(rulesRegex);
202
+ if (!match) return null;
203
+
204
+ const currentBody = match[1] ?? '';
205
+ const existingKeys = new Set(extractKeys(currentBody));
206
+ const newLines = keys.filter((key) => !existingKeys.has(key)).map((key) => `\t\t${key}: [],`);
207
+
208
+ if (!newLines.length) return schemaContent;
209
+
210
+ const trimmedBody = currentBody.trimEnd();
211
+ const nextBody = trimmedBody.length ? `${trimmedBody}\n${newLines.join('\n')}\n\t` : `\n${newLines.join('\n')}\n\t`;
212
+
213
+ return schemaContent.replace(rulesRegex, `.rules((rules) => ({${nextBody}}))`);
214
+ };
215
+
216
+ export default async function generateSchemaFromType() {
217
+ const contextName = await selectContext('Select context for schema update:');
218
+ if (!contextName) {
219
+ process.exitCode = 1;
220
+ return;
221
+ }
222
+
223
+ const presentationPath = path.join(process.cwd(), 'src', 'app', contextName, 'layers', 'presentation');
224
+ const presenterFolders = await listDirectories(presentationPath);
225
+ if (!presenterFolders.length) {
226
+ console.error(`❌ No presenter folders found in ${presentationPath}`);
227
+ process.exitCode = 1;
228
+ return;
229
+ }
230
+
231
+ const { presenterFolder } = await prompts({
232
+ type: 'select',
233
+ name: 'presenterFolder',
234
+ message: 'Select presenter folder:',
235
+ choices: presenterFolders.map((item) => ({ title: item, value: item })),
236
+ initial: 0
237
+ });
238
+
239
+ if (!presenterFolder) {
240
+ process.exitCode = 1;
241
+ return;
242
+ }
243
+
244
+ const schemaFolderPath = path.join(presentationPath, String(presenterFolder), 'schema');
245
+ const schemas = await listSchemaFiles(schemaFolderPath);
246
+ if (!schemas.length) {
247
+ console.error(`❌ No schemas found in ${schemaFolderPath}`);
248
+ process.exitCode = 1;
249
+ return;
250
+ }
251
+
252
+ const { schemaFile } = await prompts({
253
+ type: 'select',
254
+ name: 'schemaFile',
255
+ message: 'Select schema file:',
256
+ choices: schemas.map((item) => ({ title: item, value: item })),
257
+ initial: 0
258
+ });
259
+
260
+ if (!schemaFile) {
261
+ process.exitCode = 1;
262
+ return;
263
+ }
264
+
265
+ const schemaPath = path.join(schemaFolderPath, String(schemaFile));
266
+ const schemaContent = await fs.readFile(schemaPath, 'utf-8');
267
+
268
+ const typeName = extractSchemaTypeName(schemaContent);
269
+ if (!typeName) {
270
+ console.error('❌ Unable to resolve schema generic type.');
271
+ process.exitCode = 1;
272
+ return;
273
+ }
274
+
275
+ const importPath = findTypeImportPath(schemaContent, typeName);
276
+ if (!importPath) {
277
+ console.error(`❌ Unable to find import for type "${typeName}".`);
278
+ process.exitCode = 1;
279
+ return;
280
+ }
281
+
282
+ const aliasMap = await resolveAliasMap();
283
+ const typeFilePath = await resolveImportToFile(schemaPath, importPath, aliasMap);
284
+ if (!typeFilePath) {
285
+ console.error(`❌ Unable to resolve type file path from import "${importPath}".`);
286
+ process.exitCode = 1;
287
+ return;
288
+ }
289
+
290
+ let interfaceBody = await findInterfaceBodyByExportGraph(typeFilePath, typeName);
291
+ if (!interfaceBody) {
292
+ interfaceBody = await findInterfaceBodyByScan(path.dirname(typeFilePath), typeName);
293
+ }
294
+ if (!interfaceBody) {
295
+ console.error(`❌ Interface "${typeName}" not found in ${typeFilePath}`);
296
+ process.exitCode = 1;
297
+ return;
298
+ }
299
+
300
+ const interfaceKeys = extractKeys(interfaceBody);
301
+ if (!interfaceKeys.length) {
302
+ console.log('ℹ️ Interface has no explicit keys to transfer into rules.');
303
+ return;
304
+ }
305
+
306
+ const updatedSchemaContent = updateRulesObject(schemaContent, interfaceKeys);
307
+ if (!updatedSchemaContent) {
308
+ console.error('❌ Could not locate `.rules((rules) => ({}))` block in schema file.');
309
+ process.exitCode = 1;
310
+ return;
311
+ }
312
+
313
+ if (updatedSchemaContent === schemaContent) {
314
+ console.log('ℹ️ Rules already include all keys from type.');
315
+ return;
316
+ }
317
+
318
+ await fs.writeFile(schemaPath, updatedSchemaContent, 'utf-8');
319
+ console.log(`✅ Schema updated from type: ${schemaPath}`);
320
+ }