@azure-net/cli 2.0.0 → 2.0.2

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.
@@ -4,7 +4,10 @@ import { toPascalCase, toCamelCase } from './contextUtils.js';
4
4
  import { writeIfNotExists, updateIndexTs } from './fileUtils.js';
5
5
 
6
6
  export async function ensureProvider(context, layer, dependencies = {}) {
7
- const providerName = `${toPascalCase(context)}${layer}Provider`;
7
+ const contextName = toPascalCase(context);
8
+ const providerName = `${layer}Provider`;
9
+ const boundaryName = `${contextName}${layer}BoundaryProvider`;
10
+
8
11
  const contextPath = path.join(process.cwd(), 'src/app/contexts', context);
9
12
  const providerPath = path.join(contextPath, layer, 'Providers', `${layer}Provider.ts`);
10
13
 
@@ -16,29 +19,38 @@ export async function ensureProvider(context, layer, dependencies = {}) {
16
19
  const content = generateProviderContent(context, layer, dependencies);
17
20
  await writeIfNotExists(providerPath, content);
18
21
  await updateIndexTs(path.dirname(providerPath));
19
- await updateIndexTs(path.join(contextPath, layer));
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
+
20
27
  return { exists: false, name: providerName, path: providerPath };
21
28
  }
22
29
  }
23
30
 
24
31
  function generateProviderContent(context, layer, dependencies) {
25
- const providerName = `${toPascalCase(context)}${layer}BoundaryProvider`;
32
+ const contextName = toPascalCase(context);
33
+ const boundaryName = `${contextName}${layer}BoundaryProvider`;
26
34
  const varName = `${layer}Provider`;
27
35
 
28
36
  let imports = [`import { createBoundaryProvider } from '@azure-net/kit';`];
29
37
  let deps = [];
30
- let factories = [];
38
+ let depParams = '';
31
39
 
32
- if (dependencies.datasource && layer === 'Infrastructure') {
33
- imports.push(`import { ${dependencies.datasource} } from '../Datasources/index.js';`);
34
- factories.push(`\t${dependencies.datasource}: () => new ${dependencies.datasource}()`);
40
+ if (dependencies.hasDatasource && layer === 'Infrastructure') {
41
+ imports.push(`import { DatasourceProvider } from './DatasourceProvider';`);
42
+ deps.push('DatasourceProvider');
35
43
  }
36
44
 
37
45
  if (dependencies.infrastructure && layer === 'Application') {
38
- imports.push(`import { InfrastructureProvider } from '../../Infrastructure/index.js';`);
46
+ imports.push(`import { InfrastructureProvider } from '\$${context}/Infrastructure';`);
39
47
  deps.push('InfrastructureProvider');
40
48
  }
41
49
 
50
+ if (deps.length > 0) {
51
+ depParams = `{ ${deps.join(', ')} }`;
52
+ }
53
+
42
54
  const dependsOnStr = deps.length > 0
43
55
  ? `,\n\t{ dependsOn: { ${deps.join(', ')} } }`
44
56
  : '';
@@ -46,32 +58,49 @@ function generateProviderContent(context, layer, dependencies) {
46
58
  return `${imports.join('\n')}
47
59
 
48
60
  export const ${varName} = createBoundaryProvider(
49
- \t'${providerName}',
50
- \t(${deps.length > 0 ? `{ ${deps.join(', ')} }` : ''}) => ({
51
- ${factories.join(',\n')}
61
+ \t'${boundaryName}',
62
+ \t(${depParams ? depParams : ''}) => ({
63
+ \t\t// Services will be added here
52
64
  \t})${dependsOnStr}
53
65
  );`;
54
66
  }
55
67
 
56
- export async function addToProvider(providerPath, serviceName, importPath) {
68
+ export async function addToProvider(providerPath, serviceName, importPath, dependency = null) {
57
69
  let content = await fs.readFile(providerPath, 'utf-8');
58
70
 
59
71
  // Add import if not exists
60
72
  const importStatement = `import { ${serviceName} } from '${importPath}';`;
61
- if (!content.includes(importStatement)) {
73
+ if (!content.includes(serviceName)) {
62
74
  const lastImportIndex = content.lastIndexOf('import ');
63
75
  const endOfImportLine = content.indexOf('\n', lastImportIndex);
64
76
  content = content.slice(0, endOfImportLine + 1) + importStatement + '\n' + content.slice(endOfImportLine + 1);
65
77
  }
66
78
 
67
- // Add factory method
68
- const factoryLine = `\t\t${serviceName}: () => new ${serviceName}()`;
69
- const returnIndex = content.indexOf('=> ({');
70
- const endOfReturn = content.indexOf('\t})', returnIndex);
79
+ // Add factory method with dependency
80
+ const factoryLine = dependency
81
+ ? `\t\t${serviceName}: () => new ${serviceName}(${dependency})`
82
+ : `\t\t${serviceName}: () => new ${serviceName}()`;
83
+
84
+ if (!content.includes(`${serviceName}:`)) {
85
+ // Find the position to insert
86
+ const returnIndex = content.indexOf('=> ({');
87
+ const endOfReturn = content.indexOf('\t})', returnIndex);
88
+
89
+ // Check if there are existing services
90
+ const servicesSection = content.substring(returnIndex, endOfReturn);
91
+ const hasServices = servicesSection.includes(':');
71
92
 
72
- if (!content.includes(factoryLine)) {
73
- const beforeEnd = content.lastIndexOf('\n', endOfReturn);
74
- content = content.slice(0, beforeEnd) + ',\n' + factoryLine + content.slice(beforeEnd);
93
+ if (hasServices) {
94
+ // Add comma before new service
95
+ const beforeEnd = content.lastIndexOf('\n', endOfReturn);
96
+ content = content.slice(0, beforeEnd) + ',\n' + factoryLine + content.slice(beforeEnd);
97
+ } else {
98
+ // First service, replace comment
99
+ content = content.replace(
100
+ '// Services will be added here',
101
+ factoryLine
102
+ );
103
+ }
75
104
  }
76
105
 
77
106
  await fs.writeFile(providerPath, content, 'utf-8');