Skip to content

Commit b03b119

Browse files
committed
Added ESM flag and parsed it in the neccessary functions
1 parent 18e6d88 commit b03b119

File tree

6 files changed

+75
-29
lines changed

6 files changed

+75
-29
lines changed

packages/generator/src/complex-type/file.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import { complexTypeNamespace } from './namespace';
66
import type { VdmComplexType } from '../vdm-types';
77
import type { ODataVersion } from '@sap-cloud-sdk/util';
88
import type { SourceFileStructure } from 'ts-morph';
9+
import type { CreateFileOptions } from '@sap-cloud-sdk/generator-common/internal';
910

1011
/**
1112
* @internal
1213
*/
1314
export function complexTypeSourceFile(
1415
complexType: VdmComplexType,
15-
oDataVersion: ODataVersion
16+
oDataVersion: ODataVersion,
17+
options?: CreateFileOptions
1618
): SourceFileStructure {
1719
return {
1820
kind: StructureKind.SourceFile,
1921
statements: [
20-
...importDeclarations(complexType, oDataVersion),
22+
...importDeclarations(complexType, oDataVersion, options),
2123
complexTypeInterface(complexType),
2224
fieldTypeClass(complexType),
2325
complexTypeNamespace(complexType)

packages/generator/src/complex-type/imports.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ import {
66
import type { ImportDeclarationStructure } from 'ts-morph';
77
import type { ODataVersion } from '@sap-cloud-sdk/util';
88
import type { VdmComplexType } from '../vdm-types';
9+
import type { CreateFileOptions } from '@sap-cloud-sdk/generator-common/internal';
910

1011
/**
1112
* @internal
1213
*/
1314
export function importDeclarations(
1415
complexType: VdmComplexType,
15-
oDataVersion: ODataVersion
16+
oDataVersion: ODataVersion,
17+
options?: CreateFileOptions
1618
): ImportDeclarationStructure[] {
1719
return [
18-
...complexTypeImportDeclarations(complexType.properties),
19-
...enumTypeImportDeclarations(complexType.properties),
20+
...complexTypeImportDeclarations(complexType.properties, options),
21+
...enumTypeImportDeclarations(complexType.properties, options),
2022
odataImportDeclarationTsMorph(
2123
[
2224
'DefaultDeSerializers',

packages/generator/src/entity/file.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import { entityImportDeclarations, otherEntityImports } from './imports';
55
import { entityTypeInterface } from './interface';
66
import type { VdmEntity, VdmServiceMetadata } from '../vdm-types';
77
import type { SourceFileStructure } from 'ts-morph';
8+
import type { CreateFileOptions } from '@sap-cloud-sdk/generator-common/internal';
89

910
/**
1011
* @internal
1112
*/
1213
export function entitySourceFile(
1314
entity: VdmEntity,
14-
service: VdmServiceMetadata
15+
service: VdmServiceMetadata,
16+
options?: CreateFileOptions
1517
): SourceFileStructure {
1618
return {
1719
kind: StructureKind.SourceFile,
1820
statements: [
19-
...entityImportDeclarations(entity, service, service.oDataVersion),
20-
...otherEntityImports(entity, service),
21+
...entityImportDeclarations(entity, service, service.oDataVersion, options),
22+
...otherEntityImports(entity, service, options),
2123
entityClass(entity, service),
2224
entityTypeInterface(entity, service),
2325
...entity.operations.map(operation => parametersInterface(operation))

packages/generator/src/entity/imports.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,34 @@ import { operationDeclarations } from '../operations';
99
import type { ImportDeclarationStructure } from 'ts-morph';
1010
import type { ODataVersion } from '@sap-cloud-sdk/util';
1111
import type { VdmEntity, VdmServiceMetadata } from '../vdm-types';
12+
import type { CreateFileOptions } from '@sap-cloud-sdk/generator-common/internal';
1213

1314
/**
1415
* @internal
1516
*/
1617
export function entityImportDeclarations(
1718
entity: VdmEntity,
1819
service: VdmServiceMetadata,
19-
oDataVersion: ODataVersion
20+
oDataVersion: ODataVersion,
21+
options?: CreateFileOptions
2022
): ImportDeclarationStructure[] {
2123
if (oDataVersion === 'v4') {
2224
return mergeImportDeclarations([
2325
odataImportDeclarationTsMorph(
2426
['Entity', 'DefaultDeSerializers', 'DeSerializers', 'DeserializedType'],
2527
oDataVersion
2628
),
27-
...complexTypeImportDeclarations(entity.properties),
29+
...complexTypeImportDeclarations(entity.properties, options),
2830
{
2931
namedImports: [`${entity.className}Api`],
30-
moduleSpecifier: `./${entity.className}Api`,
32+
moduleSpecifier: options?.generateESM
33+
? `./${entity.className}Api.js`
34+
: `./${entity.className}Api`,
3135
kind: StructureKind.ImportDeclaration,
3236
isTypeOnly: true
3337
},
3438
...operationDeclarations(service, entity.operations),
35-
...enumTypeImportDeclarations(entity.properties)
39+
...enumTypeImportDeclarations(entity.properties, options)
3640
]);
3741
}
3842

@@ -41,22 +45,25 @@ export function entityImportDeclarations(
4145
['Entity', 'DefaultDeSerializers', 'DeSerializers', 'DeserializedType'],
4246
oDataVersion
4347
),
44-
...complexTypeImportDeclarations(entity.properties),
48+
...complexTypeImportDeclarations(entity.properties, options),
4549
{
4650
namedImports: [`${entity.className}Api`],
47-
moduleSpecifier: `./${entity.className}Api`,
51+
moduleSpecifier: options?.generateESM
52+
? `./${entity.className}Api.js`
53+
: `./${entity.className}Api`,
4854
kind: StructureKind.ImportDeclaration,
4955
isTypeOnly: true
5056
},
51-
...enumTypeImportDeclarations(entity.properties)
57+
...enumTypeImportDeclarations(entity.properties, options)
5258
];
5359
}
5460
/**
5561
* @internal
5662
*/
5763
export function otherEntityImports(
5864
entity: VdmEntity,
59-
service: VdmServiceMetadata
65+
service: VdmServiceMetadata,
66+
options?: CreateFileOptions
6067
): ImportDeclarationStructure[] {
6168
return Array.from(new Set(entity.navigationProperties.map(n => n.to)))
6269
.map(to => {
@@ -71,13 +78,15 @@ export function otherEntityImports(
7178
return matchedEntity.className;
7279
})
7380
.filter(name => name !== entity.className)
74-
.map(name => otherEntityImport(name));
81+
.map(name => otherEntityImport(name, options));
7582
}
7683

77-
function otherEntityImport(name: string): ImportDeclarationStructure {
84+
function otherEntityImport(name: string, options?: CreateFileOptions): ImportDeclarationStructure {
7885
return {
7986
kind: StructureKind.ImportDeclaration,
8087
namedImports: [name, `${name}Type`],
81-
moduleSpecifier: `./${name}`
88+
moduleSpecifier: options?.generateESM
89+
? `./${name}.js`
90+
: `./${name}`
8291
};
8392
}

packages/generator/src/generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ async function generateMandatorySources(
377377
sourceFile(
378378
serviceDir,
379379
entity.className,
380-
entitySourceFile(entity, service),
380+
entitySourceFile(entity, service, createFileOptions),
381381
createFileOptions
382382
)
383383
);
@@ -413,7 +413,7 @@ async function generateMandatorySources(
413413
sourceFile(
414414
serviceDir,
415415
complexType.typeName,
416-
complexTypeSourceFile(complexType, service.oDataVersion),
416+
complexTypeSourceFile(complexType, service.oDataVersion, createFileOptions),
417417
createFileOptions
418418
)
419419
);

packages/generator/src/imports.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
VdmNavigationProperty,
99
VdmProperty
1010
} from './vdm-types';
11+
import type { CreateFileOptions } from '@sap-cloud-sdk/generator-common/internal';
1112

1213
/**
1314
* @internal
@@ -107,25 +108,27 @@ export function navPropertyFieldTypeImportNames(
107108
* @internal
108109
*/
109110
export function complexTypeImportDeclarations(
110-
properties: VdmProperty[]
111+
properties: VdmProperty[],
112+
options?: CreateFileOptions
111113
): ImportDeclarationStructure[] {
112114
return mergeImportDeclarations(
113115
properties
114116
.filter(prop => prop.isComplex)
115-
.map(prop => complexTypeImportDeclaration(prop))
117+
.map(prop => complexTypeImportDeclaration(prop, options))
116118
);
117119
}
118120

119121
/**
120122
* @internal
121123
*/
122124
export function enumTypeImportDeclarations(
123-
properties: VdmProperty[]
125+
properties: VdmProperty[],
126+
options?: CreateFileOptions
124127
): ImportDeclarationStructure[] {
125128
return mergeImportDeclarations(
126129
properties
127130
.filter(prop => prop.isEnum)
128-
.map(prop => enumTypeImportDeclaration(prop))
131+
.map(prop => enumTypeImportDeclaration(prop, options))
129132
);
130133
}
131134

@@ -184,21 +187,49 @@ export function mergeImportDeclarations(
184187
}
185188

186189
function complexTypeImportDeclaration(
187-
prop: VdmProperty
190+
prop: VdmProperty,
191+
options?: CreateFileOptions
188192
): ImportDeclarationStructure {
189193
return {
190194
kind: StructureKind.ImportDeclaration,
191-
moduleSpecifier: `./${prop.jsType}`,
195+
moduleSpecifier: options?.generateESM
196+
? `./${prop.jsType}.js`
197+
: `./${prop.jsType}`,
192198
namedImports: [prop.jsType, ...(prop.isCollection ? [] : [prop.fieldType])]
193199
};
194200
}
195201

196202
function enumTypeImportDeclaration(
197-
prop: VdmProperty
203+
prop: VdmProperty,
204+
options?: CreateFileOptions
198205
): ImportDeclarationStructure {
199206
return {
200207
kind: StructureKind.ImportDeclaration,
201-
moduleSpecifier: `./${prop.jsType}`,
208+
moduleSpecifier: options?.generateESM
209+
? `./${prop.jsType}.js`
210+
: `./${prop.jsType}`,
202211
namedImports: [prop.jsType]
203212
};
204213
}
214+
215+
/**
216+
* @internal
217+
*/
218+
export function getImportsWithESM(
219+
schemaName: string,
220+
fileName: string,
221+
properties: VdmProperty[],
222+
options?: CreateFileOptions
223+
): ImportDeclarationStructure[] {
224+
return mergeImportDeclarations(
225+
properties
226+
.filter(prop => prop.isComplex || prop.isEnum)
227+
.map(prop => ({
228+
kind: StructureKind.ImportDeclaration,
229+
moduleSpecifier: options?.generateESM
230+
? `./${prop.jsType}.js`
231+
: `./${prop.jsType}`,
232+
namedImports: [prop.jsType, ...(prop.isCollection ? [] : [prop.fieldType])]
233+
}))
234+
);
235+
}

0 commit comments

Comments
 (0)