cd05251452b06e1dbea393763c4564299a0393cde1559a7f57b33886189bbf28a6f308962212d2f0a990ade12d050761c10892fce6484e1a64376e0fffc4f7-exec 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env node
  2. 'use strict'
  3. const fs = require('fs')
  4. // Force colors for packages that depend on https://www.npmjs.com/package/supports-color
  5. const { supportsColor } = require('chalk')
  6. if (supportsColor && supportsColor.level) {
  7. process.env.FORCE_COLOR = supportsColor.level.toString()
  8. }
  9. // Do not terminate main Listr process on SIGINT
  10. process.on('SIGINT', () => {})
  11. const pkg = require('../package.json')
  12. require('please-upgrade-node')(
  13. Object.assign({}, pkg, {
  14. engines: {
  15. node: '>=10.13.0', // First LTS release of 'Dubnium'
  16. },
  17. })
  18. )
  19. const cmdline = require('commander')
  20. const debugLib = require('debug')
  21. const lintStaged = require('../lib')
  22. const { CONFIG_STDIN_ERROR } = require('../lib/messages')
  23. const debug = debugLib('lint-staged:bin')
  24. cmdline
  25. .version(pkg.version)
  26. .option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
  27. .option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
  28. .option('-d, --debug', 'print additional debug information', false)
  29. .option('--no-stash', 'disable the backup stash, and do not revert in case of errors', false)
  30. .option(
  31. '-p, --concurrent <parallel tasks>',
  32. 'the number of tasks to run concurrently, or false to run tasks serially',
  33. true
  34. )
  35. .option('-q, --quiet', 'disable lint-staged’s own console output', false)
  36. .option('-r, --relative', 'pass relative filepaths to tasks', false)
  37. .option('-x, --shell', 'skip parsing of tasks for better shell support', false)
  38. .option(
  39. '-v, --verbose',
  40. 'show task output even when tasks succeed; by default only failed output is shown',
  41. false
  42. )
  43. .parse(process.argv)
  44. if (cmdline.debug) {
  45. debugLib.enable('lint-staged*')
  46. }
  47. debug('Running `lint-staged@%s`', pkg.version)
  48. /**
  49. * Get the maximum length of a command-line argument string based on current platform
  50. *
  51. * https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x
  52. * https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation
  53. * https://unix.stackexchange.com/a/120652
  54. */
  55. const getMaxArgLength = () => {
  56. switch (process.platform) {
  57. case 'darwin':
  58. return 262144
  59. case 'win32':
  60. return 8191
  61. default:
  62. return 131072
  63. }
  64. }
  65. const options = {
  66. allowEmpty: !!cmdline.allowEmpty,
  67. concurrent: cmdline.concurrent,
  68. configPath: cmdline.config,
  69. debug: !!cmdline.debug,
  70. maxArgLength: getMaxArgLength() / 2,
  71. stash: !!cmdline.stash, // commander inverts `no-<x>` flags to `!x`
  72. quiet: !!cmdline.quiet,
  73. relative: !!cmdline.relative,
  74. shell: !!cmdline.shell,
  75. verbose: !!cmdline.verbose,
  76. }
  77. debug('Options parsed from command-line:', options)
  78. if (options.configPath === '-') {
  79. delete options.configPath
  80. try {
  81. options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
  82. } catch {
  83. console.error(CONFIG_STDIN_ERROR)
  84. process.exit(1)
  85. }
  86. try {
  87. options.config = JSON.parse(options.config)
  88. } catch {
  89. // Let config parsing complain if it's not JSON
  90. }
  91. }
  92. lintStaged(options)
  93. .then((passed) => {
  94. process.exitCode = passed ? 0 : 1
  95. })
  96. .catch(() => {
  97. process.exitCode = 1
  98. })