1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.ExplorerSync = void 0;
|
7 | const fs_1 = __importDefault(require("fs"));
|
8 | const path_1 = __importDefault(require("path"));
|
9 | const path_type_1 = require("path-type");
|
10 | const ExplorerBase_js_1 = require("./ExplorerBase.js");
|
11 | const loaders_js_1 = require("./loaders.js");
|
12 | const util_js_1 = require("./util.js");
|
13 |
|
14 |
|
15 |
|
16 | class ExplorerSync extends ExplorerBase_js_1.ExplorerBase {
|
17 | load(filepath) {
|
18 | filepath = path_1.default.resolve(filepath);
|
19 | const load = () => {
|
20 | return this.config.transform(this.#readConfiguration(filepath));
|
21 | };
|
22 | if (this.loadCache) {
|
23 | return (0, util_js_1.emplace)(this.loadCache, filepath, load);
|
24 | }
|
25 | return load();
|
26 | }
|
27 | search(from = '') {
|
28 | if (this.config.metaConfigFilePath) {
|
29 | this.loadingMetaConfig = true;
|
30 | const config = this.load(this.config.metaConfigFilePath);
|
31 | this.loadingMetaConfig = false;
|
32 | if (config && !config.isEmpty) {
|
33 | return config;
|
34 | }
|
35 | }
|
36 | const stopDir = path_1.default.resolve(this.config.stopDir);
|
37 | from = path_1.default.resolve(from);
|
38 | const search = () => {
|
39 |
|
40 | if ((0, path_type_1.isDirectorySync)(from)) {
|
41 | for (const place of this.config.searchPlaces) {
|
42 | const filepath = path_1.default.join(from, place);
|
43 | try {
|
44 | const result = this.#readConfiguration(filepath);
|
45 | if (result !== null &&
|
46 | !(result.isEmpty && this.config.ignoreEmptySearchPlaces)) {
|
47 | return this.config.transform(result);
|
48 | }
|
49 | }
|
50 | catch (error) {
|
51 | if (error.code === 'ENOENT' ||
|
52 | error.code === 'EISDIR' ||
|
53 | error.code === 'ENOTDIR') {
|
54 | continue;
|
55 | }
|
56 | throw error;
|
57 | }
|
58 | }
|
59 | }
|
60 | const dir = path_1.default.dirname(from);
|
61 | if (from !== stopDir && from !== dir) {
|
62 | from = dir;
|
63 | if (this.searchCache) {
|
64 | return (0, util_js_1.emplace)(this.searchCache, from, search);
|
65 | }
|
66 | return search();
|
67 | }
|
68 | return this.config.transform(null);
|
69 | };
|
70 | if (this.searchCache) {
|
71 | return (0, util_js_1.emplace)(this.searchCache, from, search);
|
72 | }
|
73 | return search();
|
74 | }
|
75 | #readConfiguration(filepath) {
|
76 | const contents = fs_1.default.readFileSync(filepath, 'utf8');
|
77 | return this.toCosmiconfigResult(filepath, this.#loadConfiguration(filepath, contents));
|
78 | }
|
79 | #loadConfiguration(filepath, contents) {
|
80 | if (contents.trim() === '') {
|
81 | return;
|
82 | }
|
83 | if (path_1.default.basename(filepath) === 'package.json') {
|
84 | return ((0, util_js_1.getPropertyByPath)((0, loaders_js_1.loadJson)(filepath, contents), this.config.packageProp) ?? null);
|
85 | }
|
86 | const extension = path_1.default.extname(filepath);
|
87 | try {
|
88 | const loader = this.config.loaders[extension || 'noExt'] ??
|
89 | this.config.loaders['default'];
|
90 | if (loader !== undefined) {
|
91 | return loader(filepath, contents);
|
92 | }
|
93 | }
|
94 | catch (error) {
|
95 | error.filepath = filepath;
|
96 | throw error;
|
97 | }
|
98 | throw new Error(`No loader specified for ${(0, ExplorerBase_js_1.getExtensionDescription)(extension)}`);
|
99 | }
|
100 | |
101 |
|
102 |
|
103 |
|
104 | loadSync(filepath) {
|
105 | return this.load(filepath);
|
106 | }
|
107 | |
108 |
|
109 |
|
110 |
|
111 | searchSync(from = '') {
|
112 | return this.search(from);
|
113 | }
|
114 | }
|
115 | exports.ExplorerSync = ExplorerSync;
|
116 |
|
\ | No newline at end of file |