-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_import.ts
53 lines (44 loc) 路 1.21 KB
/
load_import.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
import { isFunction } from "../deps/npm.ts";
import { path } from "../deps/path.ts";
import { generateLookupFiles, LoadConfigResult } from "./types.ts";
/**
* Load the import dynamically.
*
* @param source The source directory to load the template configuration from.
*/
export async function loadImport<
Config extends object = Record<string, unknown>,
>(
source: string,
name: string,
): Promise<LoadConfigResult<Config> | undefined> {
const now = Date.now();
let found = false;
// lookup the configuration file from the provided `cwd`.
const files = generateLookupFiles(name);
// only do it this way if not being invoked in a worker
for (const file of files) {
const filePath = path.join(source, file);
try {
const url = new URL(`?now=${now}`, path.toFileUrl(filePath));
const exported = await import(url.href);
found = true;
if (!exported?.default) {
return;
}
const config = await (
isFunction(exported.default) ? exported.default() : exported.default
);
if (!config) {
return;
}
return { config, path: filePath };
} catch {
if (found) {
break;
}
continue;
}
}
return;
}