UNPKG

3.54 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('--max-arg-length [number]', 'maximum length of the command-line argument string', 0)
46
47/**
48 * We don't want to show the `--stash` flag because it's on by default, and only show the
49 * negatable flag `--no-stash` in stead. There seems to be a bug in Commander.js where
50 * configuring only the latter won't actually set the default value.
51 */
52cli
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
65cli.option('-q, --quiet', 'disable lint-staged’s own console output', false)
66
67cli.option('-r, --relative', 'pass relative filepaths to tasks', false)
68
69cli.option('-x, --shell [path]', 'skip parsing of tasks for better shell support', false)
70
71cli.option(
72 '-v, --verbose',
73 'show task output even when tasks succeed; by default only failed output is shown',
74 false
75)
76
77const cliOptions = cli.parse(process.argv).opts()
78
79if (cliOptions.debug) {
80 debug.enable('lint-staged*')
81}
82
83const 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 /* Either a boolean or a string pointing to the shell */,
93 stash: !!cliOptions.stash, // commander inverts `no-<x>` flags to `!x`
94 verbose: !!cliOptions.verbose,
95}
96
97debugLog('Options parsed from command-line:', options)
98
99if (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 // Let config parsing complain if it's not JSON
112 }
113}
114
115lintStaged(options)
116 .then((passed) => {
117 process.exitCode = passed ? 0 : 1
118 })
119 .catch(() => {
120 process.exitCode = 1
121 })