-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace
cosmiconfig
with lilconfig
+ yaml
to reduce depen…
…dencies
- Loading branch information
Showing
13 changed files
with
175 additions
and
195 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
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,57 @@ | ||
import { isAbsolute } from 'path' | ||
|
||
import { lilconfig } from 'lilconfig' | ||
import YAML from 'yaml' | ||
|
||
/** | ||
* The list of files `lint-staged` will read configuration | ||
* from, in the declared order. | ||
*/ | ||
const searchPlaces = [ | ||
'package.json', | ||
'.lintstagedrc', | ||
'.lintstagedrc.json', | ||
'.lintstagedrc.yaml', | ||
'.lintstagedrc.yml', | ||
'.lintstagedrc.js', | ||
'.lintstagedrc.cjs', | ||
'lint-staged.config.js', | ||
'lint-staged.config.cjs', | ||
] | ||
|
||
/** | ||
* `lilconfig` doesn't support yaml files by default, | ||
* so we add custom loaders for those. Files without | ||
* an extensions are assumed to be yaml — this | ||
* assumption is in `cosmiconfig` as well. | ||
*/ | ||
const loaders = { '.yml': YAML.parse, '.yaml': YAML.parse, noExt: YAML.parse } | ||
|
||
/** | ||
* Load and parse config from config file, with optional path. | ||
* @param {string} [configPath] | ||
* @returns {Promise<{ config: *; filepath: string } | null>} config | ||
*/ | ||
const loadConfig = async (configPath) => { | ||
const isRelativeConfigPath = configPath && !isAbsolute(configPath) | ||
|
||
/* Try to import relative filepath as an ESM Module */ | ||
if (isRelativeConfigPath) { | ||
try { | ||
const { default: config } = await import(configPath) | ||
return { config, filepath: configPath } | ||
} catch { | ||
/** | ||
* We can safely ignore this error, because it probably means | ||
* `configPath` didn't point to a module and import() | ||
* threw a `MODULE_NOT_FOUND` error. Continue to load it as a | ||
* relative path instead. | ||
*/ | ||
} | ||
} | ||
|
||
const explorer = lilconfig('lint-staged', { searchPlaces, loaders }) | ||
return configPath ? explorer.load(configPath) : explorer.search() | ||
} | ||
|
||
export default loadConfig |
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.