Skip to content

Commit

Permalink
Merge pull request #801 from okonet/simplify
Browse files Browse the repository at this point in the history
feat: use diff/apply only with partially staged files
  • Loading branch information
iiroj committed Mar 30, 2020
2 parents 2cb26a6 + 1e7298a commit 8abb09f
Show file tree
Hide file tree
Showing 12 changed files with 438 additions and 227 deletions.
14 changes: 10 additions & 4 deletions README.md
Expand Up @@ -60,13 +60,18 @@ Usage: lint-staged [options]

Options:
-V, --version output the version number
--allow-empty allow empty commits when tasks undo all staged changes (default: false)
--allow-empty allow empty commits when tasks revert all staged changes
(default: false)
-c, --config [path] path to configuration file
-d, --debug print additional debug information (default: false)
-p, --concurrent <parallel tasks> the number of tasks to run concurrently, or false to run tasks serially (default: true)
--no-stash disable the backup stash, and do not revert in case of
errors
-p, --concurrent <parallel tasks> the number of tasks to run concurrently, or false to run
tasks serially (default: true)
-q, --quiet disable lint-staged’s own console output (default: false)
-r, --relative pass relative filepaths to tasks (default: false)
-x, --shell skip parsing of tasks for better shell support (default: false)
-x, --shell skip parsing of tasks for better shell support (default:
false)
-h, --help output usage information
```
Expand All @@ -79,6 +84,7 @@ Options:
- `false`: Run all tasks serially
- `true` (default) : _Infinite_ concurrency. Runs as many tasks in parallel as possible.
- `{number}`: Run the specified number of tasks in parallel, where `1` is equivalent to `false`.
- **`--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.
- **`--quiet`**: Supress all CLI output, except from tasks.
- **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`.
- **`--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.
Expand Down Expand Up @@ -168,7 +174,7 @@ Pass arguments to your commands separated by space as you would do in the shell.
## Running multiple commands in a sequence
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.
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.
For example:
Expand Down
2 changes: 2 additions & 0 deletions bin/lint-staged.js
Expand Up @@ -31,6 +31,7 @@ cmdline
.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
.option('-c, --config [path]', 'path to configuration file')
.option('-d, --debug', 'print additional debug information', false)
.option('--no-stash', 'disable the backup stash, and do not revert in case of errors', false)
.option(
'-p, --concurrent <parallel tasks>',
'the number of tasks to run concurrently, or false to run tasks serially',
Expand Down Expand Up @@ -71,6 +72,7 @@ const options = {
configPath: cmdline.config,
debug: !!cmdline.debug,
maxArgLength: getMaxArgLength() / 2,
stash: !!cmdline.stash, // commander inverts `no-<x>` flags to `!x`
quiet: !!cmdline.quiet,
relative: !!cmdline.relative,
shell: !!cmdline.shell
Expand Down
36 changes: 9 additions & 27 deletions lib/file.js
Expand Up @@ -4,25 +4,10 @@ const debug = require('debug')('lint-staged:file')
const fs = require('fs')
const { promisify } = require('util')

const fsAccess = promisify(fs.access)
const fsReadFile = promisify(fs.readFile)
const fsUnlink = promisify(fs.unlink)
const fsWriteFile = promisify(fs.writeFile)

/**
* Check if a file exists. Returns the filename if exists.
* @param {String} filename
* @returns {String|Boolean}
*/
const exists = async filename => {
try {
await fsAccess(filename)
return filename
} catch {
return false
}
}

/**
* Read contents of a file to buffer
* @param {String} filename
Expand All @@ -44,21 +29,19 @@ const readFile = async (filename, ignoreENOENT = true) => {
}

/**
* Unlink a file if it exists
* Remove a file
* @param {String} filename
* @param {Boolean} [ignoreENOENT=true] — Whether to throw if the file doesn't exist
*/
const unlink = async (filename, ignoreENOENT = true) => {
if (filename) {
debug('Unlinking file `%s`', filename)
try {
await fsUnlink(filename)
} catch (error) {
if (ignoreENOENT && error.code === 'ENOENT') {
debug("File `%s` doesn't exist, ignoring...", filename)
} else {
throw error
}
debug('Removing file `%s`', filename)
try {
await fsUnlink(filename)
} catch (error) {
if (ignoreENOENT && error.code === 'ENOENT') {
debug("File `%s` doesn't exist, ignoring...", filename)
} else {
throw error
}
}
}
Expand All @@ -74,7 +57,6 @@ const writeFile = async (filename, buffer) => {
}

module.exports = {
exists,
readFile,
unlink,
writeFile
Expand Down

0 comments on commit 8abb09f

Please sign in to comment.