Skip to content

Commit e7f9fa0

Browse files
committed
feat: replace cosmiconfig with lilconfig + yaml to reduce dependencies
1 parent 410c3ba commit e7f9fa0

File tree

12 files changed

+128
-192
lines changed

12 files changed

+128
-192
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,7 @@ Starting with v3.1 you can now use different ways of configuring lint-staged:
114114
whether your project's _package.json_ contains the `"type": "module"` option or not.
115115
- Pass a configuration file using the `--config` or `-c` flag
116116
117-
See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.
118-
119-
Configuration should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses [micromatch](https://github.com/micromatch/micromatch) for glob patterns.
117+
Configuration should be an object where each value is a command to run and its key is a glob pattern to use for this command. This package uses [micromatch](https://github.com/micromatch/micromatch) for glob patterns. JavaScript files can also export advanced configuration as a function. See [Using JS configuration files](#using-js-configuration-files) for more info.
120118
121119
#### `package.json` example:
122120
@@ -202,7 +200,7 @@ For example:
202200
203201
going to execute `eslint` and if it exits with `0` code, it will execute `prettier --write` on all staged `*.js` files.
204202
205-
## Using JS configuration file
203+
## Using JS configuration files
206204
207205
Writing the configuration file in JavaScript is the most powerful way to configure lint-staged (`lint-staged.config.js`, [similar](https://github.com/okonet/lint-staged/README.md#configuration), or passed via `--config`). From the configuration file, you can export either a single function or an object.
208206

lib/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,19 @@ const lintStaged = async (
5858
) => {
5959
await validateOptions({ shell }, logger)
6060

61-
debugLog('Loading config using `cosmiconfig`')
61+
debugLog('Loading config using `lilconfig`')
6262

6363
const resolved = configObject
6464
? { config: configObject, filepath: '(input)' }
6565
: await loadConfig(configPath)
6666

67-
if (resolved == null) {
67+
if (!resolved) {
6868
logger.error(`${ConfigNotFoundError.message}.`)
6969
throw ConfigNotFoundError
7070
}
7171

7272
debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
7373

74-
// resolved.config is the parsed configuration object
75-
// resolved.filepath is the path to the config file that was found
7674
const config = validateConfig(resolved.config, logger)
7775

7876
if (debug) {

lib/loadConfig.js

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
1-
import { cosmiconfig } from 'cosmiconfig'
1+
import { lilconfig } from 'lilconfig'
2+
import YAML from 'yaml'
3+
4+
/**
5+
* The list of files `lint-staged` will read configuration
6+
* from, in the declared order.
7+
*/
8+
const searchPlaces = [
9+
'package.json',
10+
'.lintstagedrc',
11+
'.lintstagedrc.json',
12+
'.lintstagedrc.yaml',
13+
'.lintstagedrc.yml',
14+
'.lintstagedrc.mjs',
15+
'.lintstagedrc.js',
16+
'.lintstagedrc.cjs',
17+
'lint-staged.config.mjs',
18+
'lint-staged.config.js',
19+
'lint-staged.config.cjs',
20+
]
221

322
const dynamicImport = (path) => import(path).then((module) => module.default)
423

524
const jsonParse = (path, content) => JSON.parse(content)
625

26+
/**
27+
* `lilconfig` doesn't support yaml files by default,
28+
* so we add custom loaders for those. Files without
29+
* an extensions are assumed to be yaml — this
30+
* assumption is in `cosmiconfig` as well.
31+
*/
32+
const loaders = {
33+
'.js': dynamicImport,
34+
'.json': jsonParse,
35+
'.mjs': dynamicImport,
36+
'.cjs': dynamicImport,
37+
'.yaml': YAML.parse,
38+
'.yml': YAML.parse,
39+
noExt: YAML.parse,
40+
}
41+
742
const resolveConfig = (configPath) => {
843
try {
944
return require.resolve(configPath)
@@ -16,27 +51,6 @@ const resolveConfig = (configPath) => {
1651
* @param {string} [configPath]
1752
*/
1853
export const loadConfig = (configPath) => {
19-
const explorer = cosmiconfig('lint-staged', {
20-
searchPlaces: [
21-
'package.json',
22-
'.lintstagedrc',
23-
'.lintstagedrc.json',
24-
'.lintstagedrc.yaml',
25-
'.lintstagedrc.yml',
26-
'.lintstagedrc.mjs',
27-
'.lintstagedrc.js',
28-
'.lintstagedrc.cjs',
29-
'lint-staged.config.mjs',
30-
'lint-staged.config.js',
31-
'lint-staged.config.cjs',
32-
],
33-
loaders: {
34-
'.cjs': dynamicImport,
35-
'.js': dynamicImport,
36-
'.json': jsonParse,
37-
'.mjs': dynamicImport,
38-
},
39-
})
40-
54+
const explorer = lilconfig('lint-staged', { searchPlaces, loaders })
4155
return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
4256
}

lib/symbols.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
export const ApplyEmptyCommitError = Symbol('ApplyEmptyCommitError')
22

3-
export const ConfigNotFoundError = new Error('Config could not be found')
3+
export const ConfigNotFoundError = new Error('Configuration could not be found')
4+
5+
export const ConfigFormatError = new Error('Configuration should be an object or a function')
6+
7+
export const ConfigEmptyError = new Error('Configuration should not be empty')
48

59
export const GetBackupStashError = Symbol('GetBackupStashError')
610

lib/validateConfig.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import debug from 'debug'
22

33
import { configurationError } from './messages.js'
4+
import { ConfigEmptyError, ConfigFormatError } from './symbols.js'
45
import { validateBraces } from './validateBraces.js'
56

67
const debugLog = debug('lint-staged:validateConfig')
@@ -27,7 +28,7 @@ export const validateConfig = (config, logger) => {
2728
debugLog('Validating config')
2829

2930
if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
30-
throw new Error('Configuration should be an object or a function!')
31+
throw ConfigFormatError
3132
}
3233

3334
/**
@@ -42,7 +43,7 @@ export const validateConfig = (config, logger) => {
4243
}
4344

4445
if (Object.entries(config).length === 0) {
45-
throw new Error('Configuration should not be empty!')
46+
throw ConfigEmptyError
4647
}
4748

4849
const errors = []

0 commit comments

Comments
 (0)