-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-staged.config.js
169 lines (157 loc) · 3.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import mm from 'micromatch'
import { constants as fsConstants } from 'node:fs'
import { access } from 'node:fs/promises'
import path from 'node:path'
/**
* @typedef {{string|string[]}} Commands
* @typedef {(filenames: string[]) => Commands} Task
*/
const commands = {
prettier: ['prettier', '--cache', '--ignore-unknown', '--write'],
eslint: ['eslint', '--cache', '--fix'],
wrangler: ['wrangler', 'publish', '--dry-run'],
vitest: ['vitest', 'run'],
tsc: ['tsc', '-p', 'tsconfig.json', '--noEmit'],
esbuild: [
'esbuild',
'--bundle',
'--outfile=/dev/null',
'--tsconfig=tsconfig.json',
'src/index.ts',
],
devskim: [
'devskim',
'analyze',
'--ignore-globs',
'**/.git/**,**/bin/**,**/node_modules/**,**/test/**',
'.',
],
njsscan: ['njsscan'],
taplo: ['taplo', 'format'],
}
/**
* @param {string[]} filenames
* @param {string[]} patterns
* @param {import('micromatch').Options} options
* @returns {boolean}
*/
function matchSome(
filenames,
patterns,
options = { dot: true, basename: true },
) {
return mm.some(filenames, patterns, options)
}
/**
* @param {...Task} tasks
* @returns {Task}
*/
function combine(...tasks) {
return async (filenames) => {
return await Promise.all(tasks.map((task) => task(filenames)))
}
}
/**
*
* @param {import('node:fs').PathLike} p
* @param {number} mode
* @returns {Promise<boolean>}
*/
async function tryAccess(p, mode = fsConstants.F_OK) {
try {
await access(p, mode)
return true
} catch {
return false
}
}
/**
* @param {string} command
* @returns {Promise<boolean>}
*/
async function hasCommand(command) {
const PATH = process.env.PATH.split(':')
const bins = PATH.map((dir) => path.join(dir, command))
const exists = await Promise.all(
bins.map((bin) => tryAccess(bin, fsConstants.X_OK)),
)
return exists.some(Boolean)
}
/** @type {Record<string, Task>} */
const tasks = {
prettier: async (files) => {
if (matchSome(files, ['.prettierrc*', 'prettier.config.*'])) {
return [...commands.prettier, '.'].join(' ')
} else {
return [...commands.prettier, ...files].join(' ')
}
},
eslint: async (files) => {
if (matchSome(files, ['.eslintrc*', 'eslint.config.*'])) {
return [...commands.eslint, '.'].join(' ')
} else {
return [...commands.eslint, ...files].join(' ')
}
},
wrangler: () => commands.wrangler.join(' '),
vitest: () => commands.vitest.join(' '),
tsc: () => commands.tsc.join(' '),
esbuild: () => commands.esbuild.join(' '),
devskim: async () => {
if (await hasCommand(commands.devskim[0])) {
return commands.devskim.join(' ')
} else {
return 'true'
}
},
njsscan: async (files) => {
if (await hasCommand(commands.njsscan[0])) {
return [...commands.njsscan, ...files].join(' ')
} else {
return 'true'
}
},
taplo: async (files) => {
if (await hasCommand(commands.taplo[0])) {
return [...commands.taplo, ...files].join(' ')
} else {
return 'true'
}
},
}
const filetypes = {
js: ['js', 'cjs', 'mjs', 'jsx'],
ts: ['ts', 'tsx'],
json: ['json', 'jsonc'],
md: ['md', 'mdx'],
yaml: ['yaml', 'yml'],
toml: ['toml'],
}
/**
* @param {...string} types
* @returns {string}
*/
function fts(...types) {
const exts = types.flatMap((type) => filetypes[type])
return `*.{${exts.join()}}`
}
/**
* @param {...string} patterns
* @returns {string}
*/
function glob(...patterns) {
return `{${patterns.join()}}`
}
/** @type {Record<string, Task>} */
const config = {
'src/**/*': tasks.esbuild,
'tsconfig.json': tasks.tsc,
'wrangler.toml': combine(tasks.esbuild, tasks.wrangler),
'*!(.spec.ts)': tasks.devskim,
'*.toml': tasks.taplo,
[`src/**/${fts('js', 'ts')}`]: tasks.njsscan,
[glob('vitest.config.ts', '{src,test}/**/*')]: tasks.vitest,
[glob('eslint.config.*', '.eslintrc.*', fts('js', 'ts'))]: tasks.eslint,
'*': tasks.prettier,
}
export default config