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