马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Explorer = void 0;
const promises_1 = __importDefault(require("fs/promises"));
const path_1 = __importDefault(require("path"));
const defaults_1 = require("./defaults");
const ExplorerBase_js_1 = require("./ExplorerBase.js");
const merge_1 = require("./merge");
const util_js_1 = require("./util.js");
/**
 * @internal
 */
class Explorer extends ExplorerBase_js_1.ExplorerBase {
    async load(filepath) {
        filepath = path_1.default.resolve(filepath);
        const load = async () => {
            return await this.config.transform(await this.#readConfiguration(filepath));
        };
        if (this.loadCache) {
            return await (0, util_js_1.emplace)(this.loadCache, filepath, load);
        }
        return await load();
    }
    async search(from = '') {
        if (this.config.metaConfigFilePath) {
            this.loadingMetaConfig = true;
            const config = await this.load(this.config.metaConfigFilePath);
            this.loadingMetaConfig = false;
            if (config && !config.isEmpty) {
                return config;
            }
        }
        from = path_1.default.resolve(from);
        const dirs = this.#getDirs(from);
        const firstDirIter = await dirs.next();
        /* istanbul ignore if -- @preserve */
        if (firstDirIter.done) {
            // this should never happen
            throw new Error(`Could not find any folders to iterate through (start from ${from})`);
        }
        let currentDir = firstDirIter.value;
        const search = async () => {
            /* istanbul ignore if -- @preserve */
            if (await (0, util_js_1.isDirectory)(currentDir.path)) {
                for (const filepath of this.getSearchPlacesForDir(currentDir, defaults_1.globalConfigSearchPlaces)) {
                    try {
                        const result = await this.#readConfiguration(filepath);
                        if (result !== null &&
                            !(result.isEmpty && this.config.ignoreEmptySearchPlaces)) {
                            return await this.config.transform(result);
                        }
                    }
                    catch (error) {
                        if (error.code === 'ENOENT' ||
                            error.code === 'EISDIR' ||
                            error.code === 'ENOTDIR' ||
                            error.code === 'EACCES') {
                            continue;
                        }
                        throw error;
                    }
                }
            }
            const nextDirIter = await dirs.next();
            if (!nextDirIter.done) {
                currentDir = nextDirIter.value;
                if (this.searchCache) {
                    return await (0, util_js_1.emplace)(this.searchCache, currentDir.path, search);
                }
                return await search();
            }
            return await this.config.transform(null);
        };
        if (this.searchCache) {
            return await (0, util_js_1.emplace)(this.searchCache, from, search);
        }
        return await search();
    }
    async #readConfiguration(filepath, importStack = []) {
        const contents = await promises_1.default.readFile(filepath, { encoding: 'utf-8' });
        return this.toCosmiconfigResult(filepath, await this.#loadConfigFileWithImports(filepath, contents, importStack));
    }
    async #loadConfigFileWithImports(filepath, contents, importStack) {
        const loadedContent = await this.#loadConfiguration(filepath, contents);
        if (!loadedContent || !(0, merge_1.hasOwn)(loadedContent, '$import')) {
            return loadedContent;
        }
        const fileDirectory = path_1.default.dirname(filepath);
        const { $import: imports, ...ownContent } = loadedContent;
        const importPaths = Array.isArray(imports) ? imports : [imports];
        const newImportStack = [...importStack, filepath];
        this.validateImports(filepath, importPaths, newImportStack);
        const importedConfigs = await Promise.all(importPaths.map(async (importPath) => {
            const fullPath = path_1.default.resolve(fileDirectory, importPath);
            const result = await this.#readConfiguration(fullPath, newImportStack);
            return result?.config;
        }));
        return (0, merge_1.mergeAll)([...importedConfigs, ownContent], {
            mergeArrays: this.config.mergeImportArrays,
        });
    }
    async #loadConfiguration(filepath, contents) {
        if (contents.trim() === '') {
            return;
        }
        const extension = path_1.default.extname(filepath);
        const loader = this.config.loaders[extension || 'noExt'] ??
            this.config.loaders['default'];
        if (!loader) {
            throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`);
        }
        try {
            const loadedContents = await loader(filepath, contents);
            if (path_1.default.basename(filepath, extension) !== 'package') {
                return loadedContents;
            }
            return ((0, util_js_1.getPropertyByPath)(loadedContents, this.config.packageProp ?? this.config.moduleName) ?? null);
        }
        catch (error) {
            error.filepath = filepath;
            throw error;
        }
    }
    async #fileExists(path) {
        try {
            await promises_1.default.stat(path);
            return true;
        }
        catch (e) {
            return false;
        }
    }
    async *#getDirs(startDir) {
        switch (this.config.searchStrategy) {
            case 'none': {
                // only check in the passed directory (defaults to working directory)
                yield { path: startDir, isGlobalConfig: false };
                return;
            }
            case 'project': {
                let currentDir = startDir;
                while (true) {
                    yield { path: currentDir, isGlobalConfig: false };
                    for (const ext of ['json', 'yaml']) {
                        const packageFile = path_1.default.join(currentDir, `package.${ext}`);
                        if (await this.#fileExists(packageFile)) {
                            break;
                        }
                    }
                    const parentDir = path_1.default.dirname(currentDir);
                    /* istanbul ignore if -- @preserve */
                    if (parentDir === currentDir) {
                        // we're probably at the root of the directory structure
                        break;
                    }
                    currentDir = parentDir;
                }
                return;
            }
            case 'global': {
                yield* this.getGlobalDirs(startDir);
            }
        }
    }
}
exports.Explorer = Explorer;
//# sourceMappingURL=Explorer.js.map