-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathinput-path-provider.ts
46 lines (39 loc) · 1.22 KB
/
input-path-provider.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
/* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. */
import { lstatSync, PathLike, readdirSync } from 'fs';
import path from 'path';
const validFileExtensions = ['.edmx', '.xml'];
export function edmxPaths(input: PathLike): PathLike[] {
if (lstatSync(input).isDirectory()) {
return readdirSync(input)
.map(f => edmxPaths(path.join(input.toString(), f)))
.reduce((prev, curr) => {
prev.push(...curr);
return prev;
}, []);
}
return hasEdmxFileExtension(input.toString()) ? [input] : [];
}
export function inputPaths(
input: PathLike,
useSwagger: boolean
): ServiceDefinitionPaths[] {
return edmxPaths(input).map(edmxPath => {
if (useSwagger) {
const swaggerPath = swaggerPathForEdmx(edmxPath);
if (swaggerPath) {
return { edmxPath, swaggerPath };
}
}
return { edmxPath };
});
}
function swaggerPathForEdmx(edmxPath: PathLike): PathLike {
return edmxPath.toString().replace(/\.edmx$/, '.json');
}
function hasEdmxFileExtension(fileName: string): boolean {
return validFileExtensions.includes(path.extname(fileName.toLowerCase()));
}
export interface ServiceDefinitionPaths {
edmxPath: PathLike;
swaggerPath?: PathLike;
}