-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple configuration files
- Loading branch information
Showing
19 changed files
with
748 additions
and
516 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
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,3 @@ | ||
import { pathToFileURL } from 'url' | ||
|
||
export const dynamicImport = (path) => import(pathToFileURL(path)).then((module) => module.default) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** @typedef {import('./index').Logger} Logger */ | ||
|
||
import path from 'path' | ||
|
||
import { loadConfig } from './loadConfig.js' | ||
import { ConfigNotFoundError } from './symbols.js' | ||
import { validateConfig } from './validateConfig.js' | ||
|
||
/** | ||
* Return matched files grouped by their configuration. | ||
* | ||
* @param {object} options | ||
* @param {Object} [options.configObject] - Explicit config object from the js API | ||
* @param {string} [options.configPath] - Explicit path to a config file | ||
* @param {string} [options.cwd] - Current working directory | ||
* @param {Logger} logger | ||
*/ | ||
export const getConfigGroups = async ({ configObject, configPath, files }, logger = console) => { | ||
// Return explicit config object from js API | ||
if (configObject) { | ||
const config = validateConfig(configObject, 'config object', logger) | ||
return { '': { config, files } } | ||
} | ||
|
||
// Use only explicit config path instead of discovering multiple | ||
if (configPath) { | ||
const { config, filepath } = await loadConfig({ configPath }, logger) | ||
|
||
if (!config) { | ||
logger.error(`${ConfigNotFoundError.message}.`) | ||
throw ConfigNotFoundError | ||
} | ||
|
||
const validatedConfig = validateConfig(config, filepath, logger) | ||
return { [configPath]: { config: validatedConfig, files } } | ||
} | ||
|
||
// Group files by their base directory | ||
const filesByDir = files.reduce((acc, file) => { | ||
const dir = path.normalize(path.dirname(file)) | ||
|
||
if (dir in acc) { | ||
acc[dir].push(file) | ||
} else { | ||
acc[dir] = [file] | ||
} | ||
|
||
return acc | ||
}, {}) | ||
|
||
// Group files by their discovered config | ||
// { '.lintstagedrc.json': { config: {...}, files: [...] } } | ||
const configGroups = {} | ||
|
||
for (const [dir, files] of Object.entries(filesByDir)) { | ||
// Discover config from the base directory of the file | ||
const { config, filepath } = await loadConfig({ cwd: dir }, logger) | ||
|
||
if (!config) { | ||
logger.error(`${ConfigNotFoundError.message}.`) | ||
throw ConfigNotFoundError | ||
} | ||
|
||
if (filepath in configGroups) { | ||
// Re-use cached config and skip validation | ||
configGroups[filepath].files.push(...files) | ||
continue | ||
} | ||
|
||
const validatedConfig = validateConfig(config, filepath, logger) | ||
configGroups[filepath] = { config: validatedConfig, files } | ||
} | ||
|
||
return configGroups | ||
} |
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
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
Oops, something went wrong.