|
1
|
import { readdirSync, statSync } from "fs" |
|
2
|
import { join } from "path" |
|
3
|
|
|
4
|
/** |
|
5
|
* Load all the models from the given path |
|
6
|
* @param basePath |
|
7
|
*/ |
|
8
|
export function loadModels(basePath: string) { |
|
9
|
const excludedExtensions = [".ts.map", ".js.map", ".d.ts"] |
|
10
|
|
|
11
|
let modelsFiles: any[] = [] |
|
12
|
try { |
|
13
|
modelsFiles = readdirSync(basePath) |
|
14
|
} catch (e) {} |
|
15
|
|
|
16
|
return modelsFiles |
|
17
|
.flatMap((file) => { |
|
18
|
if ( |
|
19
|
file.startsWith("index.") || |
|
20
|
excludedExtensions.some((ext) => file.endsWith(ext)) |
|
21
|
) { |
|
22
|
return |
|
23
|
} |
|
24
|
|
|
25
|
const filePath = join(basePath, file) |
|
26
|
const stats = statSync(filePath) |
|
27
|
|
|
28
|
if (stats.isFile()) { |
|
29
|
try { |
|
30
|
const required = require(filePath) |
|
31
|
|
|
32
|
return Object.values(required).filter( |
|
33
|
(resource: any) => !!resource.name |
|
34
|
) |
|
35
|
} catch (e) {} |
|
36
|
} |
|
37
|
|
|
38
|
return |
|
39
|
}) |
|
40
|
.filter(Boolean) as any[] |
|
41
|
} |