-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to load config with child configs
- Loading branch information
Showing
4 changed files
with
310 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { readdir } from 'fs/promises'; | ||
import { join, sep } from 'path'; | ||
|
||
/** | ||
* @typedef {import('eslint').Linter.Config} Config | ||
* @typedef {{ path: string; config: Config }} ConfigFile | ||
*/ | ||
|
||
/** | ||
* Config file names for ESLint, that are supported by the config loading | ||
* functionality in this package. | ||
* | ||
* @type {string[]} | ||
*/ | ||
const ESLINT_CONFIG_NAMES = [ | ||
'eslint.config.js', | ||
'eslint.config.cjs', | ||
'eslint.config.mjs', | ||
]; | ||
|
||
/** | ||
* Get all ESLint config files in the workspace. | ||
* | ||
* @param {string} workspaceRoot - The absolute path to the root directory of | ||
* the workspace. | ||
* @returns {Promise<ConfigFile[]>} A promise that resolves to an array of | ||
* ESLint configs with their paths. | ||
*/ | ||
export async function getConfigFiles(workspaceRoot) { | ||
const files = await readdir(workspaceRoot, { | ||
recursive: true, | ||
withFileTypes: true, | ||
}); | ||
|
||
return files.reduce(async (promise, file) => { | ||
const accumulator = await promise; | ||
if (!file.isFile() || !ESLINT_CONFIG_NAMES.includes(file.name)) { | ||
return accumulator; | ||
} | ||
|
||
const segments = file.parentPath.split(sep); | ||
if (segments.includes('node_modules')) { | ||
return accumulator; | ||
} | ||
|
||
const path = join(file.parentPath, file.name); | ||
const config = await import(path); | ||
|
||
return [...accumulator, { path, config: config.default }]; | ||
}, Promise.resolve([])); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { getConfigFiles } from './file-system.mjs'; | ||
|
||
vi.mock('fs/promises', (importOriginal) => ({ | ||
...importOriginal, | ||
readdir: async () => [ | ||
{ isFile: () => true, parentPath: '/foo/bar', name: 'eslint.config.js' }, | ||
{ | ||
isFile: () => true, | ||
parentPath: '/foo/bar/baz', | ||
name: 'eslint.config.cjs', | ||
}, | ||
{ | ||
isFile: () => true, | ||
parentPath: '/foo/bar/baz/qux', | ||
name: 'eslint.config.mjs', | ||
}, | ||
{ | ||
isFile: () => true, | ||
parentPath: '/foo/bar/node_modules/package', | ||
name: 'eslint.config.js', | ||
}, | ||
{ | ||
isFile: () => false, | ||
parentPath: '/not/a/file', | ||
name: 'eslint.config.js', | ||
}, | ||
], | ||
})); | ||
|
||
vi.mock('/foo/bar/eslint.config.js', () => ({ | ||
default: { | ||
rules: { | ||
'no-console': 'error', | ||
}, | ||
}, | ||
})); | ||
|
||
vi.mock('/foo/bar/baz/eslint.config.cjs', () => ({ | ||
default: { | ||
rules: { | ||
'no-console': 'warn', | ||
}, | ||
}, | ||
})); | ||
|
||
vi.mock('/foo/bar/baz/qux/eslint.config.mjs', () => ({ | ||
default: { | ||
rules: { | ||
'no-console': 'off', | ||
}, | ||
}, | ||
})); | ||
|
||
describe('getConfigFiles', () => { | ||
it('returns an array of ESLint config files with their paths', async () => { | ||
const workspaceRoot = '/foo/bar'; | ||
const configFiles = await getConfigFiles(workspaceRoot); | ||
|
||
expect(configFiles).toStrictEqual([ | ||
{ | ||
path: '/foo/bar/eslint.config.js', | ||
config: { | ||
rules: { | ||
'no-console': 'error', | ||
}, | ||
}, | ||
}, | ||
{ | ||
path: '/foo/bar/baz/eslint.config.cjs', | ||
config: { | ||
rules: { | ||
'no-console': 'warn', | ||
}, | ||
}, | ||
}, | ||
{ | ||
path: '/foo/bar/baz/qux/eslint.config.mjs', | ||
config: { | ||
rules: { | ||
'no-console': 'off', | ||
}, | ||
}, | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters