1 | #!/usr/bin/env node
|
2 |
|
3 | import fs from 'fs'
|
4 | import path from 'path'
|
5 | import { fileURLToPath } from '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('--max-arg-length [number]', 'maximum length of the command-line argument string', 0)
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | cli
|
53 | .addOption(
|
54 | new Option('--stash', 'enable the backup stash, and revert in case of errors')
|
55 | .default(true)
|
56 | .hideHelp()
|
57 | )
|
58 | .addOption(
|
59 | new Option(
|
60 | '--no-stash',
|
61 | 'disable the backup stash, and do not revert in case of errors'
|
62 | ).default(false)
|
63 | )
|
64 |
|
65 | cli.option('-q, --quiet', 'disable lint-staged’s own console output', false)
|
66 |
|
67 | cli.option('-r, --relative', 'pass relative filepaths to tasks', false)
|
68 |
|
69 | cli.option('-x, --shell [path]', 'skip parsing of tasks for better shell support', false)
|
70 |
|
71 | cli.option(
|
72 | '-v, --verbose',
|
73 | 'show task output even when tasks succeed; by default only failed output is shown',
|
74 | false
|
75 | )
|
76 |
|
77 | const cliOptions = cli.parse(process.argv).opts()
|
78 |
|
79 | if (cliOptions.debug) {
|
80 | debug.enable('lint-staged*')
|
81 | }
|
82 |
|
83 | const options = {
|
84 | allowEmpty: !!cliOptions.allowEmpty,
|
85 | concurrent: JSON.parse(cliOptions.concurrent),
|
86 | configPath: cliOptions.config,
|
87 | cwd: cliOptions.cwd,
|
88 | debug: !!cliOptions.debug,
|
89 | maxArgLength: cliOptions.maxArgLength || undefined,
|
90 | quiet: !!cliOptions.quiet,
|
91 | relative: !!cliOptions.relative,
|
92 | shell: cliOptions.shell ,
|
93 | stash: !!cliOptions.stash,
|
94 | verbose: !!cliOptions.verbose,
|
95 | }
|
96 |
|
97 | debugLog('Options parsed from command-line:', options)
|
98 |
|
99 | if (options.configPath === '-') {
|
100 | delete options.configPath
|
101 | try {
|
102 | options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
|
103 | } catch {
|
104 | console.error(CONFIG_STDIN_ERROR)
|
105 | process.exit(1)
|
106 | }
|
107 |
|
108 | try {
|
109 | options.config = JSON.parse(options.config)
|
110 | } catch {
|
111 |
|
112 | }
|
113 | }
|
114 |
|
115 | lintStaged(options)
|
116 | .then((passed) => {
|
117 | process.exitCode = passed ? 0 : 1
|
118 | })
|
119 | .catch(() => {
|
120 | process.exitCode = 1
|
121 | })
|