UNPKG

3.12 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3import fs from 'fs'
4import path from 'path'
5import { fileURLToPath } from 'url'
6
7import cmdline 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
26cmdline
27 .version(version)
28 .option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
29 .option(
30 '-p, --concurrent <number|boolean>',
31 'the number of tasks to run concurrently, or false for serial',
32 true
33 )
34 .option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
35 .option('--cwd [path]', 'run all tasks in specific directory, instead of the current')
36 .option('-d, --debug', 'print additional debug information', false)
37 .option('--max-arg-length', 'maximum length of the command-line argument string')
38 .option('--no-stash', 'disable the backup stash, and do not revert in case of errors', false)
39 .option('-q, --quiet', 'disable lint-staged’s own console output', false)
40 .option('-r, --relative', 'pass relative filepaths to tasks', false)
41 .option('-x, --shell [path]', 'skip parsing of tasks for better shell support', false)
42 .option(
43 '-v, --verbose',
44 'show task output even when tasks succeed; by default only failed output is shown',
45 false
46 )
47 .parse(process.argv)
48
49const cmdlineOptions = cmdline.opts()
50
51if (cmdlineOptions.debug) {
52 debug.enable('lint-staged*')
53}
54
55const debugLog = debug('lint-staged:bin')
56debugLog('Running `lint-staged@%s`', version)
57
58const options = {
59 allowEmpty: !!cmdlineOptions.allowEmpty,
60 concurrent: JSON.parse(cmdlineOptions.concurrent),
61 configPath: cmdlineOptions.config,
62 cwd: cmdlineOptions.cwd,
63 debug: !!cmdlineOptions.debug,
64 maxArgLength: JSON.parse(cmdlineOptions.maxArgLength || null),
65 quiet: !!cmdlineOptions.quiet,
66 relative: !!cmdlineOptions.relative,
67 shell: cmdlineOptions.shell /* Either a boolean or a string pointing to the shell */,
68 stash: !!cmdlineOptions.stash, // commander inverts `no-<x>` flags to `!x`
69 verbose: !!cmdlineOptions.verbose,
70}
71
72debugLog('Options parsed from command-line:', options)
73
74if (options.configPath === '-') {
75 delete options.configPath
76 try {
77 options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
78 } catch {
79 console.error(CONFIG_STDIN_ERROR)
80 process.exit(1)
81 }
82
83 try {
84 options.config = JSON.parse(options.config)
85 } catch {
86 // Let config parsing complain if it's not JSON
87 }
88}
89
90lintStaged(options)
91 .then((passed) => {
92 process.exitCode = passed ? 0 : 1
93 })
94 .catch(() => {
95 process.exitCode = 1
96 })