Skip to content

Commit f895e97

Browse files
authored
Merge pull request #1289 from okonet/updates-2023-04-20
Drop Node.js v14, run tests for 20, update dependencies
2 parents 5cead2d + 217c404 commit f895e97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2783
-2137
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ on:
99
- alpha
1010
# Do not run on tags
1111
tags-ignore:
12-
- "*"
12+
- '*'
1313
pull_request:
1414
# Run on to branches with an open PR
1515
branches:
16-
- "*"
16+
- '*'
1717

1818
permissions:
1919
contents: read
@@ -26,7 +26,7 @@ jobs:
2626
- uses: actions/checkout@v3
2727
- uses: actions/setup-node@v3
2828
with:
29-
node-version: 18
29+
node-version: 20
3030
# Install node_modules
3131
- uses: actions/cache@v3
3232
id: cache-node_modules
@@ -42,11 +42,11 @@ jobs:
4242
test:
4343
strategy:
4444
matrix:
45-
# Test with Node.js v14 (LTS), v16 (LTS), and 18 (Current)
45+
# Test with Node.js v16 (LTS), v18 (LTS), and v20 (Current)
4646
node:
47-
- 14
4847
- 16
4948
- 18
49+
- 20
5050
# Test with Ubuntu, macOS, and Windows
5151
os:
5252
- ubuntu-latest
@@ -124,7 +124,7 @@ jobs:
124124
- uses: actions/checkout@v3
125125
- uses: actions/setup-node@v3
126126
with:
127-
node-version: 18 # release using Node.js LTS
127+
node-version: 20
128128
# Release using semantic-release.
129129
# While this runs on all branches, it will only release latest from master
130130
- uses: codfish/semantic-release-action@v2

.prettierrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"printWidth": 100,
33
"singleQuote": true,
4-
"semi": false
4+
"semi": false,
5+
"trailingComma": "es5"
56
}

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ See [Releases](https://github.com/okonet/lint-staged/releases).
7272

7373
### Migration
7474

75+
#### v14
76+
77+
- Since `v14.0.0` _lint-staged_ no longer supports Node.js 14. Please upgrade your Node.js version to at least `16.14.0`.
78+
7579
#### v13
7680

7781
- Since `v13.0.0` _lint-staged_ no longer supports Node.js 12. Please upgrade your Node.js version to at least `14.13.1`, or `16.0.0` onward.
@@ -218,11 +222,11 @@ If necessary, you can limit the concurrency using `--concurrent <number>` or dis
218222
Linter commands work on a subset of all staged files, defined by a _glob pattern_. lint-staged uses [micromatch](https://github.com/micromatch/micromatch) for matching files with the following rules:
219223

220224
- If the glob pattern contains no slashes (`/`), micromatch's `matchBase` option will enabled, so globs match a file's basename regardless of directory:
221-
- **`"*.js"`** will match all JS files, like `/test.js` and `/foo/bar/test.js`
222-
- **`"!(*test).js"`**. will match all JS files, except those ending in `test.js`, so `foo.js` but not `foo.test.js`
225+
- `"*.js"` will match all JS files, like `/test.js` and `/foo/bar/test.js`
226+
- `"!(*test).js"` will match all JS files, except those ending in `test.js`, so `foo.js` but not `foo.test.js`
223227
- If the glob pattern does contain a slash (`/`), it will match for paths as well:
224-
- **`"./*.js"`** will match all JS files in the git repo root, so `/test.js` but not `/foo/bar/test.js`
225-
- **`"foo/**/*.js"`** will match all JS files inside the `/foo` directory, so `/foo/bar/test.js` but not `/test.js`
228+
- `"./*.js"` will match all JS files in the git repo root, so `/test.js` but not `/foo/bar/test.js`
229+
- `"foo/**/*.js"` will match all JS files inside the `/foo` directory, so `/foo/bar/test.js` but not `/test.js`
226230

227231
When matching, lint-staged will do the following
228232

@@ -624,9 +628,7 @@ See more on [this blog post](https://medium.com/@tomchentw/imagemin-lint-staged-
624628
const path = require('path')
625629

626630
const buildEslintCommand = (filenames) =>
627-
`next lint --fix --file ${filenames
628-
.map((f) => path.relative(process.cwd(), f))
629-
.join(' --file ')}`
631+
`next lint --fix --file ${filenames.map((f) => path.relative(process.cwd(), f)).join(' --file ')}`
630632

631633
module.exports = {
632634
'*.{js,jsx,ts,tsx}': [buildEslintCommand],

bin/lint-staged.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env node
22

3-
import fs from 'node:fs'
4-
import path from 'node:path'
5-
import { fileURLToPath } from 'node:url'
3+
import fs from 'node:fs/promises'
64

75
import { supportsColor } from 'chalk'
86
import { Option, program } from 'commander'
@@ -19,8 +17,7 @@ if (supportsColor) {
1917
// Do not terminate main Listr process on SIGINT
2018
process.on('SIGINT', () => {})
2119

22-
const packageJsonPath = path.join(fileURLToPath(import.meta.url), '../../package.json')
23-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath))
20+
const packageJson = JSON.parse(await fs.readFile(new URL('../package.json', import.meta.url)))
2421
const version = packageJson.version
2522

2623
const debugLog = debug('lint-staged:bin')
@@ -113,7 +110,7 @@ debugLog('Options parsed from command-line:', options)
113110
if (options.configPath === '-') {
114111
delete options.configPath
115112
try {
116-
options.config = fs.readFileSync(process.stdin.fd, 'utf8').toString().trim()
113+
options.config = await fs.readFile(process.stdin.fd, 'utf8').toString().trim()
117114
} catch {
118115
console.error(CONFIG_STDIN_ERROR)
119116
process.exit(1)
@@ -126,10 +123,9 @@ if (options.configPath === '-') {
126123
}
127124
}
128125

129-
lintStaged(options)
130-
.then((passed) => {
131-
process.exitCode = passed ? 0 : 1
132-
})
133-
.catch(() => {
134-
process.exitCode = 1
135-
})
126+
try {
127+
const passed = await lintStaged(options)
128+
process.exitCode = passed ? 0 : 1
129+
} catch {
130+
process.exitCode = 1
131+
}

lib/chunkFiles.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'node:path'
22

33
import debug from 'debug'
4-
import normalize from 'normalize-path'
4+
5+
import { normalizePath } from './normalizePath.js'
56

67
const debugLog = debug('lint-staged:chunkFiles')
78

@@ -35,7 +36,7 @@ const chunkArray = (arr, chunkCount) => {
3536
*/
3637
export const chunkFiles = ({ files, baseDir, maxArgLength = null, relative = false }) => {
3738
const normalizedFiles = files.map((file) =>
38-
normalize(relative || !baseDir ? file : path.resolve(baseDir, file))
39+
normalizePath(relative || !baseDir ? file : path.resolve(baseDir, file))
3940
)
4041

4142
if (!maxArgLength) {

lib/generateTasks.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import path from 'node:path'
22

33
import debug from 'debug'
44
import micromatch from 'micromatch'
5-
import normalize from 'normalize-path'
5+
6+
import { normalizePath } from './normalizePath.js'
67

78
const debugLog = debug('lint-staged:generateTasks')
89

@@ -19,7 +20,7 @@ const debugLog = debug('lint-staged:generateTasks')
1920
export const generateTasks = ({ config, cwd = process.cwd(), files, relative = false }) => {
2021
debugLog('Generating linter tasks')
2122

22-
const relativeFiles = files.map((file) => normalize(path.relative(cwd, file)))
23+
const relativeFiles = files.map((file) => normalizePath(path.relative(cwd, file)))
2324

2425
return Object.entries(config).map(([pattern, commands]) => {
2526
const isParentDirPattern = pattern.startsWith('../')
@@ -42,7 +43,7 @@ export const generateTasks = ({ config, cwd = process.cwd(), files, relative = f
4243
strictBrackets: true,
4344
})
4445

45-
const fileList = matches.map((file) => normalize(relative ? file : path.resolve(cwd, file)))
46+
const fileList = matches.map((file) => normalizePath(relative ? file : path.resolve(cwd, file)))
4647

4748
const task = { pattern, commands, fileList }
4849
debugLog('Generated task: \n%O', task)

lib/getRenderer.js

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
1-
const getMainRendererOptions = ({ debug, quiet }, env) => {
2-
if (quiet) return { renderer: 'silent' }
1+
import { EOL } from 'node:os'
2+
import { Writable } from 'node:stream'
3+
4+
import { ListrLogger, ProcessOutput } from 'listr2'
5+
6+
const EOLRegex = new RegExp(EOL + '$')
7+
8+
const bindLogger = (consoleLogMethod) =>
9+
new Writable({
10+
write: function (chunk, encoding, next) {
11+
consoleLogMethod(chunk.toString().replace(EOLRegex, ''))
12+
next()
13+
},
14+
})
15+
16+
const getMainRendererOptions = ({ debug, quiet }, logger, env) => {
17+
if (quiet) {
18+
return {
19+
renderer: 'silent',
20+
}
21+
}
22+
23+
if (env.NODE_ENV === 'test') {
24+
return {
25+
renderer: 'test',
26+
rendererOptions: {
27+
logger: new ListrLogger({
28+
processOutput: new ProcessOutput(bindLogger(logger.log), bindLogger(logger.error)),
29+
}),
30+
},
31+
}
32+
}
33+
334
// Better support for dumb terminals: https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals
4-
const isDumbTerminal = env.TERM === 'dumb'
5-
if (debug || isDumbTerminal || env.NODE_ENV === 'test') return { renderer: 'verbose' }
6-
return { renderer: 'update', rendererOptions: { dateFormat: false } }
7-
}
35+
if (debug || env.TERM === 'dumb') {
36+
return {
37+
renderer: 'verbose',
38+
}
39+
}
840

9-
const getFallbackRenderer = ({ renderer }, { FORCE_COLOR }) => {
10-
if (renderer === 'silent') {
11-
return 'silent'
41+
return {
42+
renderer: 'update',
43+
rendererOptions: {
44+
formatOutput: 'truncate',
45+
},
1246
}
47+
}
1348

14-
// If colors are being forced, then also force non-fallback rendering
15-
if (Number(FORCE_COLOR) > 0) {
49+
const getFallbackRenderer = ({ renderer }, { FORCE_COLOR }) => {
50+
if (renderer === 'silent' || renderer === 'test' || Number(FORCE_COLOR) > 0) {
1651
return renderer
1752
}
1853

1954
return 'verbose'
2055
}
2156

22-
export const getRenderer = (options, env = process.env) => {
23-
const mainRendererOptions = getMainRendererOptions(options, env)
57+
export const getRenderer = (options, logger, env = process.env) => {
58+
const mainRendererOptions = getMainRendererOptions(options, logger, env)
59+
2460
return {
2561
...mainRendererOptions,
26-
nonTTYRenderer: getFallbackRenderer(mainRendererOptions, env),
62+
fallbackRenderer: getFallbackRenderer(mainRendererOptions, env),
2763
}
2864
}

lib/getStagedFiles.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import path from 'node:path'
22

3-
import normalize from 'normalize-path'
4-
53
import { execGit } from './execGit.js'
64
import { getDiffCommand } from './getDiffCommand.js'
5+
import { normalizePath } from './normalizePath.js'
76
import { parseGitZOutput } from './parseGitZOutput.js'
87

98
export const getStagedFiles = async ({ cwd = process.cwd(), diff, diffFilter } = {}) => {
109
try {
1110
const lines = await execGit(getDiffCommand(diff, diffFilter), { cwd })
1211
if (!lines) return []
1312

14-
return parseGitZOutput(lines).map((file) => normalize(path.resolve(cwd, file)))
13+
return parseGitZOutput(lines).map((file) => normalizePath(path.resolve(cwd, file)))
1514
} catch {
1615
return null
1716
}

lib/makeCmdTasks.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
1-
import cliTruncate from 'cli-truncate'
21
import debug from 'debug'
32

43
import { configurationError } from './messages.js'
54
import { resolveTaskFn } from './resolveTaskFn.js'
65

76
const debugLog = debug('lint-staged:makeCmdTasks')
87

9-
const STDOUT_COLUMNS_DEFAULT = 80
10-
11-
const listrPrefixLength = {
12-
update: ` X `.length, // indented task title where X is a checkmark or a cross (failure)
13-
verbose: `[STARTED] `.length, // verbose renderer uses 7-letter STARTED/SUCCESS prefixes
14-
}
15-
16-
/**
17-
* Get length of title based on the number of available columns prefix length
18-
* @param {string} renderer The name of the Listr renderer
19-
* @returns {number}
20-
*/
21-
const getTitleLength = (renderer, columns = process.stdout.columns) => {
22-
const prefixLength = listrPrefixLength[renderer] || 0
23-
return (columns || STDOUT_COLUMNS_DEFAULT) - prefixLength
24-
}
25-
268
/**
279
* Creates and returns an array of listr tasks which map to the given commands.
2810
*
@@ -31,11 +13,10 @@ const getTitleLength = (renderer, columns = process.stdout.columns) => {
3113
* @param {string} options.cwd
3214
* @param {Array<string>} options.files
3315
* @param {string} options.gitDir
34-
* @param {string} options.renderer
3516
* @param {Boolean} shell
3617
* @param {Boolean} verbose
3718
*/
38-
export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, shell, verbose }) => {
19+
export const makeCmdTasks = async ({ commands, cwd, files, gitDir, shell, verbose }) => {
3920
debugLog('Creating listr tasks for commands %o', commands)
4021
const commandArray = Array.isArray(commands) ? commands : [commands]
4122
const cmdTasks = []
@@ -60,10 +41,8 @@ export const makeCmdTasks = async ({ commands, cwd, files, gitDir, renderer, she
6041
)
6142
}
6243

63-
// Truncate title to single line based on renderer
64-
const title = cliTruncate(command, getTitleLength(renderer))
6544
const task = resolveTaskFn({ command, cwd, files, gitDir, isFn, shell, verbose })
66-
cmdTasks.push({ title, command, task })
45+
cmdTasks.push({ title: command, command, task })
6746
}
6847
}
6948

lib/messages.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
import { inspect } from 'node:util'
2+
13
import chalk from 'chalk'
2-
import inspect from 'object-inspect'
34

45
import { error, info, warning } from './figures.js'
56

67
export const configurationError = (opt, helpMsg, value) =>
78
`${chalk.redBright(`${error} Validation Error:`)}
89
9-
Invalid value for '${chalk.bold(opt)}': ${chalk.bold(
10-
inspect(value, { inlineCharacterLimit: Number.POSITIVE_INFINITY })
11-
)}
10+
Invalid value for '${chalk.bold(opt)}': ${chalk.bold(inspect(value))}
1211
1312
${helpMsg}`
1413

0 commit comments

Comments
 (0)