-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathglobal-name-formatter.ts
72 lines (63 loc) · 2.21 KB
/
global-name-formatter.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
import { propertyExists } from '@sap-cloud-sdk/util';
import { ServiceMapping, VdmMapping } from './service-mapping';
import { UniqueNameFinder } from './unique-name-finder';
export class GlobalNameFormatter {
private directoryNameFinder: UniqueNameFinder = new UniqueNameFinder('-');
private npmPackageNameFinder: UniqueNameFinder = new UniqueNameFinder('-');
private vdmMapping: VdmMapping;
constructor(vdmMapping: VdmMapping | undefined) {
this.vdmMapping = vdmMapping || {};
this.directoryNameFinder.addToUsedNames(
...Object.entries(this.vdmMapping).map(([k, v]) => v.directoryName)
);
this.npmPackageNameFinder.addToUsedNames(
...Object.entries(this.vdmMapping).map(([k, v]) => v.npmPackageName)
);
}
public uniqueDirectoryName(
directoryName: string,
originalFileName: string
): string {
return (
this.directoryNameFromMapping(originalFileName) ||
this.transformAndCacheDirectoryName(directoryName)
);
}
public uniqueNpmPackageName(
npmPackageName: string,
originalFileName: string
): string {
return (
this.npmPackageNameFromMapping(originalFileName) ||
this.transformAndCacheNpmPackageName(npmPackageName)
);
}
private transformAndCacheDirectoryName(directoryName: string): string {
const newName = this.directoryNameFinder.findUniqueName(directoryName);
this.directoryNameFinder.addToUsedNames(newName);
return newName;
}
private transformAndCacheNpmPackageName(npmPackageName: string): string {
const newName = this.npmPackageNameFinder.findUniqueName(npmPackageName);
this.npmPackageNameFinder.addToUsedNames(newName);
return newName;
}
private directoryNameFromMapping(
originalFileName: string
): string | undefined {
return this.fromMapping(originalFileName, 'directoryName');
}
private npmPackageNameFromMapping(
originalFileName: string
): string | undefined {
return this.fromMapping(originalFileName, 'npmPackageName');
}
private fromMapping(
originalFileName: string,
key: keyof ServiceMapping
): string | undefined {
return propertyExists(this.vdmMapping, originalFileName, key)
? this.vdmMapping[originalFileName][key]
: undefined;
}
}