UNPKG

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