@azure-net/cli 4.0.0-0 ā 4.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 +73 -34
- package/bin/azure-net.ts +81 -34
- package/eslint.config.js +15 -0
- package/package.json +1 -1
- package/src/checks/checkDomain.js +201 -0
- package/src/checks/checkDomain.ts +223 -0
- package/src/checks/checkFoldersStructure.js +70 -0
- package/src/checks/checkFoldersStructure.ts +85 -0
- package/src/checks/checkImports.ts +166 -0
- package/src/checks/checkInternal.js +37 -0
- package/src/checks/checkInternal.ts +44 -0
- package/src/checks/checkLayerBoundaries.js +178 -0
- package/src/checks/checkLayerBoundaries.ts +216 -0
- package/src/checks/checkPresenterNames.js +202 -0
- package/src/checks/checkPresenterNames.ts +208 -0
- package/src/checks/checkProject.js +42 -0
- package/src/checks/checkProject.ts +45 -0
- package/src/checks/checkProviderGraph.js +221 -0
- package/src/checks/checkProviderGraph.ts +234 -0
- package/src/checks/checkProviderNames.js +158 -0
- package/src/checks/checkProviderNames.ts +165 -0
- package/src/checks/index.js +8 -0
- package/src/checks/index.ts +9 -0
- package/src/generate/layers/generateRepository.ts +123 -0
- package/src/generate/layers/generateRepositoryMethod.ts +102 -0
- package/src/generate/layers/generateUseCases.ts +50 -0
- package/src/generate/layers/repositoryShared.ts +657 -0
- package/src/generate/module/generateModulePreset.js +307 -0
- package/src/generate/module/generateModulePreset.ts +337 -0
- package/src/generate/module/generateSchemaFromType.js +46 -8
- package/src/generate/module/generateSchemaFromType.ts +52 -8
- package/src/generate/module/repositoryModuleShared.ts +665 -0
- package/src/utils/contextUtils.ts +89 -0
- package/src/utils/fileUtils.ts +100 -0
- package/src/utils/loadConfig.ts +69 -0
- package/src/commands/generate/generateCrudBase.js +0 -188
- package/src/commands/generate/generateCrudPresenter.js +0 -225
- package/src/commands/generate/generateModule.js +0 -31
- package/src/commands/generate/generateModuleBase.js +0 -130
- package/src/commands/generate/generateModulePresenter.js +0 -103
- package/src/commands/generate/generatePresenter.js +0 -80
- package/src/commands/generate/generateRepository.js +0 -70
- package/src/commands/generate/generateSchema.js +0 -100
- package/src/commands/generate/generateService.js +0 -66
- package/src/commands/generate/index.js +0 -9
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
type ProviderNode = {
|
|
5
|
+
name: string;
|
|
6
|
+
file: string;
|
|
7
|
+
line: number;
|
|
8
|
+
dependsOn: string[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type GraphIssue = {
|
|
12
|
+
file: string;
|
|
13
|
+
line: number;
|
|
14
|
+
message: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const TARGET_EXTENSIONS = new Set(['.ts', '.js']);
|
|
18
|
+
const IGNORED_DIRS = new Set(['node_modules', 'dist', '.svelte-kit', '.git']);
|
|
19
|
+
|
|
20
|
+
const toPosixPath = (value: string): string => value.split(path.sep).join('/');
|
|
21
|
+
|
|
22
|
+
const getLineByIndex = (content: string, index: number): number => {
|
|
23
|
+
let line = 1;
|
|
24
|
+
for (let i = 0; i < index; i += 1) {
|
|
25
|
+
if (content[i] === '\n') line += 1;
|
|
26
|
+
}
|
|
27
|
+
return line;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const listSourceFiles = async (rootDir: string): Promise<string[]> => {
|
|
31
|
+
const files: string[] = [];
|
|
32
|
+
const stack: string[] = [rootDir];
|
|
33
|
+
|
|
34
|
+
while (stack.length) {
|
|
35
|
+
const current = stack.pop();
|
|
36
|
+
if (!current) continue;
|
|
37
|
+
|
|
38
|
+
let entries: import('node:fs').Dirent[] = [];
|
|
39
|
+
try {
|
|
40
|
+
entries = await fs.readdir(current, { withFileTypes: true });
|
|
41
|
+
} catch {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const entry of entries) {
|
|
46
|
+
if (entry.isDirectory()) {
|
|
47
|
+
if (!IGNORED_DIRS.has(entry.name)) stack.push(path.join(current, entry.name));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!entry.isFile()) continue;
|
|
52
|
+
const filePath = path.join(current, entry.name);
|
|
53
|
+
if (!TARGET_EXTENSIONS.has(path.extname(filePath))) continue;
|
|
54
|
+
files.push(filePath);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return files;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const findMatchingBracket = (content: string, startIndex: number, openChar: string, closeChar: string): number => {
|
|
62
|
+
let depth = 0;
|
|
63
|
+
for (let i = startIndex; i < content.length; i += 1) {
|
|
64
|
+
const ch = content[i];
|
|
65
|
+
if (ch === openChar) depth += 1;
|
|
66
|
+
if (ch === closeChar) {
|
|
67
|
+
depth -= 1;
|
|
68
|
+
if (depth === 0) return i;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return -1;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const extractDependsOn = (configBlock: string): string[] => {
|
|
75
|
+
const dependsOnMatch = configBlock.match(/dependsOn\s*:\s*\{([\s\S]*?)\}/m);
|
|
76
|
+
if (!dependsOnMatch?.[1]) return [];
|
|
77
|
+
|
|
78
|
+
const block = dependsOnMatch[1];
|
|
79
|
+
const deps = new Set<string>();
|
|
80
|
+
const keyRegex = /([A-Za-z_$][A-Za-z0-9_$]*)\s*:/g;
|
|
81
|
+
let match: RegExpExecArray | null;
|
|
82
|
+
while ((match = keyRegex.exec(block)) !== null) {
|
|
83
|
+
const key = match[1];
|
|
84
|
+
if (key) deps.add(key);
|
|
85
|
+
}
|
|
86
|
+
return [...deps];
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const parseProviderGraphFromFile = (content: string, filePath: string): ProviderNode[] => {
|
|
90
|
+
const nodes: ProviderNode[] = [];
|
|
91
|
+
const token = 'createBoundaryProvider(';
|
|
92
|
+
let fromIndex = 0;
|
|
93
|
+
|
|
94
|
+
while (fromIndex < content.length) {
|
|
95
|
+
const start = content.indexOf(token, fromIndex);
|
|
96
|
+
if (start === -1) break;
|
|
97
|
+
|
|
98
|
+
const openParenIndex = start + token.length - 1;
|
|
99
|
+
const closeParenIndex = findMatchingBracket(content, openParenIndex, '(', ')');
|
|
100
|
+
if (closeParenIndex === -1) break;
|
|
101
|
+
|
|
102
|
+
const fullCall = content.slice(openParenIndex + 1, closeParenIndex);
|
|
103
|
+
const idMatch = fullCall.match(/^\s*(['"`])([^'"`]+)\1\s*,/m);
|
|
104
|
+
const providerName = idMatch?.[2];
|
|
105
|
+
if (providerName) {
|
|
106
|
+
const objectStartInCall = fullCall.indexOf('{');
|
|
107
|
+
if (objectStartInCall !== -1) {
|
|
108
|
+
const absoluteObjectStart = openParenIndex + 1 + objectStartInCall;
|
|
109
|
+
const absoluteObjectEnd = findMatchingBracket(content, absoluteObjectStart, '{', '}');
|
|
110
|
+
const configBlock =
|
|
111
|
+
absoluteObjectEnd !== -1 ? content.slice(absoluteObjectStart, absoluteObjectEnd + 1) : fullCall.slice(objectStartInCall);
|
|
112
|
+
|
|
113
|
+
nodes.push({
|
|
114
|
+
name: providerName,
|
|
115
|
+
file: filePath,
|
|
116
|
+
line: getLineByIndex(content, start),
|
|
117
|
+
dependsOn: extractDependsOn(configBlock)
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
nodes.push({
|
|
121
|
+
name: providerName,
|
|
122
|
+
file: filePath,
|
|
123
|
+
line: getLineByIndex(content, start),
|
|
124
|
+
dependsOn: []
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
fromIndex = closeParenIndex + 1;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return nodes;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const findCycles = (graph: Map<string, string[]>): string[][] => {
|
|
136
|
+
const cycles: string[][] = [];
|
|
137
|
+
const visited = new Set<string>();
|
|
138
|
+
const inStack = new Set<string>();
|
|
139
|
+
const stack: string[] = [];
|
|
140
|
+
|
|
141
|
+
const dfs = (node: string): void => {
|
|
142
|
+
visited.add(node);
|
|
143
|
+
inStack.add(node);
|
|
144
|
+
stack.push(node);
|
|
145
|
+
|
|
146
|
+
const neighbors = graph.get(node) ?? [];
|
|
147
|
+
for (const next of neighbors) {
|
|
148
|
+
if (!graph.has(next)) continue;
|
|
149
|
+
if (!visited.has(next)) {
|
|
150
|
+
dfs(next);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (inStack.has(next)) {
|
|
154
|
+
const idx = stack.indexOf(next);
|
|
155
|
+
if (idx !== -1) cycles.push([...stack.slice(idx), next]);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
stack.pop();
|
|
160
|
+
inStack.delete(node);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
for (const node of graph.keys()) {
|
|
164
|
+
if (!visited.has(node)) dfs(node);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return cycles;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export default async function checkProviderGraph(): Promise<void> {
|
|
171
|
+
const srcPath = path.join(process.cwd(), 'src');
|
|
172
|
+
try {
|
|
173
|
+
await fs.access(srcPath);
|
|
174
|
+
} catch {
|
|
175
|
+
console.error('ā src folder not found in current workspace.');
|
|
176
|
+
process.exitCode = 1;
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const files = await listSourceFiles(srcPath);
|
|
181
|
+
const providers: ProviderNode[] = [];
|
|
182
|
+
|
|
183
|
+
for (const filePath of files) {
|
|
184
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
185
|
+
providers.push(...parseProviderGraphFromFile(content, filePath));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!providers.length) {
|
|
189
|
+
console.log('ā
No createBoundaryProvider declarations found.');
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const graph = new Map<string, string[]>();
|
|
194
|
+
const issues: GraphIssue[] = [];
|
|
195
|
+
const byName = new Map<string, ProviderNode>();
|
|
196
|
+
|
|
197
|
+
for (const provider of providers) {
|
|
198
|
+
if (byName.has(provider.name)) {
|
|
199
|
+
const existing = byName.get(provider.name)!;
|
|
200
|
+
issues.push({
|
|
201
|
+
file: provider.file,
|
|
202
|
+
line: provider.line,
|
|
203
|
+
message: `Provider graph ambiguity: duplicate provider id "${provider.name}" (first declared at ${toPosixPath(
|
|
204
|
+
path.relative(process.cwd(), existing.file)
|
|
205
|
+
)}:${existing.line})`
|
|
206
|
+
});
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
byName.set(provider.name, provider);
|
|
210
|
+
graph.set(provider.name, provider.dependsOn);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const cycles = findCycles(graph);
|
|
214
|
+
for (const cycle of cycles) {
|
|
215
|
+
const firstNode = byName.get(cycle[0] ?? '');
|
|
216
|
+
issues.push({
|
|
217
|
+
file: firstNode?.file ?? srcPath,
|
|
218
|
+
line: firstNode?.line ?? 1,
|
|
219
|
+
message: `Circular provider dependency detected: ${cycle.join(' -> ')}`
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (!issues.length) {
|
|
224
|
+
console.log(`ā
Provider graph check passed (${providers.length} providers).`);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
console.error('ā Provider graph check failed:');
|
|
229
|
+
for (const issue of issues) {
|
|
230
|
+
console.error(`⢠${toPosixPath(path.relative(process.cwd(), issue.file))}:${issue.line} ā ${issue.message}`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
process.exitCode = 1;
|
|
234
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const TARGET_EXTENSIONS = new Set(['.ts', '.js', '.svelte']);
|
|
5
|
+
const IGNORED_DIRS = new Set(['node_modules', 'dist', '.svelte-kit', '.git']);
|
|
6
|
+
const IMPORT_PATTERN = 'import\\s*\\{([^}]+)\\}\\s*from\\s*[\'"]([^\'"]+)[\'"]';
|
|
7
|
+
const CALL_PATTERN = '\\b([A-Za-z_$][A-Za-z0-9_$]*)\\s*\\(\\s*([\'"`])([^\'"`]*)\\2';
|
|
8
|
+
|
|
9
|
+
const toPosixPath = (value) => value.split(path.sep).join('/');
|
|
10
|
+
|
|
11
|
+
const parseImportAliases = (rawImports, importName) => {
|
|
12
|
+
const aliases = [];
|
|
13
|
+
for (const segment of rawImports.split(',')) {
|
|
14
|
+
const item = segment.trim();
|
|
15
|
+
if (!item.length) continue;
|
|
16
|
+
|
|
17
|
+
const aliasMatch = item.match(/^([A-Za-z_$][A-Za-z0-9_$]*)\s+as\s+([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
18
|
+
if (aliasMatch?.[1] === importName && aliasMatch[2]) {
|
|
19
|
+
aliases.push(aliasMatch[2]);
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (item === importName) {
|
|
24
|
+
aliases.push(importName);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return aliases;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const listSourceFiles = async (rootDir) => {
|
|
31
|
+
const files = [];
|
|
32
|
+
const stack = [rootDir];
|
|
33
|
+
|
|
34
|
+
while (stack.length) {
|
|
35
|
+
const current = stack.pop();
|
|
36
|
+
if (!current) continue;
|
|
37
|
+
|
|
38
|
+
let entries = [];
|
|
39
|
+
try {
|
|
40
|
+
entries = await fs.readdir(current, { withFileTypes: true });
|
|
41
|
+
} catch {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const entry of entries) {
|
|
46
|
+
if (entry.isDirectory()) {
|
|
47
|
+
if (!IGNORED_DIRS.has(entry.name)) {
|
|
48
|
+
stack.push(path.join(current, entry.name));
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!entry.isFile()) continue;
|
|
54
|
+
const filepath = path.join(current, entry.name);
|
|
55
|
+
if (!TARGET_EXTENSIONS.has(path.extname(filepath))) continue;
|
|
56
|
+
files.push(filepath);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return files;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const getLineByIndex = (content, index) => {
|
|
64
|
+
let line = 1;
|
|
65
|
+
for (let i = 0; i < index; i += 1) {
|
|
66
|
+
if (content[i] === '\n') line += 1;
|
|
67
|
+
}
|
|
68
|
+
return line;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const extractBoundaryFactoryAliases = (content) => {
|
|
72
|
+
const importRegex = new RegExp(IMPORT_PATTERN, 'g');
|
|
73
|
+
const aliases = new Set(['createBoundaryProvider']);
|
|
74
|
+
let match;
|
|
75
|
+
|
|
76
|
+
while ((match = importRegex.exec(content)) !== null) {
|
|
77
|
+
const rawImports = match[1];
|
|
78
|
+
if (!rawImports) continue;
|
|
79
|
+
for (const alias of parseImportAliases(rawImports, 'createBoundaryProvider')) {
|
|
80
|
+
aliases.add(alias);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return aliases;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const collectProviderOccurrences = (content, filepath) => {
|
|
88
|
+
const aliases = extractBoundaryFactoryAliases(content);
|
|
89
|
+
const callRegex = new RegExp(CALL_PATTERN, 'g');
|
|
90
|
+
const occurrences = [];
|
|
91
|
+
let match;
|
|
92
|
+
|
|
93
|
+
while ((match = callRegex.exec(content)) !== null) {
|
|
94
|
+
const callee = match[1];
|
|
95
|
+
const quote = match[2];
|
|
96
|
+
const providerId = match[3];
|
|
97
|
+
|
|
98
|
+
if (!callee || !providerId) continue;
|
|
99
|
+
if (!aliases.has(callee)) continue;
|
|
100
|
+
if (quote === '`' && providerId.includes('${')) continue;
|
|
101
|
+
|
|
102
|
+
occurrences.push({
|
|
103
|
+
id: providerId,
|
|
104
|
+
file: filepath,
|
|
105
|
+
line: getLineByIndex(content, match.index),
|
|
106
|
+
callee
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return occurrences;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default async function checkProviderNames() {
|
|
114
|
+
const srcPath = path.join(process.cwd(), 'src');
|
|
115
|
+
try {
|
|
116
|
+
await fs.access(srcPath);
|
|
117
|
+
} catch {
|
|
118
|
+
console.error('ā src folder not found in current workspace.');
|
|
119
|
+
process.exitCode = 1;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const files = await listSourceFiles(srcPath);
|
|
124
|
+
const occurrences = [];
|
|
125
|
+
|
|
126
|
+
for (const filepath of files) {
|
|
127
|
+
const content = await fs.readFile(filepath, 'utf-8');
|
|
128
|
+
occurrences.push(...collectProviderOccurrences(content, filepath));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!occurrences.length) {
|
|
132
|
+
console.log('ā
No providers declarations found.');
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const grouped = new Map();
|
|
137
|
+
for (const occurrence of occurrences) {
|
|
138
|
+
const list = grouped.get(occurrence.id) ?? [];
|
|
139
|
+
list.push(occurrence);
|
|
140
|
+
grouped.set(occurrence.id, list);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const duplicated = Array.from(grouped.entries()).filter(([, list]) => list.length > 1);
|
|
144
|
+
if (!duplicated.length) {
|
|
145
|
+
console.log(`ā
Provider names are unique (${occurrences.length} found).`);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
console.error('ā Duplicate provider names found:');
|
|
150
|
+
for (const [providerId, list] of duplicated) {
|
|
151
|
+
console.error(`\n- "${providerId}"`);
|
|
152
|
+
for (const item of list) {
|
|
153
|
+
console.error(` ⢠${toPosixPath(path.relative(process.cwd(), item.file))}:${item.line} (${item.callee})`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
process.exitCode = 1;
|
|
158
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
type ProviderOccurrence = {
|
|
5
|
+
id: string;
|
|
6
|
+
file: string;
|
|
7
|
+
line: number;
|
|
8
|
+
callee: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const TARGET_EXTENSIONS = new Set(['.ts', '.js', '.svelte']);
|
|
12
|
+
const IGNORED_DIRS = new Set(['node_modules', 'dist', '.svelte-kit', '.git']);
|
|
13
|
+
const IMPORT_PATTERN = 'import\\s*\\{([^}]+)\\}\\s*from\\s*[\'"]([^\'"]+)[\'"]';
|
|
14
|
+
const CALL_PATTERN = '\\b([A-Za-z_$][A-Za-z0-9_$]*)\\s*\\(\\s*([\'"`])([^\'"`]*)\\2';
|
|
15
|
+
|
|
16
|
+
const toPosixPath = (value: string): string => value.split(path.sep).join('/');
|
|
17
|
+
|
|
18
|
+
const parseImportAliases = (rawImports: string, importName: string): string[] => {
|
|
19
|
+
const aliases: string[] = [];
|
|
20
|
+
for (const segment of rawImports.split(',')) {
|
|
21
|
+
const item = segment.trim();
|
|
22
|
+
if (!item.length) continue;
|
|
23
|
+
|
|
24
|
+
const aliasMatch = item.match(/^([A-Za-z_$][A-Za-z0-9_$]*)\s+as\s+([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
25
|
+
if (aliasMatch?.[1] === importName && aliasMatch[2]) {
|
|
26
|
+
aliases.push(aliasMatch[2]);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (item === importName) {
|
|
31
|
+
aliases.push(importName);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return aliases;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const listSourceFiles = async (rootDir: string): Promise<string[]> => {
|
|
38
|
+
const files: string[] = [];
|
|
39
|
+
const stack: string[] = [rootDir];
|
|
40
|
+
|
|
41
|
+
while (stack.length) {
|
|
42
|
+
const current = stack.pop();
|
|
43
|
+
if (!current) continue;
|
|
44
|
+
|
|
45
|
+
let entries: import('node:fs').Dirent[] = [];
|
|
46
|
+
try {
|
|
47
|
+
entries = await fs.readdir(current, { withFileTypes: true });
|
|
48
|
+
} catch {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (const entry of entries) {
|
|
53
|
+
if (entry.isDirectory()) {
|
|
54
|
+
if (!IGNORED_DIRS.has(entry.name)) {
|
|
55
|
+
stack.push(path.join(current, entry.name));
|
|
56
|
+
}
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!entry.isFile()) continue;
|
|
61
|
+
const filepath = path.join(current, entry.name);
|
|
62
|
+
if (!TARGET_EXTENSIONS.has(path.extname(filepath))) continue;
|
|
63
|
+
files.push(filepath);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return files;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const getLineByIndex = (content: string, index: number): number => {
|
|
71
|
+
let line = 1;
|
|
72
|
+
for (let i = 0; i < index; i += 1) {
|
|
73
|
+
if (content[i] === '\n') line += 1;
|
|
74
|
+
}
|
|
75
|
+
return line;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const extractBoundaryFactoryAliases = (content: string): Set<string> => {
|
|
79
|
+
const importRegex = new RegExp(IMPORT_PATTERN, 'g');
|
|
80
|
+
const aliases = new Set<string>(['createBoundaryProvider']);
|
|
81
|
+
let match: RegExpExecArray | null;
|
|
82
|
+
|
|
83
|
+
while ((match = importRegex.exec(content)) !== null) {
|
|
84
|
+
const rawImports = match[1];
|
|
85
|
+
if (!rawImports) continue;
|
|
86
|
+
for (const alias of parseImportAliases(rawImports, 'createBoundaryProvider')) {
|
|
87
|
+
aliases.add(alias);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return aliases;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const collectProviderOccurrences = (content: string, filepath: string): ProviderOccurrence[] => {
|
|
95
|
+
const aliases = extractBoundaryFactoryAliases(content);
|
|
96
|
+
const callRegex = new RegExp(CALL_PATTERN, 'g');
|
|
97
|
+
const occurrences: ProviderOccurrence[] = [];
|
|
98
|
+
let match: RegExpExecArray | null;
|
|
99
|
+
|
|
100
|
+
while ((match = callRegex.exec(content)) !== null) {
|
|
101
|
+
const callee = match[1];
|
|
102
|
+
const quote = match[2];
|
|
103
|
+
const providerId = match[3];
|
|
104
|
+
|
|
105
|
+
if (!callee || !providerId) continue;
|
|
106
|
+
if (!aliases.has(callee)) continue;
|
|
107
|
+
if (quote === '`' && providerId.includes('${')) continue;
|
|
108
|
+
|
|
109
|
+
occurrences.push({
|
|
110
|
+
id: providerId,
|
|
111
|
+
file: filepath,
|
|
112
|
+
line: getLineByIndex(content, match.index),
|
|
113
|
+
callee
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return occurrences;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export default async function checkProviderNames(): Promise<void> {
|
|
121
|
+
const srcPath = path.join(process.cwd(), 'src');
|
|
122
|
+
try {
|
|
123
|
+
await fs.access(srcPath);
|
|
124
|
+
} catch {
|
|
125
|
+
console.error('ā src folder not found in current workspace.');
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const files = await listSourceFiles(srcPath);
|
|
131
|
+
const occurrences: ProviderOccurrence[] = [];
|
|
132
|
+
|
|
133
|
+
for (const filepath of files) {
|
|
134
|
+
const content = await fs.readFile(filepath, 'utf-8');
|
|
135
|
+
occurrences.push(...collectProviderOccurrences(content, filepath));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!occurrences.length) {
|
|
139
|
+
console.log('ā
No providers declarations found.');
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const grouped = new Map<string, ProviderOccurrence[]>();
|
|
144
|
+
for (const occurrence of occurrences) {
|
|
145
|
+
const list = grouped.get(occurrence.id) ?? [];
|
|
146
|
+
list.push(occurrence);
|
|
147
|
+
grouped.set(occurrence.id, list);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const duplicated = Array.from(grouped.entries()).filter(([, list]) => list.length > 1);
|
|
151
|
+
if (!duplicated.length) {
|
|
152
|
+
console.log(`ā
Provider names are unique (${occurrences.length} found).`);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
console.error('ā Duplicate provider names found:');
|
|
157
|
+
for (const [providerId, list] of duplicated) {
|
|
158
|
+
console.error(`\n- "${providerId}"`);
|
|
159
|
+
for (const item of list) {
|
|
160
|
+
console.error(` ⢠${toPosixPath(path.relative(process.cwd(), item.file))}:${item.line} (${item.callee})`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
process.exitCode = 1;
|
|
165
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { default as checkPresenterNames } from './checkPresenterNames.js';
|
|
2
|
+
export { default as checkProviderNames } from './checkProviderNames.js';
|
|
3
|
+
export { default as checkProviderGraph } from './checkProviderGraph.js';
|
|
4
|
+
export { default as checkDomain } from './checkDomain.js';
|
|
5
|
+
export { default as checkLayerBoundaries } from './checkLayerBoundaries.js';
|
|
6
|
+
export { default as checkFoldersStructure } from './checkFoldersStructure.js';
|
|
7
|
+
export { default as checkInternal } from './checkInternal.js';
|
|
8
|
+
export { default as checkProject } from './checkProject.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as checkPresenterNames } from './checkPresenterNames.js';
|
|
2
|
+
export { default as checkProviderNames } from './checkProviderNames.js';
|
|
3
|
+
export { default as checkProviderGraph } from './checkProviderGraph.js';
|
|
4
|
+
export { default as checkDomain } from './checkDomain.js';
|
|
5
|
+
export { default as checkImports } from './checkImports.js';
|
|
6
|
+
export { default as checkLayerBoundaries } from './checkLayerBoundaries.js';
|
|
7
|
+
export { default as checkFoldersStructure } from './checkFoldersStructure.js';
|
|
8
|
+
export { default as checkInternal } from './checkInternal.js';
|
|
9
|
+
export { default as checkProject } from './checkProject.js';
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import prompts from 'prompts';
|
|
4
|
+
import {
|
|
5
|
+
buildRepositoryContent,
|
|
6
|
+
ensureDomainType,
|
|
7
|
+
ensureRepositoryInterfaceFile,
|
|
8
|
+
ensureRepositoryName,
|
|
9
|
+
ensureUseCasesFile,
|
|
10
|
+
getConfigState,
|
|
11
|
+
getRepositoryMeta,
|
|
12
|
+
resolveContextAlias,
|
|
13
|
+
resolveRepositoriesPath,
|
|
14
|
+
selectContext,
|
|
15
|
+
selectDatasource,
|
|
16
|
+
promptMethodDefinition
|
|
17
|
+
} from './repositoryShared.js';
|
|
18
|
+
import { updateIndexTs, writeIfNotExists } from '../../utils/fileUtils.js';
|
|
19
|
+
|
|
20
|
+
export default async function generateRepository(): Promise<void> {
|
|
21
|
+
const { rawName } = await prompts({
|
|
22
|
+
type: 'text',
|
|
23
|
+
name: 'rawName',
|
|
24
|
+
message: 'Repository name:',
|
|
25
|
+
validate: (value) => (String(value ?? '').trim().length > 0 ? true : 'Repository name is required')
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const className = ensureRepositoryName(rawName);
|
|
29
|
+
const meta = getRepositoryMeta(className);
|
|
30
|
+
const contextName = await selectContext('Select context for repository:');
|
|
31
|
+
if (!contextName) {
|
|
32
|
+
process.exitCode = 1;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const datasource = await selectDatasource(contextName);
|
|
37
|
+
if (!datasource) {
|
|
38
|
+
process.exitCode = 1;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const { methodsCountRaw } = await prompts({
|
|
43
|
+
type: 'number',
|
|
44
|
+
name: 'methodsCountRaw',
|
|
45
|
+
message: 'How many methods to create?',
|
|
46
|
+
initial: 1,
|
|
47
|
+
min: 0
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const methodsCount = Number(methodsCountRaw ?? 0);
|
|
51
|
+
if (!Number.isFinite(methodsCount) || methodsCount < 0) {
|
|
52
|
+
console.error('ā Invalid methods count.');
|
|
53
|
+
process.exitCode = 1;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const methods: Array<Awaited<ReturnType<typeof promptMethodDefinition>>> = [];
|
|
58
|
+
for (let index = 0; index < methodsCount; index += 1) {
|
|
59
|
+
console.log(`\nš§© Method ${index + 1} of ${methodsCount}`);
|
|
60
|
+
const method = await promptMethodDefinition();
|
|
61
|
+
methods.push(method);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const { contexts } = await getConfigState();
|
|
65
|
+
const contextAlias = resolveContextAlias(contexts, contextName);
|
|
66
|
+
const repositoriesPath = resolveRepositoriesPath(contextName);
|
|
67
|
+
await fs.mkdir(repositoriesPath, { recursive: true });
|
|
68
|
+
const repositoryPath = path.join(repositoriesPath, `${meta.className}.ts`);
|
|
69
|
+
|
|
70
|
+
for (const method of methods) {
|
|
71
|
+
if (!method) continue;
|
|
72
|
+
const ensuredResponseType = await ensureDomainType({
|
|
73
|
+
contextName,
|
|
74
|
+
domainName: meta.domainName,
|
|
75
|
+
typeName: method.responseType,
|
|
76
|
+
layer: method.responseLayer
|
|
77
|
+
});
|
|
78
|
+
method.responseType = ensuredResponseType.interfaceName;
|
|
79
|
+
method.responseLayer = ensuredResponseType.layer;
|
|
80
|
+
|
|
81
|
+
if (method.requestType) {
|
|
82
|
+
const ensuredRequestType = await ensureDomainType({
|
|
83
|
+
contextName,
|
|
84
|
+
domainName: meta.domainName,
|
|
85
|
+
typeName: method.requestType,
|
|
86
|
+
layer: method.requestLayer ?? 'ports'
|
|
87
|
+
});
|
|
88
|
+
method.requestType = ensuredRequestType.interfaceName;
|
|
89
|
+
method.requestLayer = ensuredRequestType.layer;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const filteredMethods = methods.filter(Boolean);
|
|
94
|
+
await ensureRepositoryInterfaceFile({ contextName, meta, methods: filteredMethods });
|
|
95
|
+
const repositoryContent = buildRepositoryContent({
|
|
96
|
+
meta,
|
|
97
|
+
contextAlias,
|
|
98
|
+
datasourceImportPath: datasource.importPath,
|
|
99
|
+
datasourceName: datasource.name,
|
|
100
|
+
methods: filteredMethods
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const created = await writeIfNotExists(repositoryPath, repositoryContent);
|
|
104
|
+
await updateIndexTs(repositoriesPath);
|
|
105
|
+
if (!created) {
|
|
106
|
+
console.warn(`ā ļø Repository "${meta.className}" already exists. File was not overwritten.`);
|
|
107
|
+
process.exitCode = 1;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const { shouldCreateUseCases } = await prompts({
|
|
112
|
+
type: 'confirm',
|
|
113
|
+
name: 'shouldCreateUseCases',
|
|
114
|
+
message: 'Create UseCases for this repository?',
|
|
115
|
+
initial: true
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (shouldCreateUseCases) {
|
|
119
|
+
await ensureUseCasesFile({ contextName, contextAlias, meta });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
console.log(`ā
Repository generated: ${repositoryPath}`);
|
|
123
|
+
}
|