lint-staged 10.3.0 → 10.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -9
- package/bin/lint-staged.js +20 -1
- package/lib/formatConfig.js +7 -0
- package/lib/index.js +5 -1
- package/lib/messages.js +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,7 +62,7 @@ Options:
|
|
|
62
62
|
-V, --version output the version number
|
|
63
63
|
--allow-empty allow empty commits when tasks revert all staged changes
|
|
64
64
|
(default: false)
|
|
65
|
-
-c, --config [path] path to configuration file
|
|
65
|
+
-c, --config [path] path to configuration file, or - to read from stdin
|
|
66
66
|
-d, --debug print additional debug information (default: false)
|
|
67
67
|
--no-stash disable the backup stash, and do not revert in case of
|
|
68
68
|
errors
|
|
@@ -78,7 +78,7 @@ Options:
|
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
- **`--allow-empty`**: By default, when linter tasks undo all staged changes, lint-staged will exit with an error and abort the commit. Use this flag to allow creating empty git commits.
|
|
81
|
-
- **`--config [path]`**: Manually specify a path to a config file or npm package name. Note: when used, lint-staged won't perform the config file search and print an error if the specified file cannot be found.
|
|
81
|
+
- **`--config [path]`**: Manually specify a path to a config file or npm package name. Note: when used, lint-staged won't perform the config file search and print an error if the specified file cannot be found. If '-' is provided as the filename then the config will be read from stdin, allowing piping in the config like `cat my-config.json | npx lint-staged --config -`.
|
|
82
82
|
- **`--debug`**: Run in debug mode. When set, it does the following:
|
|
83
83
|
- uses [debug](https://github.com/visionmedia/debug) internally to log additional 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*`.
|
|
84
84
|
- uses [`verbose` renderer](https://github.com/SamVerschueren/listr-verbose-renderer) for `listr`; this causes serial, uncoloured output to the terminal, instead of the default (beautified, dynamic) output.
|
|
@@ -97,8 +97,11 @@ Options:
|
|
|
97
97
|
Starting with v3.1 you can now use different ways of configuring it:
|
|
98
98
|
|
|
99
99
|
- `lint-staged` object in your `package.json`
|
|
100
|
-
- `.lintstagedrc` file in JSON or YML format
|
|
101
|
-
-
|
|
100
|
+
- `.lintstagedrc` file in JSON or YML format, or you can be explicit with the file extension:
|
|
101
|
+
- `.lintstagedrc.json`
|
|
102
|
+
- `.lintstagedrc.yaml`
|
|
103
|
+
- `.lintstagedrc.yml`
|
|
104
|
+
- `lint-staged.config.js`, `.lintstagedrc.js`, or `.lintstagedrc.cjs` file in JS format
|
|
102
105
|
- Pass a configuration file using the `--config` or `-c` flag
|
|
103
106
|
|
|
104
107
|
See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.
|
|
@@ -189,14 +192,40 @@ For example:
|
|
|
189
192
|
|
|
190
193
|
going to execute `eslint` and if it exits with `0` code, it will execute `prettier --write` on all staged `*.js` files.
|
|
191
194
|
|
|
192
|
-
## Using JS
|
|
195
|
+
## Using JS configuration file
|
|
193
196
|
|
|
194
|
-
|
|
197
|
+
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.
|
|
198
|
+
|
|
199
|
+
If the `exports` value is a function, it will receive an array of all staged filenames. You can then build your own matchers for the files, and return a command string, or an array or command strings. These strings are considered complete and should include the filename arguments, if wanted.
|
|
200
|
+
|
|
201
|
+
If the `exports` value is an object, its keys should be glob matches (like in the normal non-js config format). The values can either be like in the normal config, or individual functions like described above. Instead of receiving all matched files, the functions in the exported object will only receive the staged files matching the corresponding glob key.
|
|
202
|
+
|
|
203
|
+
### Function signature
|
|
204
|
+
|
|
205
|
+
The function can also be async:
|
|
195
206
|
|
|
196
207
|
```ts
|
|
197
|
-
|
|
208
|
+
(filenames: string[]) => string | string[] | Promise<string | string[]>
|
|
198
209
|
```
|
|
199
210
|
|
|
211
|
+
### Example: Export a function to build your own matchers
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
// lint-staged.config.js
|
|
215
|
+
const micromatch = require('micromatch')
|
|
216
|
+
|
|
217
|
+
module.exports = (allStagedFiles) => {
|
|
218
|
+
const shFiles = micromatch(allStagedFiles, ['**/src/**/*.sh']);
|
|
219
|
+
if (shFiles.length) {
|
|
220
|
+
return `printf '%s\n' "Script files aren't allowed in src directory" >&2`
|
|
221
|
+
}
|
|
222
|
+
const codeFiles = micromatch(allStagedFiles, ['**/*.js', '**/*.ts']);
|
|
223
|
+
const docFiles = micromatch(allStagedFiles, ['**/*.md']);
|
|
224
|
+
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
|
|
200
229
|
### Example: Wrap filenames in single quotes and run once per file
|
|
201
230
|
|
|
202
231
|
```js
|
|
@@ -226,6 +255,7 @@ module.exports = {
|
|
|
226
255
|
```
|
|
227
256
|
|
|
228
257
|
### Example: Use your own globs
|
|
258
|
+
It's better to use the [function-based configuration (seen above)](https://github.com/okonet/lint-staged/README.md#example-export-a-function-to-build-your-own-matchers), if your use case is this.
|
|
229
259
|
|
|
230
260
|
```js
|
|
231
261
|
// lint-staged.config.js
|
|
@@ -233,8 +263,9 @@ const micromatch = require('micromatch')
|
|
|
233
263
|
|
|
234
264
|
module.exports = {
|
|
235
265
|
'*': (allFiles) => {
|
|
236
|
-
const
|
|
237
|
-
|
|
266
|
+
const codeFiles = micromatch(allFiles, ['**/*.js', '**/*.ts']);
|
|
267
|
+
const docFiles = micromatch(allFiles, ['**/*.md']);
|
|
268
|
+
return [`eslint ${codeFiles.join(' ')}`, `mdl ${docFiles.join(' ')}`];
|
|
238
269
|
}
|
|
239
270
|
}
|
|
240
271
|
```
|
package/bin/lint-staged.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict'
|
|
4
4
|
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
|
|
5
7
|
// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
|
|
6
8
|
const { supportsColor } = require('chalk')
|
|
7
9
|
if (supportsColor && supportsColor.level) {
|
|
@@ -23,13 +25,14 @@ require('please-upgrade-node')(
|
|
|
23
25
|
const cmdline = require('commander')
|
|
24
26
|
const debugLib = require('debug')
|
|
25
27
|
const lintStaged = require('../lib')
|
|
28
|
+
const { CONFIG_STDIN_ERROR } = require('../lib/messages')
|
|
26
29
|
|
|
27
30
|
const debug = debugLib('lint-staged:bin')
|
|
28
31
|
|
|
29
32
|
cmdline
|
|
30
33
|
.version(pkg.version)
|
|
31
34
|
.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
|
|
32
|
-
.option('-c, --config [path]', 'path to configuration file')
|
|
35
|
+
.option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
|
|
33
36
|
.option('-d, --debug', 'print additional debug information', false)
|
|
34
37
|
.option('--no-stash', 'disable the backup stash, and do not revert in case of errors', false)
|
|
35
38
|
.option(
|
|
@@ -86,6 +89,22 @@ const options = {
|
|
|
86
89
|
|
|
87
90
|
debug('Options parsed from command-line:', options)
|
|
88
91
|
|
|
92
|
+
if (options.configPath === '-') {
|
|
93
|
+
delete options.configPath
|
|
94
|
+
try {
|
|
95
|
+
options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
|
|
96
|
+
} catch {
|
|
97
|
+
console.error(CONFIG_STDIN_ERROR)
|
|
98
|
+
process.exit(1)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
options.config = JSON.parse(options.config)
|
|
103
|
+
} catch {
|
|
104
|
+
// Let config parsing complain if it's not JSON
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
89
108
|
lintStaged(options)
|
|
90
109
|
.then((passed) => {
|
|
91
110
|
process.exitCode = passed ? 0 : 1
|
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const { PREVENTED_EMPTY_COMMIT, GIT_ERROR, RESTORE_STASH_EXAMPLE } = require('./
|
|
|
9
9
|
const printTaskOutput = require('./printTaskOutput')
|
|
10
10
|
const runAll = require('./runAll')
|
|
11
11
|
const { ApplyEmptyCommitError, GetBackupStashError, GitError } = require('./symbols')
|
|
12
|
+
const formatConfig = require('./formatConfig')
|
|
12
13
|
const validateConfig = require('./validateConfig')
|
|
13
14
|
|
|
14
15
|
const errConfigNotFound = new Error('Config could not be found')
|
|
@@ -30,7 +31,9 @@ function loadConfig(configPath) {
|
|
|
30
31
|
'.lintstagedrc.yaml',
|
|
31
32
|
'.lintstagedrc.yml',
|
|
32
33
|
'.lintstagedrc.js',
|
|
34
|
+
'.lintstagedrc.cjs',
|
|
33
35
|
'lint-staged.config.js',
|
|
36
|
+
'lint-staged.config.cjs',
|
|
34
37
|
],
|
|
35
38
|
})
|
|
36
39
|
|
|
@@ -88,7 +91,8 @@ module.exports = async function lintStaged(
|
|
|
88
91
|
debugLog('Successfully loaded config from `%s`:\n%O', resolved.filepath, resolved.config)
|
|
89
92
|
// resolved.config is the parsed configuration object
|
|
90
93
|
// resolved.filepath is the path to the config file that was found
|
|
91
|
-
const
|
|
94
|
+
const formattedConfig = formatConfig(resolved.config)
|
|
95
|
+
const config = validateConfig(formattedConfig)
|
|
92
96
|
if (debug) {
|
|
93
97
|
// Log using logger to be able to test through `consolemock`.
|
|
94
98
|
logger.log('Running lint-staged with the following config:')
|
package/lib/messages.js
CHANGED
|
@@ -39,6 +39,8 @@ const RESTORE_STASH_EXAMPLE = ` Any lost modifications can be restored from a g
|
|
|
39
39
|
> git stash apply --index stash@{0}
|
|
40
40
|
`
|
|
41
41
|
|
|
42
|
+
const CONFIG_STDIN_ERROR = 'Error: Could not read config from stdin.'
|
|
43
|
+
|
|
42
44
|
module.exports = {
|
|
43
45
|
NOT_GIT_REPO,
|
|
44
46
|
FAILED_GET_STAGED_FILES,
|
|
@@ -51,4 +53,5 @@ module.exports = {
|
|
|
51
53
|
GIT_ERROR,
|
|
52
54
|
PREVENTED_EMPTY_COMMIT,
|
|
53
55
|
RESTORE_STASH_EXAMPLE,
|
|
56
|
+
CONFIG_STDIN_ERROR,
|
|
54
57
|
}
|