Skip to content

Commit 8abb09f

Browse files
authored
Merge pull request #801 from okonet/simplify
feat: use diff/apply only with partially staged files
2 parents 2cb26a6 + 1e7298a commit 8abb09f

12 files changed

+438
-227
lines changed

README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,18 @@ Usage: lint-staged [options]
6060

6161
Options:
6262
-V, --version output the version number
63-
--allow-empty allow empty commits when tasks undo all staged changes (default: false)
63+
--allow-empty allow empty commits when tasks revert all staged changes
64+
(default: false)
6465
-c, --config [path] path to configuration file
6566
-d, --debug print additional debug information (default: false)
66-
-p, --concurrent <parallel tasks> the number of tasks to run concurrently, or false to run tasks serially (default: true)
67+
--no-stash disable the backup stash, and do not revert in case of
68+
errors
69+
-p, --concurrent <parallel tasks> the number of tasks to run concurrently, or false to run
70+
tasks serially (default: true)
6771
-q, --quiet disable lint-staged’s own console output (default: false)
6872
-r, --relative pass relative filepaths to tasks (default: false)
69-
-x, --shell skip parsing of tasks for better shell support (default: false)
73+
-x, --shell skip parsing of tasks for better shell support (default:
74+
false)
7075
-h, --help output usage information
7176
```
7277
@@ -79,6 +84,7 @@ Options:
7984
- `false`: Run all tasks serially
8085
- `true` (default) : _Infinite_ concurrency. Runs as many tasks in parallel as possible.
8186
- `{number}`: Run the specified number of tasks in parallel, where `1` is equivalent to `false`.
87+
- **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit.
8288
- **`--quiet`**: Supress all CLI output, except from tasks.
8389
- **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`.
8490
- **`--shell`**: By default linter commands will be parsed for speed and security. This has the side-effect that regular shell scripts might not work as expected. You can skip parsing of commands with this option.
@@ -168,7 +174,7 @@ Pass arguments to your commands separated by space as you would do in the shell.
168174
169175
## Running multiple commands in a sequence
170176
171-
You can run multiple commands in a sequence on every glob. To do so, pass an array of commands instead of a single one. This is useful for running autoformatting tools like `eslint --fix` or `stylefmt` but can be used for any arbitrary sequences.
177+
You can run multiple commands in a sequence on every glob. To do so, pass an array of commands instead of a single one. This is useful for running autoformatting tools like `eslint --fix` or `stylefmt` but can be used for any arbitrary sequences.
172178
173179
For example:
174180

bin/lint-staged.js

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ cmdline
3131
.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
3232
.option('-c, --config [path]', 'path to configuration file')
3333
.option('-d, --debug', 'print additional debug information', false)
34+
.option('--no-stash', 'disable the backup stash, and do not revert in case of errors', false)
3435
.option(
3536
'-p, --concurrent <parallel tasks>',
3637
'the number of tasks to run concurrently, or false to run tasks serially',
@@ -71,6 +72,7 @@ const options = {
7172
configPath: cmdline.config,
7273
debug: !!cmdline.debug,
7374
maxArgLength: getMaxArgLength() / 2,
75+
stash: !!cmdline.stash, // commander inverts `no-<x>` flags to `!x`
7476
quiet: !!cmdline.quiet,
7577
relative: !!cmdline.relative,
7678
shell: !!cmdline.shell

lib/file.js

+9-27
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,10 @@ const debug = require('debug')('lint-staged:file')
44
const fs = require('fs')
55
const { promisify } = require('util')
66

7-
const fsAccess = promisify(fs.access)
87
const fsReadFile = promisify(fs.readFile)
98
const fsUnlink = promisify(fs.unlink)
109
const fsWriteFile = promisify(fs.writeFile)
1110

12-
/**
13-
* Check if a file exists. Returns the filename if exists.
14-
* @param {String} filename
15-
* @returns {String|Boolean}
16-
*/
17-
const exists = async filename => {
18-
try {
19-
await fsAccess(filename)
20-
return filename
21-
} catch {
22-
return false
23-
}
24-
}
25-
2611
/**
2712
* Read contents of a file to buffer
2813
* @param {String} filename
@@ -44,21 +29,19 @@ const readFile = async (filename, ignoreENOENT = true) => {
4429
}
4530

4631
/**
47-
* Unlink a file if it exists
32+
* Remove a file
4833
* @param {String} filename
4934
* @param {Boolean} [ignoreENOENT=true] — Whether to throw if the file doesn't exist
5035
*/
5136
const unlink = async (filename, ignoreENOENT = true) => {
52-
if (filename) {
53-
debug('Unlinking file `%s`', filename)
54-
try {
55-
await fsUnlink(filename)
56-
} catch (error) {
57-
if (ignoreENOENT && error.code === 'ENOENT') {
58-
debug("File `%s` doesn't exist, ignoring...", filename)
59-
} else {
60-
throw error
61-
}
37+
debug('Removing file `%s`', filename)
38+
try {
39+
await fsUnlink(filename)
40+
} catch (error) {
41+
if (ignoreENOENT && error.code === 'ENOENT') {
42+
debug("File `%s` doesn't exist, ignoring...", filename)
43+
} else {
44+
throw error
6245
}
6346
}
6447
}
@@ -74,7 +57,6 @@ const writeFile = async (filename, buffer) => {
7457
}
7558

7659
module.exports = {
77-
exists,
7860
readFile,
7961
unlink,
8062
writeFile

0 commit comments

Comments
 (0)