-
Notifications
You must be signed in to change notification settings - Fork 1
/
lint-staged.config.mjs
45 lines (41 loc) · 1.16 KB
/
lint-staged.config.mjs
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
// @ts-check
import { ESLint } from "eslint";
import { quote } from "shell-quote";
const eslint = new ESLint();
const isWin = process.platform === "win32";
/**
* @type {Record<
* string,
* (filenames: string[]) => string | string[] | Promise<string | string[]>
* >}
*/
const config = {
"**/*.{js,jsx,cjs,mjs,ts,tsx}": (filenames) => {
const escapedFileNames = filenames
.map((filename) => `"${isWin ? filename : escapeStr([filename])}"`)
.join(" ");
return [
`pnpm lint --fix ${filenames
.filter(async (file) => !(await eslint.isPathIgnored(file)))
.map((f) => `"${f}"`)
.join(" ")}`,
`pnpm format ${escapedFileNames}`,
`git add ${escapedFileNames}`,
];
},
"**/*.{json,md,mdx,css,html,yml,yaml,scss,sass}": (filenames) => {
const escapedFileNames = filenames
.map((filename) => `"${isWin ? filename : escapeStr([filename])}"`)
.join(" ");
return [`pnpm format ${escapedFileNames}`, `git add ${escapedFileNames}`];
},
};
/**
* @param {string[]} str
* @returns
*/
function escapeStr(str) {
const escaped = quote(str);
return escaped.replace(/\\@/g, "@");
}
export default config;