1 | #!/usr/bin/env node
|
2 |
|
3 | import fs from 'fs'
|
4 | import path from 'path'
|
5 | import { fileURLToPath } from 'url'
|
6 |
|
7 | import cmdline from 'commander'
|
8 | import debug from 'debug'
|
9 | import supportsColor from 'supports-color'
|
10 |
|
11 | import lintStaged from '../lib/index.js'
|
12 | import { CONFIG_STDIN_ERROR } from '../lib/messages.js'
|
13 |
|
14 |
|
15 | if (supportsColor.stdout) {
|
16 | process.env.FORCE_COLOR = supportsColor.stdout.level.toString()
|
17 | }
|
18 |
|
19 |
|
20 | process.on('SIGINT', () => {})
|
21 |
|
22 | const packageJsonPath = path.join(fileURLToPath(import.meta.url), '../../package.json')
|
23 | const packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
|
24 | const version = packageJson.version
|
25 |
|
26 | cmdline
|
27 | .version(version)
|
28 | .option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
|
29 | .option(
|
30 | '-p, --concurrent <number|boolean>',
|
31 | 'the number of tasks to run concurrently, or false for serial',
|
32 | true
|
33 | )
|
34 | .option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
|
35 | .option('--cwd [path]', 'run all tasks in specific directory, instead of the current')
|
36 | .option('-d, --debug', 'print additional debug information', false)
|
37 | .option('--max-arg-length', 'maximum length of the command-line argument string')
|
38 | .option('--no-stash', 'disable the backup stash, and do not revert in case of errors', false)
|
39 | .option('-q, --quiet', 'disable lint-staged’s own console output', false)
|
40 | .option('-r, --relative', 'pass relative filepaths to tasks', false)
|
41 | .option('-x, --shell [path]', 'skip parsing of tasks for better shell support', false)
|
42 | .option(
|
43 | '-v, --verbose',
|
44 | 'show task output even when tasks succeed; by default only failed output is shown',
|
45 | false
|
46 | )
|
47 | .parse(process.argv)
|
48 |
|
49 | const cmdlineOptions = cmdline.opts()
|
50 |
|
51 | if (cmdlineOptions.debug) {
|
52 | debug.enable('lint-staged*')
|
53 | }
|
54 |
|
55 | const debugLog = debug('lint-staged:bin')
|
56 | debugLog('Running `lint-staged@%s`', version)
|
57 |
|
58 | const options = {
|
59 | allowEmpty: !!cmdlineOptions.allowEmpty,
|
60 | concurrent: JSON.parse(cmdlineOptions.concurrent),
|
61 | configPath: cmdlineOptions.config,
|
62 | cwd: cmdlineOptions.cwd,
|
63 | debug: !!cmdlineOptions.debug,
|
64 | maxArgLength: JSON.parse(cmdlineOptions.maxArgLength || null),
|
65 | quiet: !!cmdlineOptions.quiet,
|
66 | relative: !!cmdlineOptions.relative,
|
67 | shell: cmdlineOptions.shell ,
|
68 | stash: !!cmdlineOptions.stash,
|
69 | verbose: !!cmdlineOptions.verbose,
|
70 | }
|
71 |
|
72 | debugLog('Options parsed from command-line:', options)
|
73 |
|
74 | if (options.configPath === '-') {
|
75 | delete options.configPath
|
76 | try {
|
77 | options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
|
78 | } catch {
|
79 | console.error(CONFIG_STDIN_ERROR)
|
80 | process.exit(1)
|
81 | }
|
82 |
|
83 | try {
|
84 | options.config = JSON.parse(options.config)
|
85 | } catch {
|
86 |
|
87 | }
|
88 | }
|
89 |
|
90 | lintStaged(options)
|
91 | .then((passed) => {
|
92 | process.exitCode = passed ? 0 : 1
|
93 | })
|
94 | .catch(() => {
|
95 | process.exitCode = 1
|
96 | })
|