-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathimports.ts
73 lines (70 loc) · 2.2 KB
/
imports.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { ImportDeclarationStructure, StructureKind } from 'ts-morph';
import { caps, ODataVersion } from '@sap-cloud-sdk/util';
import {
complexTypeImportDeclarations,
coreImportDeclaration,
coreNavPropertyFieldTypeImportNames,
corePropertyFieldTypeImportNames,
corePropertyTypeImportNames,
enumTypeImportDeclarations,
externalImportDeclarations
} from '../imports';
import { VdmEntity, VdmServiceMetadata } from '../vdm-types';
export function importDeclarations(
entity: VdmEntity,
oDataVersion: ODataVersion
): ImportDeclarationStructure[] {
const versionInCap = caps(oDataVersion);
return [
{
kind: StructureKind.ImportDeclaration,
moduleSpecifier: `./${entity.className}RequestBuilder`,
namedImports: [`${entity.className}RequestBuilder`]
},
...externalImportDeclarations(entity.properties),
...complexTypeImportDeclarations(entity.properties),
...enumTypeImportDeclarations(entity.properties),
coreImportDeclaration(
[
...corePropertyTypeImportNames(entity.properties),
...corePropertyFieldTypeImportNames(entity.properties),
...coreNavPropertyFieldTypeImportNames(
entity.navigationProperties,
oDataVersion
),
'AllFields',
`CustomField${versionInCap}`,
`Entity${versionInCap}`,
'EntityBuilderType',
'Field'
].sort(),
oDataVersion
)
];
}
export function otherEntityImports(
entity: VdmEntity,
service: VdmServiceMetadata
): ImportDeclarationStructure[] {
return Array.from(new Set(entity.navigationProperties.map(n => n.to)))
.map(to => {
const matchedEntity = service.entities.find(e => e.entitySetName === to);
if (!matchedEntity) {
throw Error(
`Failed to find the entity from the service: ${JSON.stringify(
service
)} for entity ${entity}`
);
}
return matchedEntity.className;
})
.filter(name => name !== entity.className)
.map(name => otherEntityImport(name));
}
function otherEntityImport(name: string): ImportDeclarationStructure {
return {
kind: StructureKind.ImportDeclaration,
namedImports: [name, `${name}Type`],
moduleSpecifier: `./${name}`
};
}