@azure-net/cli 5.0.1 → 5.1.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.
- package/bin/azure-net.js +7 -0
- package/bin/azure-net.ts +8 -0
- package/package.json +9 -2
- package/src/generate/module/generatePresenter.js +348 -360
- package/src/generate/routes/generateRoutes.js +217 -0
- package/src/generate/routes/generateRoutes.ts +282 -0
- package/src/scaffolds/createPresenterFactory.js +63 -65
- package/src/utils/loadConfig.ts +1 -0
|
@@ -8,395 +8,383 @@ import { loadUserConfig } from '../../utils/loadConfig.js';
|
|
|
8
8
|
import { getFoundationConstructImportPath, getFoundationConstructPath, getSharedState } from '../../utils/sharedFoundation.js';
|
|
9
9
|
const PRIMITIVE_TYPES = new Set(['string', 'number', 'boolean', 'unknown', 'void', 'null', 'undefined', 'bigint', 'symbol', 'never']);
|
|
10
10
|
const parseProviderInfo = (content) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return { providerName, registeredKeys: keys };
|
|
11
|
+
const providerNameMatch = content.match(/export\s+const\s+(\w+)\s*=\s*createBoundaryProvider/);
|
|
12
|
+
const providerName = providerNameMatch?.[1] ?? 'ApplicationProvider';
|
|
13
|
+
const registerMatch = content.match(/register\s*:\s*\([^)]*\)\s*=>\s*\(\{([\s\S]*?)\}\)/m);
|
|
14
|
+
const registerBody = registerMatch?.[1] ?? '';
|
|
15
|
+
const keys = [];
|
|
16
|
+
const keyRegex = /([A-Za-z_]\w*)\s*:/g;
|
|
17
|
+
let keyMatch;
|
|
18
|
+
while ((keyMatch = keyRegex.exec(registerBody)) !== null) {
|
|
19
|
+
const key = keyMatch[1];
|
|
20
|
+
if (key) keys.push(key);
|
|
21
|
+
}
|
|
22
|
+
return { providerName, registeredKeys: keys };
|
|
24
23
|
};
|
|
25
24
|
const parseUseCasesInfo = (content) => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return { repositoryInterfaceName, domainName, methods };
|
|
25
|
+
const interfaceImport = content.match(/import\s+type\s+\{\s*(\w+)\s*\}\s+from\s+['"]([^'"]+)['"]/);
|
|
26
|
+
const repositoryInterfaceName = interfaceImport?.[1] ?? null;
|
|
27
|
+
const domainImportPath = interfaceImport?.[2] ?? null;
|
|
28
|
+
const domainNameMatch = domainImportPath?.match(/\/layers\/domain\/([^'"]+)$/);
|
|
29
|
+
const domainName = domainNameMatch?.[1] ?? null;
|
|
30
|
+
const methods = [];
|
|
31
|
+
const declareRegex = /declare\s+(\w+)\s*:\s*\w+\['\w+'\];/g;
|
|
32
|
+
let declareMatch;
|
|
33
|
+
while ((declareMatch = declareRegex.exec(content)) !== null) {
|
|
34
|
+
const methodName = declareMatch[1];
|
|
35
|
+
if (methodName) methods.push(methodName);
|
|
36
|
+
}
|
|
37
|
+
return { repositoryInterfaceName, domainName, methods };
|
|
40
38
|
};
|
|
41
39
|
const parseRepositoryInterfaceMethods = (content) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return methods;
|
|
40
|
+
const methods = [];
|
|
41
|
+
const regex = /(\w+)\(([^)]*)\)\s*:\s*Promise<([^>]+)>;/g;
|
|
42
|
+
let match;
|
|
43
|
+
while ((match = regex.exec(content)) !== null) {
|
|
44
|
+
const methodName = match[1];
|
|
45
|
+
const paramsChunk = match[2];
|
|
46
|
+
const responseTypeChunk = match[3];
|
|
47
|
+
if (!methodName || paramsChunk === undefined || !responseTypeChunk) continue;
|
|
48
|
+
const paramsRaw = paramsChunk.trim();
|
|
49
|
+
const params = paramsRaw
|
|
50
|
+
? paramsRaw.split(',').map((part) => {
|
|
51
|
+
const [name, ...typeParts] = part.trim().split(':');
|
|
52
|
+
return { name: String(name ?? '').trim(), type: typeParts.join(':').trim() };
|
|
53
|
+
})
|
|
54
|
+
: [];
|
|
55
|
+
methods.push({
|
|
56
|
+
name: methodName,
|
|
57
|
+
params,
|
|
58
|
+
responseType: responseTypeChunk.trim()
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return methods;
|
|
65
62
|
};
|
|
66
63
|
const findRepositoryByInterface = async (contextName, repositoryInterfaceName) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
64
|
+
const repositoriesPath = path.join(process.cwd(), 'src', 'app', contextName, 'layers', 'infrastructure', 'http', 'repositories');
|
|
65
|
+
try {
|
|
66
|
+
const files = await fs.readdir(repositoriesPath);
|
|
67
|
+
for (const fileName of files) {
|
|
68
|
+
if (!fileName.endsWith('.ts') || fileName === 'index.ts') continue;
|
|
69
|
+
const absolute = path.join(repositoriesPath, fileName);
|
|
70
|
+
const content = await fs.readFile(absolute, 'utf-8');
|
|
71
|
+
if (content.includes(`implements ${repositoryInterfaceName}`)) {
|
|
72
|
+
return { filePath: absolute, content };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
84
79
|
};
|
|
85
80
|
const parseRepositoryHttpMethods = (content) => {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
return methods;
|
|
81
|
+
const methods = new Map();
|
|
82
|
+
const regex = /public\s+async\s+(\w+)\([^)]*\)\s*\{([\s\S]*?)\n\t\}/g;
|
|
83
|
+
let match;
|
|
84
|
+
while ((match = regex.exec(content)) !== null) {
|
|
85
|
+
const methodName = match[1];
|
|
86
|
+
if (!methodName) continue;
|
|
87
|
+
const body = match[2] ?? '';
|
|
88
|
+
const httpMatch = body.match(/http\.(get|post|put|delete|head|options|patch)\(/);
|
|
89
|
+
if (httpMatch?.[1]) methods.set(methodName, httpMatch[1]);
|
|
90
|
+
}
|
|
91
|
+
return methods;
|
|
99
92
|
};
|
|
100
93
|
const getSharedPresenterFactories = async () => {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
94
|
+
const { sharedContext } = await getSharedState();
|
|
95
|
+
const presenterPath = getFoundationConstructPath(sharedContext.name, 'presenter');
|
|
96
|
+
try {
|
|
97
|
+
const files = await fs.readdir(presenterPath);
|
|
98
|
+
return files.filter((item) => item.endsWith('.ts') && item !== 'index.ts').map((item) => item.replace(/\.ts$/, ''));
|
|
99
|
+
} catch {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
110
102
|
};
|
|
111
103
|
const getSharedSchemaFactories = async () => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
104
|
+
const { sharedContext } = await getSharedState();
|
|
105
|
+
const schemaPath = getFoundationConstructPath(sharedContext.name, 'schema');
|
|
106
|
+
try {
|
|
107
|
+
const files = await fs.readdir(schemaPath);
|
|
108
|
+
return files.filter((item) => item.endsWith('.ts') && item !== 'index.ts').map((item) => item.replace(/\.ts$/, ''));
|
|
109
|
+
} catch {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
121
112
|
};
|
|
122
113
|
const normalizePresenterName = (rawName, fallbackUseCasesName) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
114
|
+
const fallback = fallbackUseCasesName.replace(/UseCases$/, '') || 'Module';
|
|
115
|
+
const source = String(rawName ?? '').trim() || fallback;
|
|
116
|
+
const normalized = toPascalCase(source);
|
|
117
|
+
return normalized.endsWith('Presenter') ? normalized : `${normalized}Presenter`;
|
|
127
118
|
};
|
|
128
119
|
const isPrimitive = (typeName) => PRIMITIVE_TYPES.has(String(typeName ?? '').trim());
|
|
129
120
|
const normalizeTypeImports = (types) => Array.from(new Set(types.filter((typeName) => !isPrimitive(typeName))));
|
|
130
121
|
const isPathParamName = (name) => name === 'pathParam' || name === 'path';
|
|
131
122
|
const toTypeFileName = (typeName) => String(typeName ?? '').replace(/^I/, '') || 'Type';
|
|
132
123
|
const buildMethodInvocation = (method, useCasesName, schemaByMethod) => {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return `\tconst ${method.name} = async (${paramsSignature}) =>\n\t\tawait createAsyncAction${generic}(() => ${useCasesName}.${method.name}(${args.join(', ')}));`;
|
|
124
|
+
const paramsSignature = method.params.map((item) => `${item.name}: ${item.type}`).join(', ');
|
|
125
|
+
const argNames = method.params.map((item) => item.name);
|
|
126
|
+
const requestParam = method.params.find((item) => !isPathParamName(item.name));
|
|
127
|
+
const schemaInfo = schemaByMethod.get(method.name) ?? null;
|
|
128
|
+
const args = argNames.map((argName) => {
|
|
129
|
+
if (schemaInfo && requestParam && argName === requestParam.name) {
|
|
130
|
+
return `${schemaInfo.constName}.from(${argName}).json()`;
|
|
131
|
+
}
|
|
132
|
+
return argName;
|
|
133
|
+
});
|
|
134
|
+
const httpMethod = method.httpMethod;
|
|
135
|
+
if (httpMethod === 'get' || httpMethod === 'head' || httpMethod === 'options') {
|
|
136
|
+
return `\tconst ${method.name} = (${paramsSignature}) => createAsyncResource(() => ${useCasesName}.${method.name}(${args.join(', ')}));`;
|
|
137
|
+
}
|
|
138
|
+
const responseType = method.responseType;
|
|
139
|
+
const requestType = requestParam?.type ?? null;
|
|
140
|
+
let generic = `<${responseType}`;
|
|
141
|
+
if (requestType) generic += `, ${requestType}`;
|
|
142
|
+
generic += '>';
|
|
143
|
+
return `\tconst ${method.name} = async (${paramsSignature}) =>\n\t\tawait createAsyncAction${generic}(() => ${useCasesName}.${method.name}(${args.join(', ')}));`;
|
|
154
144
|
};
|
|
155
145
|
const getApplicationProviderFile = async (contextName) => {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
146
|
+
const providersPath = path.join(process.cwd(), 'src', 'app', contextName, 'layers', 'application', 'providers');
|
|
147
|
+
const targetPath = path.join(providersPath, 'ApplicationProvider.ts');
|
|
148
|
+
try {
|
|
149
|
+
await fs.access(targetPath);
|
|
150
|
+
return targetPath;
|
|
151
|
+
} catch {
|
|
152
|
+
const files = await fs.readdir(providersPath);
|
|
153
|
+
const candidate = files.find((item) => item.endsWith('.ts') && item !== 'index.ts');
|
|
154
|
+
return candidate ? path.join(providersPath, candidate) : null;
|
|
155
|
+
}
|
|
167
156
|
};
|
|
168
157
|
const resolveDomainInterfaceFile = async ({ contextName, domainName, interfaceName }) => {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
return null;
|
|
158
|
+
const portsPath = path.join(process.cwd(), 'src', 'app', contextName, 'layers', 'domain', domainName, 'ports');
|
|
159
|
+
const preferredPath = path.join(portsPath, `${toTypeFileName(interfaceName)}.ts`);
|
|
160
|
+
const legacyPath = path.join(portsPath, `${interfaceName}.ts`);
|
|
161
|
+
for (const candidate of [preferredPath, legacyPath]) {
|
|
162
|
+
try {
|
|
163
|
+
await fs.access(candidate);
|
|
164
|
+
return candidate;
|
|
165
|
+
} catch {
|
|
166
|
+
// Try the next candidate.
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
const files = await fs.readdir(portsPath);
|
|
171
|
+
for (const fileName of files) {
|
|
172
|
+
if (!fileName.endsWith('.ts') || fileName === 'index.ts') continue;
|
|
173
|
+
const candidate = path.join(portsPath, fileName);
|
|
174
|
+
const content = await fs.readFile(candidate, 'utf-8');
|
|
175
|
+
const escapedName = interfaceName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
176
|
+
if (new RegExp(`export\\s+interface\\s+${escapedName}\\b`).test(content)) {
|
|
177
|
+
return candidate;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
} catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
198
184
|
};
|
|
199
185
|
export default async function generatePresenter(options = {}) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
186
|
+
let contextName = options.contextName ? String(options.contextName) : null;
|
|
187
|
+
if (!contextName) {
|
|
188
|
+
contextName = await selectContext('Select context for presenter:');
|
|
189
|
+
}
|
|
190
|
+
if (!contextName) {
|
|
191
|
+
process.exitCode = 1;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const appProviderFile = await getApplicationProviderFile(contextName);
|
|
195
|
+
if (!appProviderFile) {
|
|
196
|
+
console.error('❌ ApplicationProvider file was not found.');
|
|
197
|
+
process.exitCode = 1;
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const appProviderContent = await fs.readFile(appProviderFile, 'utf-8');
|
|
201
|
+
const providerInfo = parseProviderInfo(appProviderContent);
|
|
202
|
+
const useCasesPath = path.join(process.cwd(), 'src', 'app', contextName, 'layers', 'application', 'use-cases');
|
|
203
|
+
let useCasesFiles = [];
|
|
204
|
+
try {
|
|
205
|
+
useCasesFiles = (await fs.readdir(useCasesPath))
|
|
206
|
+
.filter((item) => item.endsWith('.ts') && item !== 'index.ts')
|
|
207
|
+
.map((item) => item.replace(/\.ts$/, ''));
|
|
208
|
+
} catch {
|
|
209
|
+
// ignore
|
|
210
|
+
}
|
|
211
|
+
const registeredUseCases = useCasesFiles.filter((item) => providerInfo.registeredKeys.includes(item));
|
|
212
|
+
if (!registeredUseCases.length) {
|
|
213
|
+
console.error('❌ No registered UseCases were found in ApplicationProvider register().');
|
|
214
|
+
process.exitCode = 1;
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
let useCasesName = options.useCasesName ? String(options.useCasesName) : '';
|
|
218
|
+
if (!useCasesName) {
|
|
219
|
+
const { selectedUseCases } = await prompts({
|
|
220
|
+
type: 'select',
|
|
221
|
+
name: 'selectedUseCases',
|
|
222
|
+
message: 'Select UseCases:',
|
|
223
|
+
choices: registeredUseCases.map((item) => ({ title: item, value: item })),
|
|
224
|
+
initial: 0
|
|
225
|
+
});
|
|
226
|
+
useCasesName = String(selectedUseCases ?? '');
|
|
227
|
+
}
|
|
228
|
+
if (!registeredUseCases.includes(useCasesName)) {
|
|
229
|
+
console.error(`❌ UseCases "${useCasesName}" is not registered in ApplicationProvider.`);
|
|
230
|
+
process.exitCode = 1;
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const useCasesFile = path.join(useCasesPath, `${useCasesName}.ts`);
|
|
234
|
+
const useCasesContent = await fs.readFile(useCasesFile, 'utf-8');
|
|
235
|
+
const useCasesInfo = parseUseCasesInfo(useCasesContent);
|
|
236
|
+
if (!useCasesInfo.repositoryInterfaceName || !useCasesInfo.domainName) {
|
|
237
|
+
console.error('❌ Unable to parse repository interface import from UseCases.');
|
|
238
|
+
process.exitCode = 1;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const repositoryInfo = await findRepositoryByInterface(contextName, useCasesInfo.repositoryInterfaceName);
|
|
242
|
+
if (!repositoryInfo) {
|
|
243
|
+
console.error(`❌ Repository implementing ${useCasesInfo.repositoryInterfaceName} was not found.`);
|
|
244
|
+
process.exitCode = 1;
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const repositoryHttpMethods = parseRepositoryHttpMethods(repositoryInfo.content);
|
|
248
|
+
const domainPortsInterfacePath = await resolveDomainInterfaceFile({
|
|
249
|
+
contextName,
|
|
250
|
+
domainName: useCasesInfo.domainName,
|
|
251
|
+
interfaceName: useCasesInfo.repositoryInterfaceName
|
|
252
|
+
});
|
|
253
|
+
if (!domainPortsInterfacePath) {
|
|
254
|
+
console.error(
|
|
255
|
+
`❌ Repository interface "${useCasesInfo.repositoryInterfaceName}" was not found in ${path.join(process.cwd(), 'src', 'app', contextName, 'layers', 'domain', useCasesInfo.domainName, 'ports')}`
|
|
256
|
+
);
|
|
257
|
+
process.exitCode = 1;
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const repositoryInterfaceContent = await fs.readFile(domainPortsInterfacePath, 'utf-8');
|
|
261
|
+
const repositoryMethods = parseRepositoryInterfaceMethods(repositoryInterfaceContent).filter((method) =>
|
|
262
|
+
useCasesInfo.methods.includes(method.name)
|
|
263
|
+
);
|
|
264
|
+
const { presenterNameRaw } = await prompts({
|
|
265
|
+
type: 'text',
|
|
266
|
+
name: 'presenterNameRaw',
|
|
267
|
+
message: 'Presenter name (optional):'
|
|
268
|
+
});
|
|
269
|
+
const presenterName = normalizePresenterName(presenterNameRaw, useCasesName);
|
|
270
|
+
const presenterFolder = toKebabCase(presenterName.replace(/Presenter$/, ''));
|
|
271
|
+
const presentationPath = path.join(process.cwd(), 'src', 'app', contextName, 'layers', 'presentation', presenterFolder);
|
|
272
|
+
await fs.mkdir(presentationPath, { recursive: true });
|
|
273
|
+
const { contexts, sharedAlias } = await getConfigState();
|
|
274
|
+
const contextAlias = resolveContextAlias(contexts, contextName);
|
|
275
|
+
const presenterImportPath = getFoundationConstructImportPath(sharedAlias, 'presenter');
|
|
276
|
+
const schemaFactoryImportPath = getFoundationConstructImportPath(sharedAlias, 'schema');
|
|
277
|
+
let presenterFactory = String((await loadUserConfig()).defaultPresenterFactory ?? '').trim();
|
|
278
|
+
let useNativePresenter = false;
|
|
279
|
+
if (!presenterFactory) {
|
|
280
|
+
const availableFactories = await getSharedPresenterFactories();
|
|
281
|
+
const { selectedFactory } = await prompts({
|
|
282
|
+
type: 'select',
|
|
283
|
+
name: 'selectedFactory',
|
|
284
|
+
message: 'Select presenter factory:',
|
|
285
|
+
choices: [
|
|
286
|
+
...availableFactories.map((item) => ({ title: item, value: item })),
|
|
287
|
+
{ title: 'Native createPresenter', value: '__native__' }
|
|
288
|
+
],
|
|
289
|
+
initial: 0
|
|
290
|
+
});
|
|
291
|
+
if (selectedFactory === '__native__') {
|
|
292
|
+
useNativePresenter = true;
|
|
293
|
+
presenterFactory = 'createPresenter';
|
|
294
|
+
} else {
|
|
295
|
+
presenterFactory = String(selectedFactory ?? '');
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (!presenterFactory) {
|
|
299
|
+
useNativePresenter = true;
|
|
300
|
+
presenterFactory = 'createPresenter';
|
|
301
|
+
}
|
|
302
|
+
const mutationMethods = repositoryMethods.filter((method) => {
|
|
303
|
+
const httpMethod = repositoryHttpMethods.get(method.name) ?? 'get';
|
|
304
|
+
return httpMethod !== 'get' && httpMethod !== 'head' && httpMethod !== 'options';
|
|
305
|
+
});
|
|
306
|
+
let schemaFactory = String((await loadUserConfig()).defaultSchemaFactory ?? '').trim();
|
|
307
|
+
let schemaFactoryResolved = false;
|
|
308
|
+
const schemaByMethod = new Map();
|
|
309
|
+
const schemaImports = [];
|
|
310
|
+
let schemaIndexNeeded = false;
|
|
311
|
+
if (mutationMethods.length) {
|
|
312
|
+
for (const method of mutationMethods) {
|
|
313
|
+
const requestParam = method.params.find((item) => !isPathParamName(item.name));
|
|
314
|
+
if (!requestParam) continue;
|
|
315
|
+
const { shouldCreateSchema } = await prompts({
|
|
316
|
+
type: 'confirm',
|
|
317
|
+
name: 'shouldCreateSchema',
|
|
318
|
+
message: `Create schema for "${method.name}"?`,
|
|
319
|
+
initial: false
|
|
320
|
+
});
|
|
321
|
+
if (!shouldCreateSchema) continue;
|
|
322
|
+
if (!schemaFactoryResolved) {
|
|
323
|
+
if (!schemaFactory) {
|
|
324
|
+
const availableSchemas = await getSharedSchemaFactories();
|
|
325
|
+
if (!availableSchemas.length) {
|
|
326
|
+
console.error('❌ No schema factories found in shared foundation constructs/schema.');
|
|
327
|
+
process.exitCode = 1;
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const { selectedSchemaFactory } = await prompts({
|
|
331
|
+
type: 'select',
|
|
332
|
+
name: 'selectedSchemaFactory',
|
|
333
|
+
message: 'Select schema factory:',
|
|
334
|
+
choices: availableSchemas.map((item) => ({ title: item, value: item })),
|
|
335
|
+
initial: 0
|
|
336
|
+
});
|
|
337
|
+
schemaFactory = String(selectedSchemaFactory ?? '');
|
|
338
|
+
}
|
|
339
|
+
schemaFactoryResolved = true;
|
|
340
|
+
}
|
|
341
|
+
const schemaConstName = `${toPascalCase(method.name)}${presenterName}Schema`;
|
|
342
|
+
const schemaFileName = `${schemaConstName}.ts`;
|
|
343
|
+
const schemaPath = path.join(presentationPath, 'schema');
|
|
344
|
+
await fs.mkdir(schemaPath, { recursive: true });
|
|
345
|
+
schemaIndexNeeded = true;
|
|
346
|
+
const schemaContent = `import { ${schemaFactory} } from '${schemaFactoryImportPath}';
|
|
361
347
|
import type { ${requestParam.type} } from '${contextAlias}/layers/domain/${useCasesInfo.domainName}';
|
|
362
348
|
|
|
363
349
|
export const ${schemaConstName} = ${schemaFactory}<${requestParam.type}>()
|
|
364
350
|
\t.rules((rules) => ({}))
|
|
365
351
|
\t.create();
|
|
366
352
|
`;
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
353
|
+
await writeIfNotExists(path.join(schemaPath, schemaFileName), schemaContent);
|
|
354
|
+
schemaByMethod.set(method.name, { constName: schemaConstName });
|
|
355
|
+
schemaImports.push(schemaConstName);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (schemaIndexNeeded) {
|
|
359
|
+
await updateIndexTs(path.join(presentationPath, 'schema'));
|
|
360
|
+
}
|
|
361
|
+
const hasGetLike = repositoryMethods.some((method) => {
|
|
362
|
+
const httpMethod = repositoryHttpMethods.get(method.name) ?? 'get';
|
|
363
|
+
return httpMethod === 'get' || httpMethod === 'head' || httpMethod === 'options';
|
|
364
|
+
});
|
|
365
|
+
const hasMutation = repositoryMethods.some((method) => {
|
|
366
|
+
const httpMethod = repositoryHttpMethods.get(method.name) ?? 'get';
|
|
367
|
+
return httpMethod !== 'get' && httpMethod !== 'head' && httpMethod !== 'options';
|
|
368
|
+
});
|
|
369
|
+
const helperParams = [hasGetLike ? 'createAsyncResource' : null, hasMutation ? 'createAsyncAction' : null].filter(Boolean).join(', ');
|
|
370
|
+
const typeImports = normalizeTypeImports(
|
|
371
|
+
repositoryMethods.flatMap((method) => [method.responseType, ...method.params.map((item) => item.type)])
|
|
372
|
+
);
|
|
373
|
+
const methodsCode = repositoryMethods
|
|
374
|
+
.map((method) => {
|
|
375
|
+
const httpMethod = repositoryHttpMethods.get(method.name) ?? 'get';
|
|
376
|
+
return buildMethodInvocation({ ...method, httpMethod }, useCasesName, schemaByMethod);
|
|
377
|
+
})
|
|
378
|
+
.join('\n\n');
|
|
379
|
+
const factoryImport = useNativePresenter
|
|
380
|
+
? `import { createPresenter } from '@azure-net/kit/delivery';`
|
|
381
|
+
: `import { ${presenterFactory} } from '${presenterImportPath}';`;
|
|
382
|
+
const schemaImportLine = schemaImports.length ? `import { ${Array.from(new Set(schemaImports)).join(', ')} } from './schema';` : '';
|
|
383
|
+
const typeImportLine = typeImports.length
|
|
384
|
+
? `import type { ${typeImports.join(', ')} } from '${contextAlias}/layers/domain/${useCasesInfo.domainName}';`
|
|
385
|
+
: '';
|
|
386
|
+
const presenterId = `${toPascalCase(contextName)}${presenterName}`;
|
|
387
|
+
const presenterContent = `${factoryImport}
|
|
400
388
|
import { ${providerInfo.providerName} } from '${contextAlias}/layers/application/providers';
|
|
401
389
|
${typeImportLine}
|
|
402
390
|
${schemaImportLine}
|
|
@@ -408,7 +396,7 @@ ${methodsCode ? `${methodsCode}\n` : ''}
|
|
|
408
396
|
\treturn { ${repositoryMethods.map((method) => method.name).join(', ')} };
|
|
409
397
|
});
|
|
410
398
|
`;
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
399
|
+
await writeIfNotExists(path.join(presentationPath, `${presenterName}.ts`), presenterContent);
|
|
400
|
+
await fs.writeFile(path.join(presentationPath, 'index.ts'), `export * from './${presenterName}';\n`, 'utf-8');
|
|
401
|
+
console.log(`✅ Presenter generated: ${path.join(presentationPath, `${presenterName}.ts`)}`);
|
|
414
402
|
}
|