Skip to content

Commit

Permalink
feat: allow reading config from stdin (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Oct 26, 2020
1 parent 78782f9 commit 969713d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Options:
-V, --version output the version number
--allow-empty allow empty commits when tasks revert all staged changes
(default: false)
-c, --config [path] path to configuration file
-c, --config [path] path to configuration file, or - to read from stdin
-d, --debug print additional debug information (default: false)
--no-stash disable the backup stash, and do not revert in case of
errors
Expand All @@ -78,7 +78,7 @@ Options:
```
- **`--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.
- **`--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.
- **`--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 -`.
- **`--debug`**: Run in debug mode. When set, it does the following:
- 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*`.
- 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.
Expand Down
21 changes: 20 additions & 1 deletion bin/lint-staged.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

'use strict'

const fs = require('fs')

// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
const { supportsColor } = require('chalk')
if (supportsColor && supportsColor.level) {
Expand All @@ -23,13 +25,14 @@ require('please-upgrade-node')(
const cmdline = require('commander')
const debugLib = require('debug')
const lintStaged = require('../lib')
const { CONFIG_STDIN_ERROR } = require('../lib/messages')

const debug = debugLib('lint-staged:bin')

cmdline
.version(pkg.version)
.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
.option('-c, --config [path]', 'path to configuration file')
.option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
.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(
Expand Down Expand Up @@ -86,6 +89,22 @@ const options = {

debug('Options parsed from command-line:', options)

if (options.configPath === '-') {
delete options.configPath
try {
options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
} catch {
console.error(CONFIG_STDIN_ERROR)
process.exit(1)
}

try {
options.config = JSON.parse(options.config)
} catch {
// Let config parsing complain if it's not JSON
}
}

lintStaged(options)
.then((passed) => {
process.exitCode = passed ? 0 : 1
Expand Down
3 changes: 3 additions & 0 deletions lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const RESTORE_STASH_EXAMPLE = ` Any lost modifications can be restored from a g
> git stash apply --index stash@{0}
`

const CONFIG_STDIN_ERROR = 'Error: Could not read config from stdin.'

module.exports = {
NOT_GIT_REPO,
FAILED_GET_STAGED_FILES,
Expand All @@ -51,4 +53,5 @@ module.exports = {
GIT_ERROR,
PREVENTED_EMPTY_COMMIT,
RESTORE_STASH_EXAMPLE,
CONFIG_STDIN_ERROR,
}

0 comments on commit 969713d

Please sign in to comment.