-
Notifications
You must be signed in to change notification settings - Fork 41
/
imports.ts
66 lines (62 loc) · 1.98 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
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import { ImportDeclarationStructure, StructureKind } from 'ts-morph';
import {
complexTypeImportDeclarations,
coreImportDeclaration,
coreNavPropertyFieldTypeImportNames,
corePropertyFieldTypeImportNames,
corePropertyTypeImportNames,
externalImportDeclarations
} from '../imports';
import { VdmEntity, VdmServiceMetadata } from '../vdm-types';
export function importDeclarations(
entity: VdmEntity
): ImportDeclarationStructure[] {
return [
{
kind: StructureKind.ImportDeclaration,
moduleSpecifier: `./${entity.className}RequestBuilder`,
namedImports: [`${entity.className}RequestBuilder`]
},
...externalImportDeclarations(entity.properties),
...complexTypeImportDeclarations(entity.properties),
coreImportDeclaration(
[
...corePropertyTypeImportNames(entity.properties),
...corePropertyFieldTypeImportNames(entity.properties),
...coreNavPropertyFieldTypeImportNames(entity.navigationProperties),
'AllFields',
'CustomField',
'Entity',
'EntityBuilderType',
'Field'
].sort()
)
];
}
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}`
};
}