@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,178 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { loadUserConfig } from '../utils/loadConfig.js';
|
|
4
|
+
import { normalizeContexts } from '../init/initAliases.js';
|
|
5
|
+
|
|
6
|
+
const TARGET_EXTENSIONS = new Set(['.ts', '.js', '.svelte']);
|
|
7
|
+
const IGNORED_DIRS = new Set(['node_modules', 'dist', '.svelte-kit', '.git']);
|
|
8
|
+
|
|
9
|
+
const IMPORT_FROM_PATTERN = /\bimport\s+(?:type\s+)?[\s\S]*?\sfrom\s*['"]([^'"]+)['"]/g;
|
|
10
|
+
const EXPORT_FROM_PATTERN = /\bexport\s+(?:type\s+)?\{[\s\S]*?\}\sfrom\s*['"]([^'"]+)['"]/g;
|
|
11
|
+
const DYNAMIC_IMPORT_PATTERN = /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
12
|
+
|
|
13
|
+
const toPosixPath = (value) => value.split(path.sep).join('/');
|
|
14
|
+
|
|
15
|
+
const normalizeAlias = (value, fallback = '') => {
|
|
16
|
+
const raw = String(value ?? '').trim();
|
|
17
|
+
if (!raw.length) return fallback;
|
|
18
|
+
return raw.startsWith('$') ? raw : `$${raw}`;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const findLineByIndex = (content, index) => {
|
|
22
|
+
let line = 1;
|
|
23
|
+
for (let i = 0; i < index; i += 1) {
|
|
24
|
+
if (content[i] === '\n') line += 1;
|
|
25
|
+
}
|
|
26
|
+
return line;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const getRelativePath = (filePath) => toPosixPath(path.relative(process.cwd(), filePath));
|
|
30
|
+
|
|
31
|
+
const listFiles = async (rootDir) => {
|
|
32
|
+
const files = [];
|
|
33
|
+
const stack = [rootDir];
|
|
34
|
+
|
|
35
|
+
while (stack.length) {
|
|
36
|
+
const current = stack.pop();
|
|
37
|
+
if (!current) continue;
|
|
38
|
+
|
|
39
|
+
let entries = [];
|
|
40
|
+
try {
|
|
41
|
+
entries = await fs.readdir(current, { withFileTypes: true });
|
|
42
|
+
} catch {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (const entry of entries) {
|
|
47
|
+
if (entry.isDirectory()) {
|
|
48
|
+
if (!IGNORED_DIRS.has(entry.name)) {
|
|
49
|
+
stack.push(path.join(current, entry.name));
|
|
50
|
+
}
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!entry.isFile()) continue;
|
|
55
|
+
const filePath = path.join(current, entry.name);
|
|
56
|
+
if (!TARGET_EXTENSIONS.has(path.extname(filePath))) continue;
|
|
57
|
+
files.push(filePath);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return files;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const extractImports = (content) => {
|
|
65
|
+
const matches = [];
|
|
66
|
+
const patterns = [IMPORT_FROM_PATTERN, EXPORT_FROM_PATTERN, DYNAMIC_IMPORT_PATTERN];
|
|
67
|
+
|
|
68
|
+
for (const pattern of patterns) {
|
|
69
|
+
const regex = new RegExp(pattern.source, 'g');
|
|
70
|
+
let match;
|
|
71
|
+
while ((match = regex.exec(content)) !== null) {
|
|
72
|
+
const value = match[1];
|
|
73
|
+
if (!value) continue;
|
|
74
|
+
matches.push({ value, line: findLineByIndex(content, match.index) });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return matches;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const resolveAliasFromImport = (importPath, aliases) => {
|
|
82
|
+
const sorted = [...aliases].sort((a, b) => b.length - a.length);
|
|
83
|
+
for (const alias of sorted) {
|
|
84
|
+
if (importPath === alias || importPath.startsWith(`${alias}/`)) {
|
|
85
|
+
return alias;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const isSharedLikeContext = (contextName, alias) => {
|
|
92
|
+
const normalizedName = contextName.toLowerCase();
|
|
93
|
+
const normalizedAlias = alias.toLowerCase();
|
|
94
|
+
return normalizedName.includes('shared') || normalizedAlias.includes('shared');
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export default async function checkLayerBoundaries() {
|
|
98
|
+
const config = await loadUserConfig();
|
|
99
|
+
const contexts = normalizeContexts(config.contexts);
|
|
100
|
+
|
|
101
|
+
if (!contexts.length) {
|
|
102
|
+
console.error('❌ No contexts found in azure-net.config.ts/js. Fill `contexts` first.');
|
|
103
|
+
process.exitCode = 1;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const coreAlias = normalizeAlias(config.coreAlias, '$core');
|
|
108
|
+
const sharedAliasRaw = normalizeAlias(config.sharedAlias, '');
|
|
109
|
+
const sharedAlias = sharedAliasRaw.length ? sharedAliasRaw : null;
|
|
110
|
+
|
|
111
|
+
const aliasToContext = new Map();
|
|
112
|
+
for (const context of contexts) {
|
|
113
|
+
aliasToContext.set(context.alias, context);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const knownAliases = [coreAlias, ...contexts.map((context) => context.alias)];
|
|
117
|
+
if (sharedAlias && !knownAliases.includes(sharedAlias)) {
|
|
118
|
+
knownAliases.push(sharedAlias);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const issues = [];
|
|
122
|
+
|
|
123
|
+
for (const context of contexts) {
|
|
124
|
+
const layersRoot = path.join(process.cwd(), 'src', 'app', context.name, 'layers');
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
await fs.access(layersRoot);
|
|
128
|
+
} catch {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const files = await listFiles(layersRoot);
|
|
133
|
+
const allowedAliases = new Set([context.alias, coreAlias]);
|
|
134
|
+
if (sharedAlias) {
|
|
135
|
+
allowedAliases.add(sharedAlias);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const filePath of files) {
|
|
139
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
140
|
+
const imports = extractImports(content);
|
|
141
|
+
|
|
142
|
+
for (const entry of imports) {
|
|
143
|
+
const targetAlias = resolveAliasFromImport(entry.value, knownAliases);
|
|
144
|
+
if (!targetAlias) continue;
|
|
145
|
+
if (targetAlias === coreAlias) continue;
|
|
146
|
+
if (allowedAliases.has(targetAlias)) continue;
|
|
147
|
+
|
|
148
|
+
const targetContext = aliasToContext.get(targetAlias);
|
|
149
|
+
const baseMessage = targetContext
|
|
150
|
+
? `Layer boundary violation: context "${context.name}" cannot import from context "${targetContext.name}" (${targetAlias})`
|
|
151
|
+
: `Layer boundary violation: context "${context.name}" cannot import from "${targetAlias}"`;
|
|
152
|
+
|
|
153
|
+
const withSharedHint =
|
|
154
|
+
!sharedAlias && targetContext && isSharedLikeContext(targetContext.name, targetAlias)
|
|
155
|
+
? `${baseMessage}. sharedAlias is not configured in azure-net.config.ts/js; if this context is shared, set sharedAlias explicitly`
|
|
156
|
+
: baseMessage;
|
|
157
|
+
|
|
158
|
+
issues.push({
|
|
159
|
+
file: filePath,
|
|
160
|
+
line: entry.line,
|
|
161
|
+
message: withSharedHint
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!issues.length) {
|
|
168
|
+
console.log('✅ Layer boundaries check passed.');
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
console.error('❌ Layer boundaries check failed:');
|
|
173
|
+
for (const issue of issues) {
|
|
174
|
+
console.error(`• ${getRelativePath(issue.file)}:${issue.line} — ${issue.message}`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
process.exitCode = 1;
|
|
178
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { loadUserConfig } from '../utils/loadConfig.js';
|
|
4
|
+
import { normalizeContexts } from '../init/initAliases.js';
|
|
5
|
+
|
|
6
|
+
type BoundaryIssue = {
|
|
7
|
+
file: string;
|
|
8
|
+
line: number;
|
|
9
|
+
message: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
type ContextConfig = {
|
|
13
|
+
name: string;
|
|
14
|
+
alias: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const TARGET_EXTENSIONS = new Set(['.ts', '.js', '.svelte']);
|
|
18
|
+
const IGNORED_DIRS = new Set(['node_modules', 'dist', '.svelte-kit', '.git']);
|
|
19
|
+
|
|
20
|
+
const IMPORT_FROM_PATTERN = /\bimport\s+(?:type\s+)?[\s\S]*?\sfrom\s*['"]([^'"]+)['"]/g;
|
|
21
|
+
const EXPORT_FROM_PATTERN = /\bexport\s+(?:type\s+)?\{[\s\S]*?\}\sfrom\s*['"]([^'"]+)['"]/g;
|
|
22
|
+
const DYNAMIC_IMPORT_PATTERN = /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
23
|
+
|
|
24
|
+
const toPosixPath = (value: string): string => value.split(path.sep).join('/');
|
|
25
|
+
|
|
26
|
+
const normalizeAlias = (value: unknown, fallback = ''): string => {
|
|
27
|
+
const raw = String(value ?? '').trim();
|
|
28
|
+
if (!raw.length) return fallback;
|
|
29
|
+
return raw.startsWith('$') ? raw : `$${raw}`;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const findLineByIndex = (content: string, index: number): number => {
|
|
33
|
+
let line = 1;
|
|
34
|
+
for (let i = 0; i < index; i += 1) {
|
|
35
|
+
if (content[i] === '\n') line += 1;
|
|
36
|
+
}
|
|
37
|
+
return line;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const getRelativePath = (filePath: string): string => toPosixPath(path.relative(process.cwd(), filePath));
|
|
41
|
+
|
|
42
|
+
const listFiles = async (rootDir: string): Promise<string[]> => {
|
|
43
|
+
const files: string[] = [];
|
|
44
|
+
const stack: string[] = [rootDir];
|
|
45
|
+
|
|
46
|
+
while (stack.length) {
|
|
47
|
+
const current = stack.pop();
|
|
48
|
+
if (!current) continue;
|
|
49
|
+
|
|
50
|
+
let entries: import('node:fs').Dirent[] = [];
|
|
51
|
+
try {
|
|
52
|
+
entries = await fs.readdir(current, { withFileTypes: true });
|
|
53
|
+
} catch {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (const entry of entries) {
|
|
58
|
+
if (entry.isDirectory()) {
|
|
59
|
+
if (!IGNORED_DIRS.has(entry.name)) {
|
|
60
|
+
stack.push(path.join(current, entry.name));
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!entry.isFile()) continue;
|
|
66
|
+
const filePath = path.join(current, entry.name);
|
|
67
|
+
if (!TARGET_EXTENSIONS.has(path.extname(filePath))) continue;
|
|
68
|
+
files.push(filePath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return files;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const extractImports = (content: string): Array<{ value: string; line: number }> => {
|
|
76
|
+
const matches: Array<{ value: string; line: number }> = [];
|
|
77
|
+
const patterns = [IMPORT_FROM_PATTERN, EXPORT_FROM_PATTERN, DYNAMIC_IMPORT_PATTERN];
|
|
78
|
+
|
|
79
|
+
for (const pattern of patterns) {
|
|
80
|
+
const regex = new RegExp(pattern.source, 'g');
|
|
81
|
+
let match: RegExpExecArray | null;
|
|
82
|
+
while ((match = regex.exec(content)) !== null) {
|
|
83
|
+
const value = match[1];
|
|
84
|
+
if (!value) continue;
|
|
85
|
+
matches.push({ value, line: findLineByIndex(content, match.index) });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return matches;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const resolveAliasFromImport = (importPath: string, aliases: string[]): string | null => {
|
|
93
|
+
const sorted = [...aliases].sort((a, b) => b.length - a.length);
|
|
94
|
+
for (const alias of sorted) {
|
|
95
|
+
if (importPath === alias || importPath.startsWith(`${alias}/`)) {
|
|
96
|
+
return alias;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const isSharedLikeContext = (contextName: string, alias: string): boolean => {
|
|
103
|
+
const normalizedName = contextName.toLowerCase();
|
|
104
|
+
const normalizedAlias = alias.toLowerCase();
|
|
105
|
+
return normalizedName.includes('shared') || normalizedAlias.includes('shared');
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export default async function checkLayerBoundaries(): Promise<void> {
|
|
109
|
+
const config = await loadUserConfig();
|
|
110
|
+
const contexts = normalizeContexts(config.contexts) as ContextConfig[];
|
|
111
|
+
|
|
112
|
+
if (!contexts.length) {
|
|
113
|
+
console.error('❌ No contexts found in azure-net.config.ts/js. Fill `contexts` first.');
|
|
114
|
+
process.exitCode = 1;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const coreAlias = normalizeAlias(config.coreAlias, '$core');
|
|
119
|
+
const sharedAliasRaw = normalizeAlias(config.sharedAlias, '');
|
|
120
|
+
const sharedAlias = sharedAliasRaw.length ? sharedAliasRaw : null;
|
|
121
|
+
|
|
122
|
+
const aliasToContext = new Map<string, ContextConfig>();
|
|
123
|
+
for (const context of contexts) {
|
|
124
|
+
aliasToContext.set(context.alias, context);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const knownAliases = [coreAlias, ...contexts.map((context) => context.alias)];
|
|
128
|
+
if (sharedAlias && !knownAliases.includes(sharedAlias)) {
|
|
129
|
+
knownAliases.push(sharedAlias);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const issues: BoundaryIssue[] = [];
|
|
133
|
+
|
|
134
|
+
const coreRoot = path.join(process.cwd(), 'src', 'core');
|
|
135
|
+
try {
|
|
136
|
+
await fs.access(coreRoot);
|
|
137
|
+
const coreFiles = await listFiles(coreRoot);
|
|
138
|
+
for (const filePath of coreFiles) {
|
|
139
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
140
|
+
const imports = extractImports(content);
|
|
141
|
+
|
|
142
|
+
for (const entry of imports) {
|
|
143
|
+
const targetAlias = resolveAliasFromImport(entry.value, knownAliases);
|
|
144
|
+
if (!targetAlias || targetAlias === coreAlias) continue;
|
|
145
|
+
if (!aliasToContext.has(targetAlias)) continue;
|
|
146
|
+
|
|
147
|
+
const targetContext = aliasToContext.get(targetAlias);
|
|
148
|
+
issues.push({
|
|
149
|
+
file: filePath,
|
|
150
|
+
line: entry.line,
|
|
151
|
+
message: targetContext
|
|
152
|
+
? `Layer boundary violation: core cannot import from context "${targetContext.name}" (${targetAlias})`
|
|
153
|
+
: `Layer boundary violation: core cannot import from "${targetAlias}"`
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
} catch {
|
|
158
|
+
// Core folder is optional for the check.
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
for (const context of contexts) {
|
|
162
|
+
const contextRoot = path.join(process.cwd(), 'src', 'app', context.name);
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
await fs.access(contextRoot);
|
|
166
|
+
} catch {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const files = await listFiles(contextRoot);
|
|
171
|
+
const allowedAliases = new Set<string>([context.alias, coreAlias]);
|
|
172
|
+
if (sharedAlias && context.alias !== sharedAlias) {
|
|
173
|
+
allowedAliases.add(sharedAlias);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
for (const filePath of files) {
|
|
177
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
178
|
+
const imports = extractImports(content);
|
|
179
|
+
|
|
180
|
+
for (const entry of imports) {
|
|
181
|
+
const targetAlias = resolveAliasFromImport(entry.value, knownAliases);
|
|
182
|
+
if (!targetAlias) continue;
|
|
183
|
+
if (targetAlias === coreAlias) continue;
|
|
184
|
+
if (allowedAliases.has(targetAlias)) continue;
|
|
185
|
+
|
|
186
|
+
const targetContext = aliasToContext.get(targetAlias);
|
|
187
|
+
const baseMessage = targetContext
|
|
188
|
+
? `Layer boundary violation: context "${context.name}" cannot import from context "${targetContext.name}" (${targetAlias})`
|
|
189
|
+
: `Layer boundary violation: context "${context.name}" cannot import from "${targetAlias}"`;
|
|
190
|
+
|
|
191
|
+
const withSharedHint =
|
|
192
|
+
!sharedAlias && targetContext && isSharedLikeContext(targetContext.name, targetAlias)
|
|
193
|
+
? `${baseMessage}. sharedAlias is not configured in azure-net.config.ts/js; if this context is shared, set sharedAlias explicitly`
|
|
194
|
+
: baseMessage;
|
|
195
|
+
|
|
196
|
+
issues.push({
|
|
197
|
+
file: filePath,
|
|
198
|
+
line: entry.line,
|
|
199
|
+
message: withSharedHint
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!issues.length) {
|
|
206
|
+
console.log('✅ Layer boundaries check passed.');
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
console.error('❌ Layer boundaries check failed:');
|
|
211
|
+
for (const issue of issues) {
|
|
212
|
+
console.error(`• ${getRelativePath(issue.file)}:${issue.line} — ${issue.message}`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
process.exitCode = 1;
|
|
216
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
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
|
+
|
|
7
|
+
const CREATE_FACTORY_PATTERN = '(?:export\\s+)?(?:const|let|var)\\s+([A-Za-z_$][A-Za-z0-9_$]*)\\s*=\\s*createPresenterFactory\\s*\\(';
|
|
8
|
+
const IMPORT_FROM_PRESENTER_PATTERN = 'import\\s*\\{([^}]+)\\}\\s*from\\s*[\'"]([^\'"]+)[\'"]';
|
|
9
|
+
const PRESENTER_CALL_PATTERN = '\\b([A-Za-z_$][A-Za-z0-9_$]*)\\s*\\(\\s*([\'"`])([^\'"`]*)\\2';
|
|
10
|
+
|
|
11
|
+
const toPosixPath = (value) => value.split(path.sep).join('/');
|
|
12
|
+
|
|
13
|
+
const isPresenterImportPath = (importPath) => {
|
|
14
|
+
const normalized = importPath.replace(/\\/g, '/');
|
|
15
|
+
return /(^|\/)presenter(\/|$)/.test(normalized);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const parseImportLocals = (rawImports) => {
|
|
19
|
+
const locals = [];
|
|
20
|
+
for (const segment of rawImports.split(',')) {
|
|
21
|
+
const item = segment.trim();
|
|
22
|
+
if (!item.length) continue;
|
|
23
|
+
const aliasMatch = item.match(/^([A-Za-z_$][A-Za-z0-9_$]*)\s+as\s+([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
24
|
+
if (aliasMatch?.[2]) {
|
|
25
|
+
locals.push(aliasMatch[2]);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const directMatch = item.match(/^([A-Za-z_$][A-Za-z0-9_$]*)$/);
|
|
30
|
+
if (directMatch?.[1]) {
|
|
31
|
+
locals.push(directMatch[1]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return locals;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const listSourceFiles = async (rootDir) => {
|
|
38
|
+
const result = [];
|
|
39
|
+
const stack = [rootDir];
|
|
40
|
+
|
|
41
|
+
while (stack.length) {
|
|
42
|
+
const current = stack.pop();
|
|
43
|
+
if (!current) continue;
|
|
44
|
+
|
|
45
|
+
let entries = [];
|
|
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 fullPath = path.join(current, entry.name);
|
|
62
|
+
const extension = path.extname(fullPath);
|
|
63
|
+
if (!TARGET_EXTENSIONS.has(extension)) continue;
|
|
64
|
+
result.push(fullPath);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return result;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const getLineByIndex = (content, index) => {
|
|
72
|
+
let line = 1;
|
|
73
|
+
for (let i = 0; i < index; i += 1) {
|
|
74
|
+
if (content[i] === '\n') line += 1;
|
|
75
|
+
}
|
|
76
|
+
return line;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const extractFactoryNamesFromContent = (content) => {
|
|
80
|
+
const createFactoryRegex = new RegExp(CREATE_FACTORY_PATTERN, 'g');
|
|
81
|
+
const names = [];
|
|
82
|
+
let match;
|
|
83
|
+
while ((match = createFactoryRegex.exec(content)) !== null) {
|
|
84
|
+
const name = match[1];
|
|
85
|
+
if (name) names.push(name);
|
|
86
|
+
}
|
|
87
|
+
return names;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const extractImportedPresenterFactories = (content) => {
|
|
91
|
+
const importFromPresenterRegex = new RegExp(IMPORT_FROM_PRESENTER_PATTERN, 'g');
|
|
92
|
+
const names = [];
|
|
93
|
+
let match;
|
|
94
|
+
while ((match = importFromPresenterRegex.exec(content)) !== null) {
|
|
95
|
+
const rawImports = match[1];
|
|
96
|
+
const importPath = match[2];
|
|
97
|
+
if (!rawImports || !importPath) continue;
|
|
98
|
+
if (!isPresenterImportPath(importPath)) continue;
|
|
99
|
+
names.push(...parseImportLocals(rawImports));
|
|
100
|
+
}
|
|
101
|
+
return names;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const collectPresenterOccurrences = (content, filePath, knownFactories) => {
|
|
105
|
+
const presenterCallRegex = new RegExp(PRESENTER_CALL_PATTERN, 'g');
|
|
106
|
+
const occurrences = [];
|
|
107
|
+
const availableCallees = new Set(['createPresenter']);
|
|
108
|
+
|
|
109
|
+
for (const name of extractFactoryNamesFromContent(content)) {
|
|
110
|
+
availableCallees.add(name);
|
|
111
|
+
}
|
|
112
|
+
for (const name of extractImportedPresenterFactories(content)) {
|
|
113
|
+
availableCallees.add(name);
|
|
114
|
+
}
|
|
115
|
+
for (const name of knownFactories) {
|
|
116
|
+
availableCallees.add(name);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let match;
|
|
120
|
+
while ((match = presenterCallRegex.exec(content)) !== null) {
|
|
121
|
+
const callee = match[1];
|
|
122
|
+
const quote = match[2];
|
|
123
|
+
const presenterId = match[3];
|
|
124
|
+
if (!callee || !presenterId) continue;
|
|
125
|
+
if (!availableCallees.has(callee)) continue;
|
|
126
|
+
if (quote === '`' && presenterId.includes('${')) continue;
|
|
127
|
+
|
|
128
|
+
occurrences.push({
|
|
129
|
+
id: presenterId,
|
|
130
|
+
file: filePath,
|
|
131
|
+
line: getLineByIndex(content, match.index),
|
|
132
|
+
callee
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return occurrences;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export default async function checkPresenterNames() {
|
|
140
|
+
const srcPath = path.join(process.cwd(), 'src');
|
|
141
|
+
try {
|
|
142
|
+
await fs.access(srcPath);
|
|
143
|
+
} catch {
|
|
144
|
+
console.error('❌ src folder not found in current workspace.');
|
|
145
|
+
process.exitCode = 1;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const files = await listSourceFiles(srcPath);
|
|
150
|
+
const fileContents = new Map();
|
|
151
|
+
|
|
152
|
+
for (const filePath of files) {
|
|
153
|
+
// eslint-disable-next-line no-await-in-loop
|
|
154
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
155
|
+
fileContents.set(filePath, content);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const globalFactoryNames = new Set();
|
|
159
|
+
for (const [filePath, content] of fileContents.entries()) {
|
|
160
|
+
for (const name of extractFactoryNamesFromContent(content)) {
|
|
161
|
+
globalFactoryNames.add(name);
|
|
162
|
+
}
|
|
163
|
+
if (filePath.endsWith(path.join('core', 'presenter', 'index.ts')) || filePath.endsWith(path.join('core', 'presenter', 'index.js'))) {
|
|
164
|
+
for (const name of extractImportedPresenterFactories(content)) {
|
|
165
|
+
globalFactoryNames.add(name);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const occurrences = [];
|
|
171
|
+
for (const [filePath, content] of fileContents.entries()) {
|
|
172
|
+
occurrences.push(...collectPresenterOccurrences(content, filePath, globalFactoryNames));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (!occurrences.length) {
|
|
176
|
+
console.log('✅ No presenter declarations found.');
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const grouped = new Map();
|
|
181
|
+
for (const occurrence of occurrences) {
|
|
182
|
+
const list = grouped.get(occurrence.id) ?? [];
|
|
183
|
+
list.push(occurrence);
|
|
184
|
+
grouped.set(occurrence.id, list);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const duplicated = Array.from(grouped.entries()).filter(([, list]) => list.length > 1);
|
|
188
|
+
if (!duplicated.length) {
|
|
189
|
+
console.log(`✅ Presenter names are unique (${occurrences.length} found).`);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
console.error('❌ Duplicate presenter names found:');
|
|
194
|
+
for (const [presenterId, list] of duplicated) {
|
|
195
|
+
console.error(`\n- "${presenterId}"`);
|
|
196
|
+
for (const item of list) {
|
|
197
|
+
console.error(` • ${toPosixPath(path.relative(process.cwd(), item.file))}:${item.line} (${item.callee})`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
process.exitCode = 1;
|
|
202
|
+
}
|