/
Explorer.ts
122 lines (95 loc) · 3.27 KB
/
Explorer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import path from 'path';
import { ExplorerBase } from './ExplorerBase';
import { readFile } from './readFile';
import { cacheWrapper } from './cacheWrapper';
import { getDirectory } from './getDirectory';
import { CosmiconfigResult, ExplorerOptions, LoadedFileContent } from './types';
class Explorer extends ExplorerBase<ExplorerOptions> {
public constructor(options: ExplorerOptions) {
super(options);
}
public async search(
searchFrom: string = process.cwd(),
): Promise<CosmiconfigResult> {
const startDirectory = await getDirectory(searchFrom);
const result = await this.searchFromDirectory(startDirectory);
return result;
}
private async searchFromDirectory(dir: string): Promise<CosmiconfigResult> {
const absoluteDir = path.resolve(process.cwd(), dir);
const run = async (): Promise<CosmiconfigResult> => {
const result = await this.searchDirectory(absoluteDir);
const nextDir = this.nextDirectoryToSearch(absoluteDir, result);
if (nextDir) {
return this.searchFromDirectory(nextDir);
}
const transformResult = await this.config.transform(result);
return transformResult;
};
if (this.searchCache) {
return cacheWrapper(this.searchCache, absoluteDir, run);
}
return run();
}
private async searchDirectory(dir: string): Promise<CosmiconfigResult> {
for await (const place of this.config.searchPlaces) {
const placeResult = await this.loadSearchPlace(dir, place);
if (this.shouldSearchStopWithResult(placeResult) === true) {
return placeResult;
}
}
// config not found
return null;
}
private async loadSearchPlace(
dir: string,
place: string,
): Promise<CosmiconfigResult> {
const filepath = path.join(dir, place);
const fileContents = await readFile(filepath);
const result = await this.createCosmiconfigResult(filepath, fileContents);
return result;
}
private async loadFileContent(
filepath: string,
content: string | null,
): Promise<LoadedFileContent> {
if (content === null) {
return null;
}
if (content.trim() === '') {
return undefined;
}
const loader = this.getLoaderEntryForFile(filepath);
const loaderResult = await loader(filepath, content);
return loaderResult;
}
private async createCosmiconfigResult(
filepath: string,
content: string | null,
): Promise<CosmiconfigResult> {
const fileContent = await this.loadFileContent(filepath, content);
const result = this.loadedContentToCosmiconfigResult(filepath, fileContent);
return result;
}
public async load(filepath: string): Promise<CosmiconfigResult> {
this.validateFilePath(filepath);
const absoluteFilePath = path.resolve(process.cwd(), filepath);
const runLoad = async (): Promise<CosmiconfigResult> => {
const fileContents = await readFile(absoluteFilePath, {
throwNotFound: true,
});
const result = await this.createCosmiconfigResult(
absoluteFilePath,
fileContents,
);
const transformResult = await this.config.transform(result);
return transformResult;
};
if (this.loadCache) {
return cacheWrapper(this.loadCache, absoluteFilePath, runLoad);
}
return runLoad();
}
}
export { Explorer };