Skip to content

Commit c34a3f7

Browse files
cameronhunterokonet
authored andcommitted
feat: Resolve a npm package passed as --config (lint-staged#464)
Enables a npm package to be passed to the command line `--config` parameter which will be resolved by lint-staged using Node's resolve mechanism. Example: Say you have a npm package `my-tools-config/lint-staged`, your application can now depend on the configuration package and lint-staged allowing: ``` lint-staged --config my-tools-config/lint-staged ```
1 parent 3d0ccb2 commit c34a3f7

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ $ ./node_modules/.bin/lint-staged --help
7171
Options:
7272
7373
-V, --version output the version number
74-
-c, --config [path] Path to configuration file
74+
-c, --config [path] Configuration file path or package
7575
-d, --debug Enable debug mode
7676
-h, --help output usage information
7777
```
7878

79-
* **`--config [path]`**: This can be used to manually specify the `lint-staged` config file location. However, if the specified file cannot be found, it will error out instead of performing the usual search.
79+
* **`--config [path]`**: This can be used to manually specify the `lint-staged` config file location. However, if the specified file cannot be found, it will error out instead of performing the usual search. You may pass a npm package name for configuration also.
8080
* **`--debug`**: Enabling the debug mode does the following:
8181
* `lint-staged` uses the [debug](https://github.com/visionmedia/debug) module internally to log information about staged files, commands being executed, location of binaries etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable `$DEBUG` to `lint-staged*`.
8282
* Use the [`verbose` renderer](https://github.com/SamVerschueren/listr-verbose-renderer) for `listr`.
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
module.exports = {
4+
'*': 'mytask'
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "my-lint-staged-config",
3+
"version": "0.0.0"
4+
}

src/index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,28 @@ if (process.stdout.isTTY) {
1818

1919
const errConfigNotFound = new Error('Config could not be found')
2020

21+
function resolveConfig(configPath) {
22+
try {
23+
return require.resolve(configPath)
24+
} catch (ignore) {
25+
return configPath
26+
}
27+
}
28+
2129
function loadConfig(configPath) {
2230
const explorer = cosmiconfig('lint-staged', {
2331
searchPlaces: [
2432
'package.json',
25-
`.lintstagedrc`,
26-
`.lintstagedrc.json`,
27-
`.lintstagedrc.yaml`,
28-
`.lintstagedrc.yml`,
29-
`.lintstagedrc.js`,
30-
`lint-staged.config.js`
33+
'.lintstagedrc',
34+
'.lintstagedrc.json',
35+
'.lintstagedrc.yaml',
36+
'.lintstagedrc.yml',
37+
'.lintstagedrc.js',
38+
'lint-staged.config.js'
3139
]
3240
})
3341

34-
return configPath ? explorer.load(configPath) : explorer.search()
42+
return configPath ? explorer.load(resolveConfig(configPath)) : explorer.search()
3543
}
3644

3745
/**

test/__snapshots__/index.spec.js.snap

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`lintStaged should load an npm config package when specified 1`] = `
4+
"
5+
LOG Running lint-staged with the following config:
6+
LOG {
7+
linters: {
8+
'*': 'mytask'
9+
},
10+
concurrent: true,
11+
chunkSize: 9007199254740991,
12+
globOptions: {
13+
matchBase: true,
14+
dot: true
15+
},
16+
ignore: [],
17+
subTaskConcurrency: 1,
18+
renderer: 'verbose'
19+
}"
20+
`;
21+
322
exports[`lintStaged should load config file when specified 1`] = `
423
"
524
LOG Running lint-staged with the following config:

test/index.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ describe('lintStaged', () => {
4949
expect(logger.printHistory()).toMatchSnapshot()
5050
})
5151

52+
it('should load an npm config package when specified', async () => {
53+
expect.assertions(1)
54+
jest.mock('my-lint-staged-config')
55+
await lintStaged(logger, 'my-lint-staged-config', true)
56+
expect(logger.printHistory()).toMatchSnapshot()
57+
})
58+
5259
it('should print helpful error message when config file is not found', async () => {
5360
expect.assertions(1)
5461
mockCosmiconfigWith(null)

0 commit comments

Comments
 (0)