@azure-net/cli 2.0.5 → 2.0.7

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.
@@ -1,254 +0,0 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import { toPascalCase, toCamelCase } from './contextUtils.js';
4
- import { writeIfNotExists, updateIndexTs } from './fileUtils.js';
5
-
6
- export async function ensureProvider(context, layer, dependencies = {}) {
7
- const contextName = toPascalCase(context);
8
- const providerName = `${layer}Provider`;
9
- const boundaryName = `${contextName}${layer}BoundaryProvider`;
10
-
11
- const contextPath = path.join(process.cwd(), 'src/app/contexts', context);
12
- const providerPath = path.join(contextPath, layer, 'Providers', `${layer}Provider.ts`);
13
-
14
- try {
15
- await fs.access(providerPath);
16
- return { exists: true, name: providerName, path: providerPath };
17
- } catch {
18
- // Provider doesn't exist, create it
19
- const content = generateProviderContent(context, layer, dependencies);
20
- await writeIfNotExists(providerPath, content);
21
- await updateIndexTs(path.dirname(providerPath));
22
-
23
- // Update layer index to export provider
24
- const layerIndexPath = path.join(contextPath, layer, 'index.ts');
25
- await writeIfNotExists(layerIndexPath, `export { ${providerName} } from './Providers';`);
26
-
27
- return { exists: false, name: providerName, path: providerPath };
28
- }
29
- }
30
-
31
- function generateProviderContent(context, layer, dependencies) {
32
- const contextName = toPascalCase(context);
33
- const boundaryName = `${contextName}${layer}BoundaryProvider`;
34
- const varName = `${layer}Provider`;
35
-
36
- let imports = [`import { createBoundaryProvider } from '@azure-net/kit';`];
37
- let deps = [];
38
-
39
- if (dependencies.hasDatasource && layer === 'Infrastructure') {
40
- imports.push(`import { DatasourceProvider } from './DatasourceProvider';`);
41
- deps.push('DatasourceProvider');
42
- }
43
-
44
- if (dependencies.infrastructure && layer === 'Application') {
45
- imports.push(`import { InfrastructureProvider } from '\$${context}/Infrastructure';`);
46
- deps.push('InfrastructureProvider');
47
- }
48
-
49
- const depParams = deps.length > 0 ? `{ ${deps.join(', ')} }` : '';
50
- const dependsOnStr = deps.length > 0
51
- ? `,\n\t{ dependsOn: { ${deps.join(', ')} } }`
52
- : '';
53
-
54
- return `${imports.join('\n')}
55
-
56
- export const ${varName} = createBoundaryProvider(
57
- \t'${boundaryName}',
58
- \t(${depParams}) => ({
59
- \t\t// Services will be added here
60
- \t})${dependsOnStr}
61
- );`;
62
- }
63
-
64
- export async function addToProvider(providerPath, serviceName, importPath, dependency = null) {
65
- let content = await fs.readFile(providerPath, 'utf-8');
66
-
67
- // Parse existing imports to group them
68
- const importRegex = /^import\s+{([^}]+)}\s+from\s+['"]([^'"]+)['"]/gm;
69
- const imports = new Map();
70
- let match;
71
-
72
- while ((match = importRegex.exec(content)) !== null) {
73
- const [fullMatch, importedItems, fromPath] = match;
74
- if (!imports.has(fromPath)) {
75
- imports.set(fromPath, new Set());
76
- }
77
- importedItems.split(',').forEach(item => {
78
- imports.get(fromPath).add(item.trim());
79
- });
80
- }
81
-
82
- // Add new import to the map
83
- if (!Array.from(imports.values()).some(set => set.has(serviceName))) {
84
- if (!imports.has(importPath)) {
85
- imports.set(importPath, new Set());
86
- }
87
- imports.get(importPath).add(serviceName);
88
- }
89
-
90
- // Rebuild import section
91
- const newImports = [`import { createBoundaryProvider } from '@azure-net/kit';`];
92
-
93
- // Add DatasourceProvider import if exists
94
- if (content.includes('DatasourceProvider') && !content.includes("import { DatasourceProvider }")) {
95
- newImports.push(`import { DatasourceProvider } from './DatasourceProvider';`);
96
- }
97
-
98
- // Add other imports
99
- for (const [fromPath, items] of imports) {
100
- if (fromPath !== '@azure-net/kit' && !fromPath.includes('DatasourceProvider')) {
101
- const itemsStr = Array.from(items).join(', ');
102
- newImports.push(`import { ${itemsStr} } from '${fromPath}';`);
103
- }
104
- }
105
-
106
- // Find where imports end and provider starts
107
- const providerStartIndex = content.indexOf('export const');
108
- const beforeProvider = content.substring(0, providerStartIndex);
109
- const afterProvider = content.substring(providerStartIndex);
110
-
111
- // Check if DatasourceProvider dependency needs to be added
112
- let updatedAfterProvider = afterProvider;
113
- if (dependency && dependency.includes('DatasourceProvider')) {
114
- // Check if provider already has dependencies
115
- if (!afterProvider.includes('({ DatasourceProvider })')) {
116
- // Add DatasourceProvider to function params
117
- updatedAfterProvider = updatedAfterProvider.replace(
118
- /createBoundaryProvider\(\s*'[^']+',\s*\(\)/,
119
- `createBoundaryProvider(\n\t'$1',\n\t({ DatasourceProvider })`
120
- );
121
-
122
- // Add dependsOn if not exists
123
- if (!updatedAfterProvider.includes('dependsOn')) {
124
- updatedAfterProvider = updatedAfterProvider.replace(
125
- /\)\s*\);$/,
126
- `),\n\t{ dependsOn: { DatasourceProvider } }\n);`
127
- );
128
- }
129
- }
130
- }
131
-
132
- // Add factory method with dependency
133
- const factoryLine = dependency
134
- ? `\t\t${serviceName}: () => new ${serviceName}(${dependency})`
135
- : `\t\t${serviceName}: () => new ${serviceName}()`;
136
-
137
- if (!updatedAfterProvider.includes(`${serviceName}:`)) {
138
- // Find the position to insert
139
- const returnIndex = updatedAfterProvider.indexOf('=> ({');
140
- const endOfReturn = updatedAfterProvider.indexOf('\t})', returnIndex);
141
-
142
- // Check if there are existing services
143
- const servicesSection = updatedAfterProvider.substring(returnIndex, endOfReturn);
144
- const hasServices = servicesSection.includes(':') && !servicesSection.includes('// Services will be added here');
145
-
146
- if (hasServices) {
147
- // Add comma before new service
148
- const beforeEnd = updatedAfterProvider.lastIndexOf('\n', endOfReturn);
149
- updatedAfterProvider = updatedAfterProvider.slice(0, beforeEnd) + ',\n' + factoryLine + updatedAfterProvider.slice(beforeEnd);
150
- } else {
151
- // First service, replace comment
152
- updatedAfterProvider = updatedAfterProvider.replace(
153
- '// Services will be added here',
154
- factoryLine
155
- );
156
- }
157
- }
158
-
159
- // Combine everything
160
- const finalContent = newImports.join('\n') + '\n\n' + updatedAfterProvider;
161
- await fs.writeFile(providerPath, finalContent, 'utf-8');
162
- }
163
-
164
- export async function ensureDatasourceProvider(context, datasourceName, isFromCore = true) {
165
- const contextName = toPascalCase(context);
166
- const contextPath = path.join(process.cwd(), 'src/app/contexts', context);
167
- const providerPath = path.join(contextPath, 'Infrastructure/Providers/DatasourceProvider.ts');
168
-
169
- try {
170
- await fs.access(providerPath);
171
- // Provider exists, add datasource if needed
172
- await addDatasourceToProvider(providerPath, datasourceName, isFromCore);
173
- } catch {
174
- // Create new DatasourceProvider
175
- const content = `import { createBoundaryProvider } from '@azure-net/kit';
176
- import { ${datasourceName} } from '${isFromCore ? '$core' : '../Http/Datasource'}';
177
-
178
- export const DatasourceProvider = createBoundaryProvider('${contextName}DatasourceProvider', () => ({
179
- \t${datasourceName}: () => new ${datasourceName}({})
180
- }));`;
181
-
182
- await writeIfNotExists(providerPath, content);
183
- await updateIndexTs(path.dirname(providerPath));
184
- }
185
- }
186
-
187
- async function addDatasourceToProvider(providerPath, datasourceName, isFromCore = true) {
188
- let content = await fs.readFile(providerPath, 'utf-8');
189
-
190
- // Check if datasource already exists
191
- if (content.includes(`${datasourceName}:`)) {
192
- return;
193
- }
194
-
195
- // Parse existing imports to group them
196
- const importRegex = /^import\s+{([^}]+)}\s+from\s+['"]([^'"]+)['"]/gm;
197
- const imports = new Map();
198
- let match;
199
-
200
- while ((match = importRegex.exec(content)) !== null) {
201
- const [fullMatch, importedItems, fromPath] = match;
202
- if (!imports.has(fromPath)) {
203
- imports.set(fromPath, new Set());
204
- }
205
- importedItems.split(',').forEach(item => {
206
- imports.get(fromPath).add(item.trim());
207
- });
208
- }
209
-
210
- // Add datasource to imports
211
- const importPath = isFromCore ? '$core' : '../Http/Datasource';
212
- if (!imports.has(importPath)) {
213
- imports.set(importPath, new Set());
214
- }
215
- imports.get(importPath).add(datasourceName);
216
-
217
- // Rebuild import section
218
- const newImports = [`import { createBoundaryProvider } from '@azure-net/kit';`];
219
- for (const [fromPath, items] of imports) {
220
- if (fromPath !== '@azure-net/kit') {
221
- const itemsStr = Array.from(items).join(', ');
222
- newImports.push(`import { ${itemsStr} } from '${fromPath}';`);
223
- }
224
- }
225
-
226
- // Find provider content
227
- const providerStartIndex = content.indexOf('export const');
228
- const afterProvider = content.substring(providerStartIndex);
229
-
230
- // Add factory method
231
- const factoryLine = `\t${datasourceName}: () => new ${datasourceName}({})`;
232
-
233
- const returnIndex = afterProvider.indexOf('=> ({');
234
- const endOfReturn = afterProvider.indexOf('})', returnIndex);
235
-
236
- // Check if there are existing services
237
- const servicesSection = afterProvider.substring(returnIndex, endOfReturn);
238
- const hasServices = servicesSection.includes(':');
239
-
240
- let updatedAfterProvider = afterProvider;
241
- if (hasServices) {
242
- // Add comma before new service
243
- const beforeEnd = updatedAfterProvider.lastIndexOf('\n', endOfReturn);
244
- updatedAfterProvider = updatedAfterProvider.slice(0, beforeEnd) + ',\n' + factoryLine + updatedAfterProvider.slice(beforeEnd);
245
- } else {
246
- // First service
247
- const insertPoint = returnIndex + '=> ({\n'.length;
248
- updatedAfterProvider = updatedAfterProvider.slice(0, insertPoint) + factoryLine + '\n' + updatedAfterProvider.slice(insertPoint);
249
- }
250
-
251
- // Combine everything
252
- const finalContent = newImports.join('\n') + '\n\n' + updatedAfterProvider;
253
- await fs.writeFile(providerPath, finalContent, 'utf-8');
254
- }