@markuplint/file-resolver 3.15.0 → 4.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/lib/auto-load-rules.d.ts +2 -2
- package/lib/auto-load-rules.js +12 -39
- package/lib/config-load-error.d.ts +6 -0
- package/lib/config-load-error.js +8 -0
- package/lib/config-provider.d.ts +14 -11
- package/lib/config-provider.js +146 -198
- package/lib/cosmiconfig.d.ts +8 -13
- package/lib/cosmiconfig.js +25 -33
- package/lib/debug.d.ts +3 -0
- package/lib/debug.js +2 -0
- package/lib/force-import-json-in-module.d.ts +1 -0
- package/lib/force-import-json-in-module.js +31 -0
- package/lib/general-import.d.ts +1 -0
- package/lib/general-import.js +40 -0
- package/lib/get-preset.d.ts +2 -0
- package/lib/get-preset.js +13 -0
- package/lib/index.d.ts +7 -7
- package/lib/index.js +6 -9
- package/lib/is-plugin-module-name.d.ts +1 -0
- package/lib/is-plugin-module-name.js +3 -0
- package/lib/is-preset-module-name.d.ts +1 -0
- package/lib/is-preset-module-name.js +3 -0
- package/lib/ml-file/get-anonymous-file.d.ts +1 -1
- package/lib/ml-file/get-anonymous-file.js +3 -7
- package/lib/ml-file/get-file.d.ts +1 -1
- package/lib/ml-file/get-file.js +3 -7
- package/lib/ml-file/get-files.d.ts +2 -2
- package/lib/ml-file/get-files.js +7 -9
- package/lib/ml-file/index.d.ts +4 -4
- package/lib/ml-file/index.js +3 -6
- package/lib/ml-file/ml-file.d.ts +22 -22
- package/lib/ml-file/ml-file.js +54 -48
- package/lib/module-exists.d.ts +1 -0
- package/lib/module-exists.js +49 -0
- package/lib/path-to-abs-or-name.d.ts +1 -0
- package/lib/path-to-abs-or-name.js +38 -0
- package/lib/resolve-files.d.ts +3 -3
- package/lib/resolve-files.js +4 -8
- package/lib/resolve-name-or-abs-path.d.ts +1 -0
- package/lib/resolve-name-or-abs-path.js +14 -0
- package/lib/resolve-parser.d.ts +7 -11
- package/lib/resolve-parser.js +16 -12
- package/lib/resolve-plugins.js +24 -56
- package/lib/resolve-rules.d.ts +5 -9
- package/lib/resolve-rules.js +18 -44
- package/lib/resolve-specs.d.ts +2 -5
- package/lib/resolve-specs.js +10 -11
- package/lib/types.d.ts +18 -20
- package/lib/types.js +1 -2
- package/lib/utils.js +6 -13
- package/package.json +22 -18
package/LICENSE
CHANGED
package/lib/auto-load-rules.d.ts
CHANGED
package/lib/auto-load-rules.js
CHANGED
|
@@ -1,35 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.autoLoadRules = void 0;
|
|
27
|
-
const ml_core_1 = require("@markuplint/ml-core");
|
|
1
|
+
import { MLRule } from '@markuplint/ml-core';
|
|
28
2
|
const cache = new Map();
|
|
29
3
|
/**
|
|
30
4
|
* @deprecated
|
|
31
5
|
*/
|
|
32
|
-
async function autoLoadRules(ruleset) {
|
|
6
|
+
export async function autoLoadRules(ruleset) {
|
|
33
7
|
const rules = [];
|
|
34
8
|
const errors = [];
|
|
35
9
|
for (const ruleName of Object.keys(ruleset.rules)) {
|
|
@@ -40,17 +14,17 @@ async function autoLoadRules(ruleset) {
|
|
|
40
14
|
}
|
|
41
15
|
let seed = null;
|
|
42
16
|
try {
|
|
43
|
-
const _module = await
|
|
17
|
+
const _module = await import(`@markuplint/rule-${ruleName}`);
|
|
44
18
|
seed = _module.default;
|
|
45
19
|
if (!(seed && 'defaultValue' in seed && 'defaultOptions' in seed && 'verify' in seed)) {
|
|
46
20
|
seed = null;
|
|
47
21
|
}
|
|
48
22
|
}
|
|
49
|
-
catch (
|
|
50
|
-
errors.push(
|
|
23
|
+
catch (error) {
|
|
24
|
+
errors.push(error);
|
|
51
25
|
}
|
|
52
26
|
if (seed) {
|
|
53
|
-
const rule = new
|
|
27
|
+
const rule = new MLRule({
|
|
54
28
|
name: ruleName,
|
|
55
29
|
...seed,
|
|
56
30
|
});
|
|
@@ -59,17 +33,17 @@ async function autoLoadRules(ruleset) {
|
|
|
59
33
|
continue;
|
|
60
34
|
}
|
|
61
35
|
try {
|
|
62
|
-
const _module = await
|
|
36
|
+
const _module = await import(`markuplint-rule-${ruleName}`);
|
|
63
37
|
seed = _module.default;
|
|
64
38
|
if (!(seed && 'defaultValue' in seed && 'defaultOptions' in seed && 'verify' in seed)) {
|
|
65
39
|
seed = null;
|
|
66
40
|
}
|
|
67
41
|
}
|
|
68
|
-
catch (
|
|
69
|
-
errors.push(
|
|
42
|
+
catch (error) {
|
|
43
|
+
errors.push(error);
|
|
70
44
|
}
|
|
71
45
|
if (seed) {
|
|
72
|
-
const rule = new
|
|
46
|
+
const rule = new MLRule({
|
|
73
47
|
name: ruleName,
|
|
74
48
|
...seed,
|
|
75
49
|
});
|
|
@@ -80,9 +54,8 @@ async function autoLoadRules(ruleset) {
|
|
|
80
54
|
}
|
|
81
55
|
return {
|
|
82
56
|
// Clone
|
|
83
|
-
rules: rules
|
|
57
|
+
rules: [...rules],
|
|
84
58
|
// Clone
|
|
85
|
-
errors: errors
|
|
59
|
+
errors: [...errors],
|
|
86
60
|
};
|
|
87
61
|
}
|
|
88
|
-
exports.autoLoadRules = autoLoadRules;
|
package/lib/config-provider.d.ts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import type { MLFile } from './ml-file';
|
|
2
|
-
import type { ConfigSet } from './types';
|
|
1
|
+
import type { MLFile } from './ml-file/index.js';
|
|
2
|
+
import type { ConfigSet } from './types.js';
|
|
3
3
|
import type { Config } from '@markuplint/ml-config';
|
|
4
4
|
import type { Nullable } from '@markuplint/shared';
|
|
5
5
|
export declare class ConfigProvider {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
#private;
|
|
7
|
+
recursiveLoad(key: string, cache: boolean, referrer: string, depth?: number): Promise<{
|
|
8
|
+
stack: Set<string>;
|
|
9
|
+
errs: Error[];
|
|
10
|
+
}>;
|
|
11
|
+
resolve(targetFile: Readonly<MLFile>, names: readonly Nullable<string>[], cache?: boolean): Promise<ConfigSet>;
|
|
12
|
+
search(targetFile: Readonly<MLFile>): Promise<string | null>;
|
|
13
|
+
set(config: Config, key?: string): string;
|
|
14
|
+
private _load;
|
|
15
|
+
private _mergeConfigs;
|
|
16
|
+
private _pathResolve;
|
|
17
|
+
private _validateConfig;
|
|
15
18
|
}
|
package/lib/config-provider.js
CHANGED
|
@@ -1,56 +1,102 @@
|
|
|
1
|
-
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
2
6
|
var _ConfigProvider_cache, _ConfigProvider_held, _ConfigProvider_recursiveLoadKeyAndDepth, _ConfigProvider_store;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { mergeConfig } from '@markuplint/ml-config';
|
|
9
|
+
import { ConfigParserError } from '@markuplint/parser-utils';
|
|
10
|
+
import { InvalidSelectorError, createSelector } from '@markuplint/selector';
|
|
11
|
+
import { nonNullableFilter, toNoEmptyStringArrayFromStringOrArray } from '@markuplint/shared';
|
|
12
|
+
import { ConfigLoadError } from './config-load-error.js';
|
|
13
|
+
import { load as loadConfig, search } from './cosmiconfig.js';
|
|
14
|
+
import { log } from './debug.js';
|
|
15
|
+
import { generalImport } from './general-import.js';
|
|
16
|
+
import { getPreset } from './get-preset.js';
|
|
17
|
+
import { isPluginModuleName } from './is-plugin-module-name.js';
|
|
18
|
+
import { isPresetModuleName } from './is-preset-module-name.js';
|
|
19
|
+
import { moduleExists } from './module-exists.js';
|
|
20
|
+
import { relPathToNameOrAbsPath } from './path-to-abs-or-name.js';
|
|
21
|
+
import { cacheClear, resolvePlugins } from './resolve-plugins.js';
|
|
22
|
+
import { fileExists, uuid } from './utils.js';
|
|
23
|
+
const cpLog = log.extend('config-provider');
|
|
15
24
|
const KEY_SEPARATOR = '__ML_CONFIG_MERGE__';
|
|
16
|
-
class ConfigProvider {
|
|
25
|
+
export class ConfigProvider {
|
|
17
26
|
constructor() {
|
|
18
27
|
_ConfigProvider_cache.set(this, new Map());
|
|
19
28
|
_ConfigProvider_held.set(this, new Set());
|
|
20
29
|
_ConfigProvider_recursiveLoadKeyAndDepth.set(this, new Map());
|
|
21
30
|
_ConfigProvider_store.set(this, new Map());
|
|
22
31
|
}
|
|
32
|
+
async recursiveLoad(key, cache, referrer, depth = 1) {
|
|
33
|
+
const stack = new Set();
|
|
34
|
+
const errs = [];
|
|
35
|
+
const ancestorDepth = __classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
|
|
36
|
+
if (ancestorDepth != null && ancestorDepth < depth) {
|
|
37
|
+
return {
|
|
38
|
+
stack,
|
|
39
|
+
errs: [new CircularReferenceError(`Circular reference detected: ${key}`)],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
|
|
43
|
+
let config = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key);
|
|
44
|
+
if (!config) {
|
|
45
|
+
config = await this._load(key, cache, referrer);
|
|
46
|
+
}
|
|
47
|
+
if (!config) {
|
|
48
|
+
return { stack, errs: [] };
|
|
49
|
+
}
|
|
50
|
+
if (config instanceof ConfigLoadError) {
|
|
51
|
+
stack.add(config.filePath);
|
|
52
|
+
return {
|
|
53
|
+
stack,
|
|
54
|
+
errs: [config],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const depKeys = config.extends === null ? null : toNoEmptyStringArrayFromStringOrArray(config.extends);
|
|
58
|
+
if (depKeys) {
|
|
59
|
+
for (const depKey of depKeys) {
|
|
60
|
+
const keys = await this.recursiveLoad(depKey, cache, key, depth + 1);
|
|
61
|
+
for (const key of keys.stack) {
|
|
62
|
+
stack.add(key);
|
|
63
|
+
}
|
|
64
|
+
errs.push(...keys.errs);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
stack.add(key);
|
|
68
|
+
return { stack, errs };
|
|
69
|
+
}
|
|
23
70
|
async resolve(targetFile, names, cache = true) {
|
|
24
|
-
var _a, _b;
|
|
25
71
|
if (!cache) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
72
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").clear();
|
|
73
|
+
__classPrivateFieldGet(this, _ConfigProvider_cache, "f").clear();
|
|
74
|
+
cacheClear();
|
|
29
75
|
}
|
|
30
|
-
const keys = names.filter(
|
|
76
|
+
const keys = names.filter(nonNullableFilter);
|
|
31
77
|
const key = keys.join(KEY_SEPARATOR);
|
|
32
|
-
const currentConfig =
|
|
78
|
+
const currentConfig = __classPrivateFieldGet(this, _ConfigProvider_cache, "f").get(key);
|
|
33
79
|
if (currentConfig) {
|
|
34
80
|
return currentConfig;
|
|
35
81
|
}
|
|
36
|
-
let configSet = await this._mergeConfigs(keys, cache);
|
|
37
|
-
const filePath =
|
|
82
|
+
let configSet = await this._mergeConfigs(keys, cache, targetFile.path);
|
|
83
|
+
const filePath = [...configSet.files].reverse()[0];
|
|
38
84
|
if (!filePath) {
|
|
39
|
-
throw new
|
|
85
|
+
throw new ConfigParserError('Config file not found', {
|
|
40
86
|
filePath: targetFile.path,
|
|
41
87
|
});
|
|
42
88
|
}
|
|
43
89
|
const errors = this._validateConfig(configSet.config, filePath);
|
|
44
90
|
configSet.errs.push(...errors);
|
|
45
|
-
const plugins = await
|
|
46
|
-
if (
|
|
47
|
-
const extendHelds =
|
|
91
|
+
const plugins = await resolvePlugins(configSet.config.plugins);
|
|
92
|
+
if (__classPrivateFieldGet(this, _ConfigProvider_held, "f").size > 0) {
|
|
93
|
+
const extendHelds = [...__classPrivateFieldGet(this, _ConfigProvider_held, "f").values()];
|
|
48
94
|
for (const held of extendHelds) {
|
|
49
|
-
const [, prefix, namespace, name] =
|
|
95
|
+
const [, prefix, namespace, name] = held.match(/^([a-z]+:)([^/]+)(?:\/(.+))?$/) ?? [];
|
|
50
96
|
switch (prefix) {
|
|
51
97
|
case 'plugin:': {
|
|
52
98
|
const plugin = plugins.find(plugin => plugin.name === namespace);
|
|
53
|
-
const config =
|
|
99
|
+
const config = plugin?.configs?.[name ?? ''];
|
|
54
100
|
if (config) {
|
|
55
101
|
this.set(config, held);
|
|
56
102
|
}
|
|
@@ -58,8 +104,8 @@ class ConfigProvider {
|
|
|
58
104
|
}
|
|
59
105
|
}
|
|
60
106
|
}
|
|
61
|
-
configSet = await this._mergeConfigs([...keys, ...extendHelds], cache);
|
|
62
|
-
|
|
107
|
+
configSet = await this._mergeConfigs([...keys, ...extendHelds], cache, targetFile.path);
|
|
108
|
+
__classPrivateFieldGet(this, _ConfigProvider_held, "f").clear();
|
|
63
109
|
}
|
|
64
110
|
// Resolves `overrides`
|
|
65
111
|
if (configSet.config.overrides) {
|
|
@@ -78,74 +124,78 @@ class ConfigProvider {
|
|
|
78
124
|
...configSet,
|
|
79
125
|
plugins,
|
|
80
126
|
};
|
|
81
|
-
|
|
127
|
+
__classPrivateFieldGet(this, _ConfigProvider_cache, "f").set(key, result);
|
|
82
128
|
return result;
|
|
83
129
|
}
|
|
84
130
|
async search(targetFile) {
|
|
85
|
-
|
|
131
|
+
const isExists = await targetFile.dirExists();
|
|
132
|
+
cpLog('search: %s', targetFile.path);
|
|
133
|
+
cpLog('isExists: %s', isExists);
|
|
134
|
+
if (!isExists) {
|
|
86
135
|
return null;
|
|
87
136
|
}
|
|
88
|
-
const res = await
|
|
137
|
+
const res = await search(targetFile.path, false);
|
|
138
|
+
cpLog('searched config: %O', res);
|
|
89
139
|
if (!res) {
|
|
90
140
|
return null;
|
|
91
141
|
}
|
|
92
142
|
const { filePath, config } = res;
|
|
93
|
-
const pathResolvedConfig = this._pathResolve(config, filePath);
|
|
94
|
-
|
|
143
|
+
const pathResolvedConfig = await this._pathResolve(config, filePath);
|
|
144
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
|
|
145
|
+
cpLog('Store key: %s', filePath);
|
|
95
146
|
return filePath;
|
|
96
147
|
}
|
|
97
148
|
set(config, key) {
|
|
98
|
-
key = key
|
|
99
|
-
|
|
149
|
+
key = key ?? uuid();
|
|
150
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(key, config);
|
|
100
151
|
return key;
|
|
101
152
|
}
|
|
102
|
-
async _load(filePath, cache) {
|
|
103
|
-
|
|
104
|
-
const entity = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
|
|
153
|
+
async _load(filePath, cache, referrer) {
|
|
154
|
+
const entity = __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(filePath);
|
|
105
155
|
if (entity) {
|
|
106
156
|
return entity;
|
|
107
157
|
}
|
|
108
|
-
if (
|
|
109
|
-
const [, name] =
|
|
110
|
-
const config = await
|
|
111
|
-
const pathResolvedConfig = this._pathResolve(config, filePath);
|
|
112
|
-
|
|
158
|
+
if (isPresetModuleName(filePath)) {
|
|
159
|
+
const [, name] = filePath.match(/^markuplint:(.+)$/i) ?? [];
|
|
160
|
+
const config = await getPreset(name ?? filePath);
|
|
161
|
+
const pathResolvedConfig = await this._pathResolve(config, filePath);
|
|
162
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
|
|
113
163
|
return pathResolvedConfig;
|
|
114
164
|
}
|
|
115
|
-
if (
|
|
116
|
-
|
|
117
|
-
return
|
|
165
|
+
if (isPluginModuleName(filePath)) {
|
|
166
|
+
__classPrivateFieldGet(this, _ConfigProvider_held, "f").add(filePath);
|
|
167
|
+
return;
|
|
118
168
|
}
|
|
119
|
-
if (!moduleExists(filePath) && !
|
|
169
|
+
if (!(await moduleExists(filePath)) && !path.isAbsolute(filePath)) {
|
|
120
170
|
throw new TypeError(`${filePath} is not an absolute path`);
|
|
121
171
|
}
|
|
122
|
-
const config = await load(filePath, cache);
|
|
123
|
-
if (
|
|
124
|
-
return
|
|
172
|
+
const config = await load(filePath, cache, referrer);
|
|
173
|
+
if (config instanceof ConfigLoadError) {
|
|
174
|
+
return config;
|
|
125
175
|
}
|
|
126
|
-
const pathResolvedConfig = this._pathResolve(config, filePath);
|
|
127
|
-
|
|
176
|
+
const pathResolvedConfig = await this._pathResolve(config, filePath);
|
|
177
|
+
__classPrivateFieldGet(this, _ConfigProvider_store, "f").set(filePath, pathResolvedConfig);
|
|
128
178
|
return pathResolvedConfig;
|
|
129
179
|
}
|
|
130
|
-
async _mergeConfigs(keys, cache) {
|
|
180
|
+
async _mergeConfigs(keys, cache, referrer) {
|
|
131
181
|
const resolvedKeys = new Set();
|
|
132
182
|
const errs = [];
|
|
133
183
|
for (const key of keys) {
|
|
134
|
-
|
|
135
|
-
const keySet = await this.
|
|
184
|
+
__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").clear();
|
|
185
|
+
const keySet = await this.recursiveLoad(key, cache, referrer);
|
|
136
186
|
for (const k of keySet.stack) {
|
|
137
187
|
resolvedKeys.add(k);
|
|
138
188
|
}
|
|
139
|
-
|
|
140
|
-
errs.push(...keySet.errs);
|
|
141
|
-
}
|
|
189
|
+
errs.push(...keySet.errs);
|
|
142
190
|
}
|
|
143
|
-
const configs =
|
|
144
|
-
.map(name => tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name))
|
|
145
|
-
.filter(shared_1.nonNullableFilter);
|
|
191
|
+
const configs = [...resolvedKeys].map(name => __classPrivateFieldGet(this, _ConfigProvider_store, "f").get(name)).filter(nonNullableFilter);
|
|
146
192
|
let resultConfig = {};
|
|
147
193
|
for (const config of configs) {
|
|
148
|
-
|
|
194
|
+
if (config instanceof ConfigLoadError) {
|
|
195
|
+
errs.push(config);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
resultConfig = mergeConfig(resultConfig, config);
|
|
149
199
|
}
|
|
150
200
|
return {
|
|
151
201
|
config: resultConfig,
|
|
@@ -153,157 +203,55 @@ class ConfigProvider {
|
|
|
153
203
|
errs,
|
|
154
204
|
};
|
|
155
205
|
}
|
|
156
|
-
_pathResolve(config, filePath) {
|
|
157
|
-
const dir =
|
|
206
|
+
async _pathResolve(config, filePath) {
|
|
207
|
+
const dir = path.dirname(filePath);
|
|
158
208
|
return {
|
|
159
209
|
...config,
|
|
160
|
-
extends:
|
|
161
|
-
plugins:
|
|
162
|
-
parser:
|
|
163
|
-
specs:
|
|
164
|
-
excludeFiles:
|
|
165
|
-
overrides:
|
|
210
|
+
extends: await relPathToNameOrAbsPath(dir, config.extends),
|
|
211
|
+
plugins: await relPathToNameOrAbsPath(dir, config.plugins, ['name']),
|
|
212
|
+
parser: await relPathToNameOrAbsPath(dir, config.parser),
|
|
213
|
+
specs: await relPathToNameOrAbsPath(dir, config.specs),
|
|
214
|
+
excludeFiles: await relPathToNameOrAbsPath(dir, config.excludeFiles),
|
|
215
|
+
overrides: await relPathToNameOrAbsPath(dir, config.overrides, undefined, true),
|
|
166
216
|
};
|
|
167
217
|
}
|
|
168
|
-
async _recursiveLoad(key, cache, depth = 1) {
|
|
169
|
-
var _a;
|
|
170
|
-
const stack = new Set();
|
|
171
|
-
const errs = [];
|
|
172
|
-
const ancestorDepth = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").get(key);
|
|
173
|
-
if (ancestorDepth != null && ancestorDepth < depth) {
|
|
174
|
-
return {
|
|
175
|
-
stack,
|
|
176
|
-
errs: [new CircularReferenceError(`Circular reference detected: ${key}`)],
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
tslib_1.__classPrivateFieldGet(this, _ConfigProvider_recursiveLoadKeyAndDepth, "f").set(key, depth);
|
|
180
|
-
let config = (_a = tslib_1.__classPrivateFieldGet(this, _ConfigProvider_store, "f").get(key)) !== null && _a !== void 0 ? _a : null;
|
|
181
|
-
if (!config) {
|
|
182
|
-
config = await this._load(key, cache);
|
|
183
|
-
}
|
|
184
|
-
if (!config) {
|
|
185
|
-
return { stack, errs: null };
|
|
186
|
-
}
|
|
187
|
-
const depKeys = config.extends !== null ? (0, shared_1.toNoEmptyStringArrayFromStringOrArray)(config.extends) : null;
|
|
188
|
-
if (depKeys) {
|
|
189
|
-
for (const depKey of depKeys) {
|
|
190
|
-
const keys = await this._recursiveLoad(depKey, cache, depth + 1);
|
|
191
|
-
for (const key of keys.stack) {
|
|
192
|
-
stack.add(key);
|
|
193
|
-
}
|
|
194
|
-
if (keys.errs) {
|
|
195
|
-
errs.push(...keys.errs);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
stack.add(key);
|
|
200
|
-
return { stack, errs };
|
|
201
|
-
}
|
|
202
218
|
_validateConfig(config, filePath) {
|
|
203
|
-
var _a;
|
|
204
219
|
const errors = [];
|
|
205
|
-
(
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
220
|
+
if (config.nodeRules)
|
|
221
|
+
for (const rule of config.nodeRules) {
|
|
222
|
+
if (rule.selector) {
|
|
223
|
+
try {
|
|
224
|
+
createSelector(rule.selector);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
if (error instanceof InvalidSelectorError) {
|
|
228
|
+
errors.push(new ConfigParserError(error.message, {
|
|
229
|
+
filePath,
|
|
230
|
+
raw: rule.selector,
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
216
233
|
}
|
|
217
234
|
}
|
|
218
235
|
}
|
|
219
|
-
});
|
|
220
236
|
return errors;
|
|
221
237
|
}
|
|
222
238
|
}
|
|
223
|
-
exports.ConfigProvider = ConfigProvider;
|
|
224
239
|
_ConfigProvider_cache = new WeakMap(), _ConfigProvider_held = new WeakMap(), _ConfigProvider_recursiveLoadKeyAndDepth = new WeakMap(), _ConfigProvider_store = new WeakMap();
|
|
225
|
-
async function load(filePath, cache) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const mod = await Promise.resolve(`${filePath}`).then(s => tslib_1.__importStar(require(s)));
|
|
229
|
-
const config = (_a = mod === null || mod === void 0 ? void 0 : mod.default) !== null && _a !== void 0 ? _a : null;
|
|
240
|
+
async function load(filePath, cache, referrer) {
|
|
241
|
+
if (!fileExists(filePath) && (await moduleExists(filePath))) {
|
|
242
|
+
const config = (await generalImport(filePath)) ?? new ConfigLoadError('Module is not found', filePath, referrer);
|
|
230
243
|
return config;
|
|
231
244
|
}
|
|
232
|
-
const res = await (
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
return res.config;
|
|
237
|
-
}
|
|
238
|
-
function pathResolve(dir, filePath, resolveProps, resolveKey = false) {
|
|
239
|
-
if (filePath == null) {
|
|
240
|
-
// @ts-ignore
|
|
241
|
-
return undefined;
|
|
242
|
-
}
|
|
243
|
-
if (typeof filePath === 'string') {
|
|
244
|
-
// @ts-ignore
|
|
245
|
-
return resolve(dir, filePath);
|
|
246
|
-
}
|
|
247
|
-
if (Array.isArray(filePath)) {
|
|
248
|
-
// @ts-ignore
|
|
249
|
-
return filePath.map(fp => pathResolve(dir, fp, resolveProps));
|
|
250
|
-
}
|
|
251
|
-
const res = {};
|
|
252
|
-
for (const [key, fp] of Object.entries(filePath)) {
|
|
253
|
-
let _key = key;
|
|
254
|
-
if (resolveKey) {
|
|
255
|
-
_key = resolve(dir, key);
|
|
256
|
-
}
|
|
257
|
-
if (typeof fp === 'string') {
|
|
258
|
-
if (!resolveProps) {
|
|
259
|
-
res[_key] = resolve(dir, fp);
|
|
260
|
-
}
|
|
261
|
-
else if (resolveProps.includes(key)) {
|
|
262
|
-
res[_key] = resolve(dir, fp);
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
res[_key] = fp;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
res[_key] = fp;
|
|
245
|
+
const res = await loadConfig(filePath, !cache, referrer).catch((error) => {
|
|
246
|
+
if (error instanceof ConfigLoadError) {
|
|
247
|
+
return error;
|
|
270
248
|
}
|
|
249
|
+
throw error;
|
|
250
|
+
});
|
|
251
|
+
if (res instanceof ConfigLoadError) {
|
|
252
|
+
return res;
|
|
271
253
|
}
|
|
272
|
-
|
|
273
|
-
return res;
|
|
274
|
-
}
|
|
275
|
-
function resolve(dir, pathOrModName) {
|
|
276
|
-
var _a, _b, _c;
|
|
277
|
-
if (moduleExists(pathOrModName) || isPreset(pathOrModName) || isPlugin(pathOrModName)) {
|
|
278
|
-
return pathOrModName;
|
|
279
|
-
}
|
|
280
|
-
const bangAndPath = (_a = /^(!)(.*)/.exec(pathOrModName)) !== null && _a !== void 0 ? _a : [];
|
|
281
|
-
const bang = (_b = bangAndPath[1]) !== null && _b !== void 0 ? _b : '';
|
|
282
|
-
const pathname = (_c = bangAndPath[2]) !== null && _c !== void 0 ? _c : pathOrModName;
|
|
283
|
-
const absPath = path_1.default.resolve(dir, pathname);
|
|
284
|
-
return bang + absPath;
|
|
285
|
-
}
|
|
286
|
-
function moduleExists(name) {
|
|
287
|
-
try {
|
|
288
|
-
require.resolve(name);
|
|
289
|
-
}
|
|
290
|
-
catch (err) {
|
|
291
|
-
if (
|
|
292
|
-
// @ts-ignore
|
|
293
|
-
'code' in err &&
|
|
294
|
-
// @ts-ignore
|
|
295
|
-
err.code === 'MODULE_NOT_FOUND') {
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
throw err;
|
|
299
|
-
}
|
|
300
|
-
return true;
|
|
301
|
-
}
|
|
302
|
-
function isPreset(name) {
|
|
303
|
-
return /^markuplint:/i.test(name);
|
|
304
|
-
}
|
|
305
|
-
function isPlugin(name) {
|
|
306
|
-
return /^plugin:/i.test(name);
|
|
254
|
+
return res.config;
|
|
307
255
|
}
|
|
308
256
|
class CircularReferenceError extends ReferenceError {
|
|
309
257
|
constructor() {
|
package/lib/cosmiconfig.d.ts
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import type { LoaderSync } from 'cosmiconfig';
|
|
2
|
+
import { ConfigLoadError } from './config-load-error.js';
|
|
2
3
|
type CosmiConfig = ReturnType<LoaderSync>;
|
|
3
|
-
export declare function search<T = CosmiConfig>(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
): Promise<{
|
|
7
|
-
filePath: string;
|
|
8
|
-
config: T;
|
|
9
|
-
} | null>;
|
|
10
|
-
export declare function load<T = CosmiConfig>(
|
|
11
|
-
filePath: string,
|
|
12
|
-
cacheClear: boolean,
|
|
13
|
-
): Promise<{
|
|
14
|
-
filePath: string;
|
|
15
|
-
config: T;
|
|
4
|
+
export declare function search<T = CosmiConfig>(filePath: string, cacheClear: boolean): Promise<{
|
|
5
|
+
filePath: string;
|
|
6
|
+
config: T;
|
|
16
7
|
} | null>;
|
|
8
|
+
export declare function load<T = CosmiConfig>(filePath: string, cacheClear: boolean, referrer: string): Promise<ConfigLoadError | {
|
|
9
|
+
filePath: string;
|
|
10
|
+
config: T;
|
|
11
|
+
}>;
|
|
17
12
|
export {};
|