UNPKG

3.23 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict'
4
5const fs = require('fs')
6
7// Force colors for packages that depend on https://www.npmjs.com/package/supports-color
8const { supportsColor } = require('chalk')
9if (supportsColor && supportsColor.level) {
10 process.env.FORCE_COLOR = supportsColor.level.toString()
11}
12
13// Do not terminate main Listr process on SIGINT
14process.on('SIGINT', () => {})
15
16const pkg = require('../package.json')
17require('please-upgrade-node')(
18 Object.assign({}, pkg, {
19 engines: {
20 node: '>=10.13.0', // First LTS release of 'Dubnium'
21 },
22 })
23)
24
25const cmdline = require('commander')
26const debugLib = require('debug')
27const lintStaged = require('../lib')
28const { CONFIG_STDIN_ERROR } = require('../lib/messages')
29
30const debug = debugLib('lint-staged:bin')
31
32cmdline
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
53if (cmdline.debug) {
54 debugLib.enable('lint-staged*')
55}
56
57debug('Running `lint-staged@%s`', pkg.version)
58
59/**
60 * Get the maximum length of a command-line argument string based on current platform
61 *
62 * https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x
63 * https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation
64 * https://unix.stackexchange.com/a/120652
65 */
66const 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
77const 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, // commander inverts `no-<x>` flags to `!x`
84 quiet: !!cmdline.quiet,
85 relative: !!cmdline.relative,
86 shell: !!cmdline.shell,
87 verbose: !!cmdline.verbose,
88}
89
90debug('Options parsed from command-line:', options)
91
92if (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 // Let config parsing complain if it's not JSON
105 }
106}
107
108lintStaged(options)
109 .then((passed) => {
110 process.exitCode = passed ? 0 : 1
111 })
112 .catch(() => {
113 process.exitCode = 1
114 })