forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-staged.config.js
48 lines (43 loc) · 1.47 KB
/
lint-staged.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { quote } = require('shell-quote')
const { ESLint } = require('eslint')
const eslint = new ESLint()
/**
* Escape filenames to ensure that spaces and such aren't interpreted as
* separators.
*
* @param {string[]} filenames
* @returns {string[]}
*/
function escape(filenames) {
if (process.platform === 'win32') {
return filenames
}
return filenames.map((filename) => quote([filename]).replace(/\\@/g, '@'))
}
module.exports = {
'**/*.{js,jsx,mjs,ts,tsx,mts}': async (filenames) => {
const escapedFileNames = escape(filenames).join(' ')
const eslintFileNames = await Promise.all(
filenames.map(async (filename) => {
const ignored = await eslint.isPathIgnored(filename)
return ignored ? null : filename
})
)
return [
`prettier --with-node-modules --ignore-path .prettierignore --write ${escapedFileNames}`,
`eslint --no-ignore --max-warnings=0 --fix ${escape(eslintFileNames.filter((filename) => filename !== null)).join(' ')}`,
`git add ${escapedFileNames}`,
]
},
'**/*.{json,md,mdx,css,html,yml,yaml,scss}': (filenames) => {
const escapedFileNames = escape(filenames).join(' ')
return [
`prettier --with-node-modules --ignore-path .prettierignore --write ${escapedFileNames}`,
`git add ${escapedFileNames}`,
]
},
'**/*.rs': (filenames) => {
const escapedFileNames = escape(filenames).join(' ')
return [`cargo fmt -- ${escapedFileNames}`, `git add ${escapedFileNames}`]
},
}