1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const fs = require('fs')
|
6 |
|
7 |
|
8 | const { supportsColor } = require('chalk')
|
9 | if (supportsColor && supportsColor.level) {
|
10 | process.env.FORCE_COLOR = supportsColor.level.toString()
|
11 | }
|
12 |
|
13 |
|
14 | process.on('SIGINT', () => {})
|
15 |
|
16 | const pkg = require('../package.json')
|
17 | require('please-upgrade-node')(
|
18 | Object.assign({}, pkg, {
|
19 | engines: {
|
20 | node: '>=10.13.0',
|
21 | },
|
22 | })
|
23 | )
|
24 |
|
25 | const cmdline = require('commander')
|
26 | const debugLib = require('debug')
|
27 | const lintStaged = require('../lib')
|
28 | const { CONFIG_STDIN_ERROR } = require('../lib/messages')
|
29 |
|
30 | const debug = debugLib('lint-staged:bin')
|
31 |
|
32 | cmdline
|
33 | .version(pkg.version)
|
34 | .option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
|
35 | .option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
|
36 | .option('-d, --debug', 'print additional debug information', false)
|
37 | .option('--no-stash', 'disable the backup stash, and do not revert in case of errors', false)
|
38 | .option(
|
39 | '-p, --concurrent <parallel tasks>',
|
40 | 'the number of tasks to run concurrently, or false to run tasks serially',
|
41 | true
|
42 | )
|
43 | .option('-q, --quiet', 'disable lint-staged’s own console output', false)
|
44 | .option('-r, --relative', 'pass relative filepaths to tasks', false)
|
45 | .option('-x, --shell', 'skip parsing of tasks for better shell support', false)
|
46 | .option(
|
47 | '-v, --verbose',
|
48 | 'show task output even when tasks succeed; by default only failed output is shown',
|
49 | false
|
50 | )
|
51 | .parse(process.argv)
|
52 |
|
53 | if (cmdline.debug) {
|
54 | debugLib.enable('lint-staged*')
|
55 | }
|
56 |
|
57 | debug('Running `lint-staged@%s`', pkg.version)
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const getMaxArgLength = () => {
|
67 | switch (process.platform) {
|
68 | case 'darwin':
|
69 | return 262144
|
70 | case 'win32':
|
71 | return 8191
|
72 | default:
|
73 | return 131072
|
74 | }
|
75 | }
|
76 |
|
77 | const options = {
|
78 | allowEmpty: !!cmdline.allowEmpty,
|
79 | concurrent: cmdline.concurrent,
|
80 | configPath: cmdline.config,
|
81 | debug: !!cmdline.debug,
|
82 | maxArgLength: getMaxArgLength() / 2,
|
83 | stash: !!cmdline.stash,
|
84 | quiet: !!cmdline.quiet,
|
85 | relative: !!cmdline.relative,
|
86 | shell: !!cmdline.shell,
|
87 | verbose: !!cmdline.verbose,
|
88 | }
|
89 |
|
90 | debug('Options parsed from command-line:', options)
|
91 |
|
92 | if (options.configPath === '-') {
|
93 | delete options.configPath
|
94 | try {
|
95 | options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
|
96 | } catch {
|
97 | console.error(CONFIG_STDIN_ERROR)
|
98 | process.exit(1)
|
99 | }
|
100 |
|
101 | try {
|
102 | options.config = JSON.parse(options.config)
|
103 | } catch {
|
104 |
|
105 | }
|
106 | }
|
107 |
|
108 | lintStaged(options)
|
109 | .then((passed) => {
|
110 | process.exitCode = passed ? 0 : 1
|
111 | })
|
112 | .catch(() => {
|
113 | process.exitCode = 1
|
114 | })
|