@azure-net/cli 2.0.4 → 2.0.6

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,112 +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
- let depParams = '';
39
-
40
- if (dependencies.hasDatasource && layer === 'Infrastructure') {
41
- imports.push(`import { DatasourceProvider } from './DatasourceProvider';`);
42
- deps.push('DatasourceProvider');
43
- }
44
-
45
- if (dependencies.infrastructure && layer === 'Application') {
46
- imports.push(`import { InfrastructureProvider } from '\$${context}/Infrastructure';`);
47
- deps.push('InfrastructureProvider');
48
- }
49
-
50
- if (deps.length > 0) {
51
- depParams = `{ ${deps.join(', ')} }`;
52
- }
53
-
54
- const dependsOnStr = deps.length > 0
55
- ? `,\n\t{ dependsOn: { ${deps.join(', ')} } }`
56
- : '';
57
-
58
- return `${imports.join('\n')}
59
-
60
- export const ${varName} = createBoundaryProvider(
61
- \t'${boundaryName}',
62
- \t(${depParams ? depParams : ''}) => ({
63
- \t\t// Services will be added here
64
- \t})${dependsOnStr}
65
- );`;
66
- }
67
-
68
- export async function addToProvider(providerPath, serviceName, importPath, dependency = null) {
69
- let content = await fs.readFile(providerPath, 'utf-8');
70
-
71
- // Use alias import path instead of relative
72
- const aliasImportPath = importPath.startsWith('$') ? importPath : importPath.startsWith('../')
73
- ? importPath // Keep relative if it's internal to the same layer
74
- : importPath;
75
-
76
- // Add import if not exists
77
- const importStatement = `import { ${serviceName} } from '${aliasImportPath}';`;
78
- if (!content.includes(serviceName)) {
79
- const lastImportIndex = content.lastIndexOf('import ');
80
- const endOfImportLine = content.indexOf('\n', lastImportIndex);
81
- content = content.slice(0, endOfImportLine + 1) + importStatement + '\n' + content.slice(endOfImportLine + 1);
82
- }
83
-
84
- // Add factory method with dependency
85
- const factoryLine = dependency
86
- ? `\t\t${serviceName}: () => new ${serviceName}(${dependency})`
87
- : `\t\t${serviceName}: () => new ${serviceName}()`;
88
-
89
- if (!content.includes(`${serviceName}:`)) {
90
- // Find the position to insert
91
- const returnIndex = content.indexOf('=> ({');
92
- const endOfReturn = content.indexOf('\t})', returnIndex);
93
-
94
- // Check if there are existing services
95
- const servicesSection = content.substring(returnIndex, endOfReturn);
96
- const hasServices = servicesSection.includes(':');
97
-
98
- if (hasServices) {
99
- // Add comma before new service
100
- const beforeEnd = content.lastIndexOf('\n', endOfReturn);
101
- content = content.slice(0, beforeEnd) + ',\n' + factoryLine + content.slice(beforeEnd);
102
- } else {
103
- // First service, replace comment
104
- content = content.replace(
105
- '// Services will be added here',
106
- factoryLine
107
- );
108
- }
109
- }
110
-
111
- await fs.writeFile(providerPath, content, 'utf-8');
112
- }