1 | #!/usr/bin/env node
|
2 |
|
3 | import fs from 'node:fs'
|
4 | import path from 'node:path'
|
5 | import { fileURLToPath } from 'node:url'
|
6 |
|
7 | import { Option, program } 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 | const debugLog = debug('lint-staged:bin')
|
27 | debugLog('Running `lint-staged@%s`', version)
|
28 |
|
29 | const cli = program.version(version)
|
30 |
|
31 | cli.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
|
32 |
|
33 | cli.option(
|
34 | '-p, --concurrent <number|boolean>',
|
35 | 'the number of tasks to run concurrently, or false for serial',
|
36 | true
|
37 | )
|
38 |
|
39 | cli.option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
|
40 |
|
41 | cli.option('--cwd [path]', 'run all tasks in specific directory, instead of the current')
|
42 |
|
43 | cli.option('-d, --debug', 'print additional debug information', false)
|
44 |
|
45 | cli.option(
|
46 | '--diff [string]',
|
47 | 'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".'
|
48 | )
|
49 |
|
50 | cli.option(
|
51 | '--diff-filter [string]',
|
52 | 'override the default "--diff-filter=ACMR" flag of "git diff" to get list of files'
|
53 | )
|
54 |
|
55 | cli.option('--max-arg-length [number]', 'maximum length of the command-line argument string', 0)
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | cli
|
63 | .addOption(
|
64 | new Option('--stash', 'enable the backup stash, and revert in case of errors')
|
65 | .default(true)
|
66 | .hideHelp()
|
67 | )
|
68 | .addOption(
|
69 | new Option(
|
70 | '--no-stash',
|
71 | 'disable the backup stash, and do not revert in case of errors'
|
72 | ).default(false)
|
73 | )
|
74 |
|
75 | cli.option('-q, --quiet', 'disable lint-staged’s own console output', false)
|
76 |
|
77 | cli.option('-r, --relative', 'pass relative filepaths to tasks', false)
|
78 |
|
79 | cli.option('-x, --shell [path]', 'skip parsing of tasks for better shell support', false)
|
80 |
|
81 | cli.option(
|
82 | '-v, --verbose',
|
83 | 'show task output even when tasks succeed; by default only failed output is shown',
|
84 | false
|
85 | )
|
86 |
|
87 | const cliOptions = cli.parse(process.argv).opts()
|
88 |
|
89 | if (cliOptions.debug) {
|
90 | debug.enable('lint-staged*')
|
91 | }
|
92 |
|
93 | const options = {
|
94 | allowEmpty: !!cliOptions.allowEmpty,
|
95 | concurrent: JSON.parse(cliOptions.concurrent),
|
96 | configPath: cliOptions.config,
|
97 | cwd: cliOptions.cwd,
|
98 | debug: !!cliOptions.debug,
|
99 | diff: cliOptions.diff,
|
100 | diffFilter: cliOptions.diffFilter,
|
101 | maxArgLength: cliOptions.maxArgLength || undefined,
|
102 | quiet: !!cliOptions.quiet,
|
103 | relative: !!cliOptions.relative,
|
104 | shell: cliOptions.shell ,
|
105 | stash: !!cliOptions.stash,
|
106 | verbose: !!cliOptions.verbose,
|
107 | }
|
108 |
|
109 | debugLog('Options parsed from command-line:', options)
|
110 |
|
111 | if (options.configPath === '-') {
|
112 | delete options.configPath
|
113 | try {
|
114 | options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
|
115 | } catch {
|
116 | console.error(CONFIG_STDIN_ERROR)
|
117 | process.exit(1)
|
118 | }
|
119 |
|
120 | try {
|
121 | options.config = JSON.parse(options.config)
|
122 | } catch {
|
123 |
|
124 | }
|
125 | }
|
126 |
|
127 | lintStaged(options)
|
128 | .then((passed) => {
|
129 | process.exitCode = passed ? 0 : 1
|
130 | })
|
131 | .catch(() => {
|
132 | process.exitCode = 1
|
133 | })
|