forked from AmadeusITGroup/otter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eslint.config.mjs
61 lines (56 loc) · 1.9 KB
/
eslint.config.mjs
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
import {
dirname,
posix,
relative,
sep,
} from 'node:path';
import {
fileURLToPath,
pathToFileURL,
} from 'node:url';
import shared from './eslint.shared.config.mjs';
import {
sync,
} from 'globby';
const __filename = fileURLToPath(import.meta.url);
// __dirname is not defined in ES module scope
const __dirname = dirname(__filename);
/**
* Add a prefix to a path glob
* @param {string} prefix
* @param {string | undefined} pathGlob
* @returns {string}
*/
const addPrefix = (prefix, pathGlob = '**/*') => pathGlob.replace(/^(!?)(\.?\/)?/, `$1${prefix}/`).replaceAll(sep, posix.sep).replace(/^\//, '');
/**
* Merge ESLint config
* @param {string | string[]} globs List of globs to find ESLint config path
* @returns {Promise<import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray>}
*/
const mergeESLintConfigs = async (globs) => {
const localConfigFiles = sync(globs, { absolute: true });
/** @type {import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray} */
let localConfigs = [];
for (const localConfigFile of localConfigFiles) {
const module = await import(pathToFileURL(localConfigFile));
const moduleConfig = await (module.default ?? module);
/** @type {import('@typescript-eslint/utils').TSESLint.FlatConfig.ConfigArray} */
const configArray = Array.isArray(moduleConfig) ? moduleConfig : [moduleConfig];
const directory = relative(__dirname, dirname(localConfigFile));
localConfigs = localConfigs.concat(
configArray.map((config) => ({
...config,
...(
config.ignores
? { ignores: config.ignores.map((pathGlob) => addPrefix(directory, pathGlob)) }
: { files: (config.files || ['**/*']).flat().map((pathGlob) => addPrefix(directory, pathGlob)) }
)
}))
);
}
return [
...shared,
...localConfigs
];
};
export default mergeESLintConfigs('**/eslint.local.config.mjs');