forked from lint-staged/lint-staged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-staged.js
executable file
·135 lines (107 loc) · 3.82 KB
/
lint-staged.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { supportsColor } from 'chalk'
import { Option, program } from 'commander'
import debug from 'debug'
import lintStaged from '../lib/index.js'
import { CONFIG_STDIN_ERROR } from '../lib/messages.js'
// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
if (supportsColor) {
process.env.FORCE_COLOR = supportsColor.level.toString()
}
// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})
const packageJsonPath = path.join(fileURLToPath(import.meta.url), '../../package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
const version = packageJson.version
const debugLog = debug('lint-staged:bin')
debugLog('Running `lint-staged@%s`', version)
const cli = program.version(version)
cli.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
cli.option(
'-p, --concurrent <number|boolean>',
'the number of tasks to run concurrently, or false for serial',
true
)
cli.option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
cli.option('--cwd [path]', 'run all tasks in specific directory, instead of the current')
cli.option('-d, --debug', 'print additional debug information', false)
cli.addOption(
new Option(
'--diff [string]',
'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".'
).implies({ stash: false })
)
cli.option(
'--diff-filter [string]',
'override the default "--diff-filter=ACMR" flag of "git diff" to get list of files'
)
cli.option('--max-arg-length [number]', 'maximum length of the command-line argument string', 0)
/**
* We don't want to show the `--stash` flag because it's on by default, and only show the
* negatable flag `--no-stash` in stead. There seems to be a bug in Commander.js where
* configuring only the latter won't actually set the default value.
*/
cli
.addOption(
new Option('--stash', 'enable the backup stash, and revert in case of errors')
.default(true)
.hideHelp()
)
.addOption(
new Option(
'--no-stash',
'disable the backup stash, and do not revert in case of errors'
).default(false)
)
cli.option('-q, --quiet', 'disable lint-staged’s own console output', false)
cli.option('-r, --relative', 'pass relative filepaths to tasks', false)
cli.option('-x, --shell [path]', 'skip parsing of tasks for better shell support', false)
cli.option(
'-v, --verbose',
'show task output even when tasks succeed; by default only failed output is shown',
false
)
const cliOptions = cli.parse(process.argv).opts()
if (cliOptions.debug) {
debug.enable('lint-staged*')
}
const options = {
allowEmpty: !!cliOptions.allowEmpty,
concurrent: JSON.parse(cliOptions.concurrent),
configPath: cliOptions.config,
cwd: cliOptions.cwd,
debug: !!cliOptions.debug,
diff: cliOptions.diff,
diffFilter: cliOptions.diffFilter,
maxArgLength: cliOptions.maxArgLength || undefined,
quiet: !!cliOptions.quiet,
relative: !!cliOptions.relative,
shell: cliOptions.shell /* Either a boolean or a string pointing to the shell */,
stash: !!cliOptions.stash, // commander inverts `no-<x>` flags to `!x`
verbose: !!cliOptions.verbose,
}
debugLog('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
})
.catch(() => {
process.exitCode = 1
})