lint-staged 16.1.6 → 16.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -112,43 +112,101 @@ Options:
112
112
  --diff [string] override the default "--staged" flag of "git diff" to get list of files. Implies
113
113
  "--no-stash".
114
114
  --diff-filter [string] override the default "--diff-filter=ACMR" flag of "git diff" to get list of files
115
+ --continue-on-error run all tasks to completion even if one fails (default: false)
116
+ --fail-on-changes fail with exit code 1 when tasks modify tracked files (default: false)
115
117
  --max-arg-length [number] maximum length of the command-line argument string (default: 0)
116
118
  --no-revert do not revert to original state in case of errors.
117
119
  --no-stash disable the backup stash. Implies "--no-revert".
118
120
  --no-hide-partially-staged disable hiding unstaged changes from partially staged files
121
+ --hide-unstaged hide all unstaged changes, instead of just partially staged (default: false)
119
122
  -q, --quiet disable lint-staged’s own console output (default: false)
120
123
  -r, --relative pass relative filepaths to tasks (default: false)
121
- -v, --verbose show task output even when tasks succeed; by default only failed output is
122
- shown (default: false)
124
+ -v, --verbose show task output even when tasks succeed; by default only failed output is shown
125
+ (default: false)
123
126
  -h, --help display help for command
124
127
 
125
128
  Any lost modifications can be restored from a git stash:
126
129
 
127
- > git stash list
128
- stash@{0}: automatic lint-staged backup
129
- > git stash apply --index stash@{0}
130
+ > git stash list --format="%h %s"
131
+ h0a0s0h0 On main: lint-staged automatic backup
132
+ > git apply --index h0a0s0h0
130
133
  ```
131
134
 
132
- - **`--allow-empty`**: By default, when tasks undo all staged changes, lint-staged will exit with an error and abort the commit. Use this flag to allow creating empty git commits.
133
- - **`--concurrent [number|boolean]`**: Controls the [concurrency of tasks](#task-concurrency) being run by lint-staged. **NOTE**: This does NOT affect the concurrency of subtasks (they will always be run sequentially). Possible values are:
134
- - `false`: Run all tasks serially
135
- - `true` (default) : _Infinite_ concurrency. Runs as many tasks in parallel as possible.
136
- - `{number}`: Run the specified number of tasks in parallel, where `1` is equivalent to `false`.
137
- - **`--config [path]`**: Manually specify a path to a config file or npm package name. Note: when used, lint-staged won't perform the config file search and will print an error if the specified file cannot be found. If '-' is provided as the filename then the config will be read from stdin, allowing piping in the config like `cat my-config.json | npx lint-staged --config -`.
138
- - **`--cwd [path]`**: By default tasks run in the current working directory. Use the `--cwd some/directory` to override this. The path can be absolute or relative to the current working directory.
139
- - **`--debug`**: Run in debug mode. When set, it does the following:
140
- - uses [debug](https://github.com/visionmedia/debug) internally to log additional information about staged files, commands being executed, location of binaries, etc. Debug logs, which are automatically enabled by passing the flag, can also be enabled by setting the environment variable `$DEBUG` to `lint-staged*`.
141
- - uses [`verbose` renderer](https://listr2.kilic.dev/renderers/verbose-renderer/) for `listr2`; this causes serial, uncoloured output to the terminal, instead of the default (beautified, dynamic) output.
142
- (the [`verbose` renderer](https://listr2.kilic.dev/renderers/verbose-renderer/) can also be activated by setting the `TERM=dumb` or `NODE_ENV=test` environment variables)
143
- - **`--diff`**: By default tasks are filtered against all files staged in git, generated from `git diff --staged`. This option allows you to override the `--staged` flag with arbitrary revisions. For example to get a list of changed files between two branches, use `--diff="branch1...branch2"`. You can also read more from about [git diff](https://git-scm.com/docs/git-diff) and [gitrevisions](https://git-scm.com/docs/gitrevisions). This option also implies `--no-stash`.
144
- - **`--diff-filter`**: By default only files that are _added_, _copied_, _modified_, or _renamed_ are included. Use this flag to override the default `ACMR` value with something else: _added_ (`A`), _copied_ (`C`), _deleted_ (`D`), _modified_ (`M`), _renamed_ (`R`), _type changed_ (`T`), _unmerged_ (`U`), _unknown_ (`X`), or _pairing broken_ (`B`). See also the `git diff` docs for [--diff-filter](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203).
145
- - **`--max-arg-length`**: long commands (a lot of files) are automatically split into multiple chunks when it detects the current shell cannot handle them. Use this flag to override the maximum length of the generated command string.
146
- - **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit.
147
- - **`--no-hide-partially-staged`**: By default, unstaged changes from partially staged files will be hidden and applied back after running tasks. This option will disable this behavior, causing those changes to also be committed.
148
- - **`--quiet`**: Suppress all CLI output, except from tasks.
149
- - **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`.
150
- - **`--no-revert`**: By default all task modifications will be reverted in case of an error. This option will disable the behavior, and apply task modifications to the index before aborting the commit.
151
- - **`--verbose`**: Show task output even when tasks succeed. By default only failed output is shown.
135
+ #### `--allow-empty`
136
+
137
+ By default, when tasks undo all staged changes, lint-staged will exit with an error and abort the commit. Use this flag to allow creating empty git commits.
138
+
139
+ #### `--concurrent [number|boolean]`
140
+
141
+ Controls the [concurrency of tasks](#task-concurrency) being run by lint-staged. **NOTE**: This does NOT affect the concurrency of subtasks (they will always be run sequentially). Possible values are:
142
+
143
+ - `false`: Run all tasks serially
144
+ - `true` (default) : _Infinite_ concurrency. Runs as many tasks in parallel as possible.
145
+ - `{number}`: Run the specified number of tasks in parallel, where `1` is equivalent to `false`.
146
+
147
+ #### `--config [path]`
148
+
149
+ Manually specify a path to a config file or npm package name. Note: when used, lint-staged won't perform the config file search and will print an error if the specified file cannot be found. If '-' is provided as the filename then the config will be read from stdin, allowing piping in the config like `cat my-config.json | npx lint-staged --config -`.
150
+
151
+ #### `--cwd [path]`
152
+
153
+ By default tasks run in the current working directory. Use the `--cwd some/directory` to override this. The path can be absolute or relative to the current working directory.
154
+
155
+ #### `--debug`
156
+
157
+ Run in debug mode. When set, it does the following:
158
+
159
+ - log additional information about staged files, commands being executed, location of binaries, etc.
160
+ - uses [`verbose` renderer](https://listr2.kilic.dev/renderers/verbose-renderer/) for `listr2`; this causes serial, uncoloured output to the terminal, instead of the default (beautified, dynamic) output.
161
+ (the [`verbose` renderer](https://listr2.kilic.dev/renderers/verbose-renderer/) can also be activated by setting the `TERM=dumb` or `NODE_ENV=test` environment variables)
162
+
163
+ #### `--diff`
164
+
165
+ By default tasks are filtered against all files staged in git, generated from `git diff --staged`. This option allows you to override the `--staged` flag with arbitrary revisions. For example to get a list of changed files between two branches, use `--diff="branch1...branch2"`. You can also read more from about [git diff](https://git-scm.com/docs/git-diff) and [gitrevisions](https://git-scm.com/docs/gitrevisions). This option also implies `--no-stash`.
166
+
167
+ #### `--diff-filter [string]`
168
+
169
+ By default only files that are _added_, _copied_, _modified_, or _renamed_ are included. Use this flag to override the default `ACMR` value with something else: _added_ (`A`), _copied_ (`C`), _deleted_ (`D`), _modified_ (`M`), _renamed_ (`R`), _type changed_ (`T`), _unmerged_ (`U`), _unknown_ (`X`), or _pairing broken_ (`B`). See also the `git diff` docs for [--diff-filter](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203).
170
+
171
+ #### `--continue-on-error`
172
+
173
+ By default _lint-staged_ will "exit early" when any of the configured tasks fails, to make sure the runtime is short. With this flag, _lint-staged_ will instead run all tasks to completion and only fail at the end, allowing all task output to be seen.
174
+
175
+ #### `--fail-on-changes`
176
+
177
+ By default changes made by tasks are automatically staged and added to the commit. This flag disables the behavior and makes _lint-staged_ exit with code 1, failing the commit instead. Using this flag also implies the `--no-revert` flag which means any changes made my tasks will be left in the working tree after failing, so that they can be manually staged and the commit tried again.
178
+
179
+ #### `--max-arg-length [number]`
180
+
181
+ long commands (a lot of files) are automatically split into multiple chunks when it detects the current shell cannot handle them. Use this flag to override the maximum length of the generated command string.
182
+
183
+ #### `--no-stash`
184
+
185
+ By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit.
186
+
187
+ #### `--no-hide-partially-staged`
188
+
189
+ By default, unstaged changes from partially staged files will be hidden and applied back after running tasks. This option will disable this behavior, causing those changes to also be committed.
190
+
191
+ #### `--hide-unstaged`
192
+
193
+ Use this option to hide all unstaged changes to tracked files before running tasks. The changes will be applied back after running the tasks. Note that the combination of flags `--hide-unstaged --no-hide-partially-staged` isn't meaningful and behaves the same as just `--hide-unstaged`.
194
+
195
+ #### `--quiet`
196
+
197
+ Suppress all CLI output, except from tasks.
198
+
199
+ #### `--relative`
200
+
201
+ Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`.
202
+
203
+ #### `--no-revert`
204
+
205
+ By default all task modifications will be reverted in case of an error. This option will disable the behavior, and apply task modifications to the index before aborting the commit.
206
+
207
+ #### `--verbose`
208
+
209
+ Show task output even when tasks succeed. By default only failed output is shown.
152
210
 
153
211
  ## Configuration
154
212
 
@@ -912,6 +970,14 @@ You can then run lint-staged against the same files with:
912
970
  npx lint-staged --diff="main...my-branch"
913
971
  ```
914
972
 
973
+ Note that --diff="main..my-branch" will have files that changed on `main` and are not yet caught up on `my-branch` be detected as changed files.
974
+
975
+ To see just that changes on the current branch, as compared to `main` you may wish to use:
976
+
977
+ ```
978
+ npx lint-staged --diff="$(git merge-base main HEAD)"
979
+ ```
980
+
915
981
  </details>
916
982
 
917
983
  ### Can I use `lint-staged` with `ng lint`
@@ -2,70 +2,83 @@
2
2
 
3
3
  import { userInfo } from 'node:os'
4
4
 
5
- import { supportsColor } from 'chalk'
6
5
  import { Option, program } from 'commander'
7
- import debug from 'debug'
8
6
 
7
+ import { createDebug, enableDebug } from '../lib/debug.js'
9
8
  import lintStaged from '../lib/index.js'
10
9
  import { CONFIG_STDIN_ERROR, RESTORE_STASH_EXAMPLE } from '../lib/messages.js'
11
10
  import { readStdin } from '../lib/readStdin.js'
12
11
  import { getVersion } from '../lib/version.js'
13
12
 
14
- // Force colors for packages that depend on https://www.npmjs.com/package/supports-color
15
- if (supportsColor) {
16
- process.env.FORCE_COLOR = supportsColor.level.toString()
17
- }
18
-
19
- const debugLog = debug('lint-staged:bin')
13
+ const debugLog = createDebug('lint-staged:bin')
20
14
 
21
15
  // Do not terminate main Listr process on SIGINT
22
16
  process.on('SIGINT', () => {})
23
17
 
24
- program.version(await getVersion())
25
-
26
- /**
27
- * This shouldn't be necessary for lint-staged, but add migration step just in case
28
- * to preserve old behavior of "commander".
29
- *
30
- * @todo remove this in the major version
31
- * @see https://github.com/tj/commander.js/releases/tag/v13.0.0
32
- * */
33
- program.allowExcessArguments()
34
-
35
- program.option('--allow-empty', 'allow empty commits when tasks revert all staged changes', false)
36
-
37
- program.option(
38
- '-p, --concurrent <number|boolean>',
39
- 'the number of tasks to run concurrently, or false for serial',
40
- true
41
- )
42
-
43
- program.option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
44
-
45
- program.option('--cwd [path]', 'run all tasks in specific directory, instead of the current')
46
-
47
- program.option('-d, --debug', 'print additional debug information', false)
48
-
49
- program.addOption(
50
- new Option(
51
- '--diff [string]',
52
- 'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".'
53
- ).implies({ stash: false })
54
- )
18
+ program
19
+ .version(await getVersion())
55
20
 
56
- program.option(
57
- '--diff-filter [string]',
58
- 'override the default "--diff-filter=ACMR" flag of "git diff" to get list of files'
59
- )
21
+ /**
22
+ * This shouldn't be necessary for lint-staged, but add migration step just in case
23
+ * to preserve old behavior of "commander".
24
+ *
25
+ * @todo remove this in the major version
26
+ * @see https://github.com/tj/commander.js/releases/tag/v13.0.0
27
+ * */
28
+ .allowExcessArguments()
60
29
 
61
- program.option('--max-arg-length [number]', 'maximum length of the command-line argument string', 0)
30
+ .addOption(
31
+ new Option('--allow-empty', 'allow empty commits when tasks revert all staged changes').default(
32
+ false
33
+ )
34
+ )
35
+ .addOption(
36
+ new Option(
37
+ '-p, --concurrent <number|boolean>',
38
+ 'the number of tasks to run concurrently, or false for serial'
39
+ ).default(true)
40
+ )
41
+ .addOption(
42
+ new Option('-c, --config [path]', 'path to configuration file, or - to read from stdin')
43
+ )
44
+ .addOption(
45
+ new Option('--cwd [path]', 'run all tasks in specific directory, instead of the current')
46
+ )
47
+ .addOption(new Option('-d, --debug', 'print additional debug information').default(false))
48
+ .addOption(
49
+ new Option(
50
+ '--diff [string]',
51
+ 'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".'
52
+ ).implies({ stash: false })
53
+ )
54
+ .addOption(
55
+ new Option(
56
+ '--diff-filter [string]',
57
+ 'override the default "--diff-filter=ACMR" flag of "git diff" to get list of files'
58
+ )
59
+ )
60
+ .addOption(
61
+ new Option('--continue-on-error', 'run all tasks to completion even if one fails').default(
62
+ false
63
+ )
64
+ )
65
+ .addOption(
66
+ new Option('--fail-on-changes', 'fail with exit code 1 when tasks modify tracked files')
67
+ .default(false)
68
+ .implies({ revert: false })
69
+ )
70
+ .addOption(
71
+ new Option(
72
+ '--max-arg-length [number]',
73
+ 'maximum length of the command-line argument string'
74
+ ).default(0)
75
+ )
62
76
 
63
- /**
64
- * We don't want to show the `--revert` flag because it's on by default, and only show the
65
- * negatable flag `--no-rever` instead. There seems to be a bug in Commander.js where
66
- * configuring only the latter won't actually set the default value.
67
- */
68
- program
77
+ /**
78
+ * We don't want to show the `--revert` flag because it's on by default, and only show the
79
+ * negatable flag `--no-rever` instead. There seems to be a bug in Commander.js where
80
+ * configuring only the latter won't actually set the default value.
81
+ */
69
82
  .addOption(
70
83
  new Option('--revert', 'revert to original state in case of errors').default(true).hideHelp()
71
84
  )
@@ -73,7 +86,6 @@ program
73
86
  new Option('--no-revert', 'do not revert to original state in case of errors.').default(false)
74
87
  )
75
88
 
76
- program
77
89
  .addOption(new Option('--stash', 'enable the backup stash').default(true).hideHelp())
78
90
  .addOption(
79
91
  new Option('--no-stash', 'disable the backup stash. Implies "--no-revert".')
@@ -81,7 +93,6 @@ program
81
93
  .implies({ revert: false })
82
94
  )
83
95
 
84
- program
85
96
  .addOption(
86
97
  new Option('--hide-partially-staged', 'hide unstaged changes from partially staged files')
87
98
  .default(true)
@@ -94,38 +105,46 @@ program
94
105
  ).default(false)
95
106
  )
96
107
 
97
- program.option('-q, --quiet', 'disable lint-staged’s own console output', false)
98
-
99
- program.option('-r, --relative', 'pass relative filepaths to tasks', false)
108
+ .addOption(
109
+ new Option('--hide-unstaged', 'hide all unstaged changes, instead of just partially staged')
110
+ .default(false)
111
+ .implies({ hidePartiallyStaged: false })
112
+ )
100
113
 
101
- program.option(
102
- '-v, --verbose',
103
- 'show task output even when tasks succeed; by default only failed output is shown',
104
- false
105
- )
114
+ .addOption(new Option('-q, --quiet', 'disable lint-staged’s own console output').default(false))
115
+ .addOption(new Option('-r, --relative', 'pass relative filepaths to tasks').default(false))
116
+ .addOption(
117
+ new Option(
118
+ '-v, --verbose',
119
+ 'show task output even when tasks succeed; by default only failed output is shown'
120
+ ).default(false)
121
+ )
106
122
 
107
- program.addHelpText('afterAll', '\n' + RESTORE_STASH_EXAMPLE)
123
+ .addHelpText('afterAll', '\n' + RESTORE_STASH_EXAMPLE)
108
124
 
109
125
  const cliOptions = program.parse(process.argv).opts()
110
126
 
111
127
  if (cliOptions.debug) {
112
- debug.enable('lint-staged*')
128
+ enableDebug()
113
129
  }
114
130
 
115
131
  const options = {
116
132
  allowEmpty: !!cliOptions.allowEmpty,
117
133
  concurrent: JSON.parse(cliOptions.concurrent),
118
134
  configPath: cliOptions.config,
135
+ continueOnError: !!cliOptions.continueOnError,
119
136
  cwd: cliOptions.cwd,
120
137
  debug: !!cliOptions.debug,
121
138
  diff: cliOptions.diff,
122
139
  diffFilter: cliOptions.diffFilter,
140
+ failOnChanges: !!cliOptions.failOnChanges,
141
+ hidePartiallyStaged: !!cliOptions.hidePartiallyStaged, // commander inverts `no-<x>` flags to `!x`
142
+ hideUnstaged: !!cliOptions.hideUnstaged,
123
143
  maxArgLength: cliOptions.maxArgLength || undefined,
124
144
  quiet: !!cliOptions.quiet,
125
145
  relative: !!cliOptions.relative,
126
146
  revert: !!cliOptions.revert, // commander inverts `no-<x>` flags to `!x`
127
147
  stash: !!cliOptions.stash, // commander inverts `no-<x>` flags to `!x`
128
- hidePartiallyStaged: !!cliOptions.hidePartiallyStaged, // commander inverts `no-<x>` flags to `!x`
129
148
  verbose: !!cliOptions.verbose,
130
149
  }
131
150
 
@@ -150,9 +169,7 @@ if (options.configPath === '-') {
150
169
  }
151
170
  }
152
171
 
153
- try {
154
- const passed = await lintStaged(options)
155
- process.exitCode = passed ? 0 : 1
156
- } catch {
172
+ const passed = await lintStaged(options)
173
+ if (!passed) {
157
174
  process.exitCode = 1
158
175
  }
package/lib/chunkFiles.js CHANGED
@@ -1,10 +1,9 @@
1
1
  import path from 'node:path'
2
2
 
3
- import debug from 'debug'
4
-
3
+ import { createDebug } from './debug.js'
5
4
  import { normalizePath } from './normalizePath.js'
6
5
 
7
- const debugLog = debug('lint-staged:chunkFiles')
6
+ const debugLog = createDebug('lint-staged:chunkFiles')
8
7
 
9
8
  /**
10
9
  * Chunk array into sub-arrays
package/lib/colors.js ADDED
@@ -0,0 +1,106 @@
1
+ import nodeTty from 'node:tty'
2
+
3
+ /**
4
+ * @example NO_COLOR
5
+ * @exmaple NO_COLOR=1
6
+ * @exmaple NO_COLOR=true
7
+ */
8
+ const TRUTHRY_ENV_VAR_VALUES = ['', '1', 'true']
9
+
10
+ /**
11
+ * @exmaple FORCE_COLOR=0
12
+ * @exmaple FORCE_COLOR=false
13
+ */
14
+ const FALSY_ENV_VAR_VALUES = ['0', 'false']
15
+
16
+ /**
17
+ * @returns `true` if ANSI colors are supported
18
+ *
19
+ * @param {NodeJS.Process} [p]
20
+ * @param {boolean} [isTty]
21
+ */
22
+ export const supportsAnsiColors = (p = process, isTty = nodeTty.isatty(1)) => {
23
+ const noColor = p?.env?.NO_COLOR?.toLowerCase()
24
+ if (TRUTHRY_ENV_VAR_VALUES.includes(noColor)) {
25
+ return false
26
+ }
27
+
28
+ const forceColor = p?.env?.FORCE_COLOR?.toLowerCase()
29
+ if (TRUTHRY_ENV_VAR_VALUES.includes(forceColor)) {
30
+ return true
31
+ } else if (FALSY_ENV_VAR_VALUES.includes(forceColor)) {
32
+ return false
33
+ }
34
+
35
+ const forceTty = p?.env?.FORCE_TTY
36
+ if (TRUTHRY_ENV_VAR_VALUES.includes(forceTty)) {
37
+ return true
38
+ } else if (FALSY_ENV_VAR_VALUES.includes(forceTty)) {
39
+ return false
40
+ }
41
+
42
+ if (isTty) {
43
+ return true
44
+ }
45
+
46
+ /**
47
+ * Assume CI supports color
48
+ * @see {@link https://github.com/alexeyraspopov/picocolors/blob/0e7c4af2de299dd7bc5916f2bddd151fa2f66740/picocolors.js#L4}
49
+ * @see {@link https://github.com/tinylibs/tinyrainbow/blob/071034bf2eafa28d91ef0ba48a3837420d81a40a/src/index.ts#L91}
50
+ */
51
+ if (TRUTHRY_ENV_VAR_VALUES.includes(p?.env?.CI)) {
52
+ return true
53
+ }
54
+
55
+ if (p?.env?.TERM && p.env.TERM === 'dumb') {
56
+ return false
57
+ }
58
+
59
+ /**
60
+ * Assume Windows supports color
61
+ * @see {@link https://github.com/alexeyraspopov/picocolors/blob/0e7c4af2de299dd7bc5916f2bddd151fa2f66740/picocolors.js#L4}
62
+ * @see {@link https://github.com/tinylibs/tinyrainbow/blob/071034bf2eafa28d91ef0ba48a3837420d81a40a/src/index.ts#L89}
63
+ */
64
+ if (p?.platform === 'win32') {
65
+ return true
66
+ }
67
+
68
+ return false
69
+ }
70
+
71
+ /**
72
+ * @deprecated replace this with Node.js builtin after minimum supported version is >=20.18.0
73
+ * @example util.styleText('red', 'test') !== 'text'
74
+ */
75
+ export const SUPPORTS_COLOR = supportsAnsiColors()
76
+
77
+ const ANSI_RESET = '\u001B[0m'
78
+
79
+ /**
80
+ * @callback WrapAnsi
81
+ * @param {string} text
82
+ * @returns {string}
83
+ */
84
+ /**
85
+ * @deprecated replace this with Node.js builtin after minimum supported version is >=20.18.0
86
+ * @example (format) => (text) => util.styleText(format, text)
87
+ *
88
+ * @param {string} code
89
+ * @param {boolean} [supported]
90
+ * @returns {WrapAnsi}
91
+ *
92
+ */
93
+ export const wrapAnsiColor = (code, supported = SUPPORTS_COLOR) => {
94
+ if (supported) {
95
+ return (text) => code + text + ANSI_RESET
96
+ }
97
+
98
+ return (text) => text
99
+ }
100
+
101
+ export const red = wrapAnsiColor('\u001B[0;31m')
102
+ export const green = wrapAnsiColor('\u001B[0;32m')
103
+ export const yellow = wrapAnsiColor('\u001B[0;33m')
104
+ export const blue = wrapAnsiColor('\u001B[0;34m')
105
+ export const blackBright = wrapAnsiColor('\u001B[0;90m')
106
+ export const bold = wrapAnsiColor('\u001b[1m')
package/lib/debug.js ADDED
@@ -0,0 +1,27 @@
1
+ import { formatWithOptions } from 'node:util'
2
+
3
+ import { blackBright, SUPPORTS_COLOR } from './colors.js'
4
+
5
+ const format = (...args) => formatWithOptions({ colors: SUPPORTS_COLOR }, ...args)
6
+
7
+ let activeLogger
8
+
9
+ export const enableDebug = (logger = console) => {
10
+ if (!activeLogger) {
11
+ activeLogger = logger
12
+ }
13
+ }
14
+
15
+ /** @param {string} name */
16
+ export const createDebug = (name) => {
17
+ let previous = process.hrtime.bigint()
18
+
19
+ return (...args) => {
20
+ if (!activeLogger) return
21
+
22
+ const now = process.hrtime.bigint()
23
+ const ms = (now - previous) / 1_000_000n
24
+ previous = now
25
+ activeLogger.debug(blackBright(name + ': ') + format(...args) + blackBright(` +${ms}ms`))
26
+ }
27
+ }
package/lib/execGit.js CHANGED
@@ -1,7 +1,8 @@
1
- import debug from 'debug'
2
1
  import spawn, { SubprocessError } from 'nano-spawn'
3
2
 
4
- const debugLog = debug('lint-staged:execGit')
3
+ import { createDebug } from './debug.js'
4
+
5
+ const debugLog = createDebug('lint-staged:execGit')
5
6
 
6
7
  /**
7
8
  * Explicitly never recurse commands into submodules, overriding local/global configuration.
package/lib/figures.js CHANGED
@@ -1,8 +1,9 @@
1
- import chalk from 'chalk'
2
1
  import { figures } from 'listr2'
3
2
 
4
- export const info = chalk.blue(figures.arrowRight)
3
+ import { blue, red, yellow } from './colors.js'
5
4
 
6
- export const error = chalk.redBright(figures.cross)
5
+ export const info = blue(figures.arrowRight)
7
6
 
8
- export const warning = chalk.yellow(figures.warning)
7
+ export const error = red(figures.cross)
8
+
9
+ export const warning = yellow(figures.warning)
package/lib/file.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import fs from 'node:fs/promises'
2
2
 
3
- import debug from 'debug'
3
+ import { createDebug } from './debug.js'
4
4
 
5
- const debugLog = debug('lint-staged:file')
5
+ const debugLog = createDebug('lint-staged:file')
6
6
 
7
7
  /**
8
8
  * Read contents of a file to buffer
@@ -1,11 +1,11 @@
1
1
  import path from 'node:path'
2
2
 
3
- import debug from 'debug'
4
3
  import micromatch from 'micromatch'
5
4
 
5
+ import { createDebug } from './debug.js'
6
6
  import { normalizePath } from './normalizePath.js'
7
7
 
8
- const debugLog = debug('lint-staged:generateTasks')
8
+ const debugLog = createDebug('lint-staged:generateTasks')
9
9
 
10
10
  /**
11
11
  * Generates all task commands, and filelist
@@ -1,8 +1,7 @@
1
- import debug from 'debug'
2
-
1
+ import { createDebug } from './debug.js'
3
2
  import { makeErr } from './getSpawnedTask.js'
4
3
 
5
- const debugLog = debug('lint-staged:getFunctionTasks')
4
+ const debugLog = createDebug('lint-staged:getFunctionTasks')
6
5
 
7
6
  /**
8
7
  * @typedef {{ title: string; task: Function }} FunctionTask
@@ -13,7 +13,7 @@ const bindLogger = (consoleLogMethod) =>
13
13
  },
14
14
  })
15
15
 
16
- const getMainRendererOptions = ({ debug, quiet }, logger, env) => {
16
+ const getMainRendererOptions = ({ color, debug, quiet }, logger, env) => {
17
17
  if (quiet) {
18
18
  return {
19
19
  renderer: 'silent',
@@ -31,8 +31,7 @@ const getMainRendererOptions = ({ debug, quiet }, logger, env) => {
31
31
  }
32
32
  }
33
33
 
34
- // Better support for dumb terminals: https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals
35
- if (debug || env.TERM === 'dumb') {
34
+ if (debug || !color) {
36
35
  return {
37
36
  renderer: 'verbose',
38
37
  }
@@ -46,19 +45,19 @@ const getMainRendererOptions = ({ debug, quiet }, logger, env) => {
46
45
  }
47
46
  }
48
47
 
49
- const getFallbackRenderer = ({ renderer }, { FORCE_COLOR }) => {
50
- if (renderer === 'silent' || renderer === 'test' || Number(FORCE_COLOR) > 0) {
48
+ const getFallbackRenderer = ({ renderer }, { color = false }) => {
49
+ if (renderer === 'silent' || renderer === 'test' || !color) {
51
50
  return renderer
52
51
  }
53
52
 
54
53
  return 'verbose'
55
54
  }
56
55
 
57
- export const getRenderer = (options, logger, env = process.env) => {
58
- const mainRendererOptions = getMainRendererOptions(options, logger, env)
56
+ export const getRenderer = ({ color, debug, quiet }, logger, env = process.env) => {
57
+ const mainRendererOptions = getMainRendererOptions({ color, debug, quiet }, logger, env)
59
58
 
60
59
  return {
61
60
  ...mainRendererOptions,
62
- fallbackRenderer: getFallbackRenderer(mainRendererOptions, env),
61
+ fallbackRenderer: getFallbackRenderer(mainRendererOptions, { color }),
63
62
  }
64
63
  }