forked from FarmData2/FarmData2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.lintstagedrc.cjs
More file actions
338 lines (316 loc) · 10.1 KB
/
.lintstagedrc.cjs
File metadata and controls
338 lines (316 loc) · 10.1 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const { ESLint } = require('eslint');
const path = require('path');
const fd2EntrypointsTested = new Map();
const examplesEntrypointsTested = new Map();
const schoolEntrypointsTested = new Map();
const fd2LibsTested = new Map();
const examplesLibsTested = new Map();
const schoolLibsTested = new Map();
const compsTested = new Map();
const libsTested = new Map();
/*
* lint-staged provides the command for each pattern with an explicit
* list of files. If one of those files is ignored by .eslintignore
* then eslint outputs a warning that it was asked to lint an ignored
* file and the command fails. The code below applies the .eslintignore
* patterns to the list of files before passing them to eslint.
*
* This was adopted/adapted from:
* https://stackoverflow.com/a/73818629
*/
const removeIgnoredFiles = async (files) => {
const eslint = new ESLint();
const ignoredFiles = await Promise.all(
files.map((file) => eslint.isPathIgnored(file))
);
const filteredFiles = files.filter((_, i) => !ignoredFiles[i]);
return filteredFiles.join(' ');
};
/*
* Construct a test command for each entrypoint .vue file that is staged.
* The command will use a glob to run all e2e.cy.js tests in the
* endpoint directory containing the .vue file.
*/
const getModuleTestsVue = (files) => {
const testCommands = files.map((file) => {
if (file.includes('/farm_fd2/')) {
fd2EntrypointsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --fd2 --e2e --live --glob=' +
'/modules/farm_fd2/src/entrypoints/' +
path.basename(path.dirname(file)) +
'/*.e2e.cy.js'
);
} else if (file.includes('/farm_fd2_examples/')) {
examplesEntrypointsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --examples --e2e --live --glob=' +
'/modules/farm_fd2_examples/src/entrypoints/' +
path.basename(path.dirname(file)) +
'/*.e2e.cy.js'
);
} else if (file.includes('/farm_fd2_school/')) {
schoolEntrypointsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --school --e2e --live --glob=' +
'/modules/farm_fd2_school/src/entrypoints/' +
path.basename(path.dirname(file)) +
'/*.e2e.cy.js'
);
} else {
console.log('.vue file found in unrecognized module.');
console.log(
'All .vue files must be in fd2 or fd2_examples or fd2_school.'
);
}
});
return testCommands;
};
/*
* Construct a test command for each entrypoint e2e.cy.js file that is staged.
*/
const getModuleTestsE2ECyJs = (files) => {
const testCommands = files.map((file) => {
if (file.includes('/farm_fd2/')) {
if (fd2EntrypointsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --fd2 --e2e --live --glob=' +
file.substring(file.indexOf('/modules'))
);
}
} else if (file.includes('/farm_fd2_examples/')) {
if (examplesEntrypointsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --examples --e2e --live --glob=' +
file.substring(file.indexOf('/modules'))
);
}
} else if (file.includes('/farm_fd2_school/')) {
if (schoolEntrypointsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --school --e2e --live --glob=' +
file.substring(file.indexOf('/modules'))
);
}
} else {
console.log('.cy.js file found in unrecognized module.');
console.log(
'All .cy.js files must be in fd2 or fd2_examples or fd2_school.'
);
}
});
return testCommands;
};
/*
* Construct a test command for each entrypoint lib.js file
* that is staged.
*/
const getModuleTestsJs = (files) => {
const testCommands = files.map((file) => {
if (file.includes('/farm_fd2/')) {
fd2LibsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --fd2 --unit --glob=' +
'/modules/farm_fd2/src/entrypoints/' +
path.basename(path.dirname(file)) +
'/lib.*.unit.cy.js'
);
} else if (file.includes('/farm_fd2_examples/')) {
examplesLibsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --examples --unit --glob=' +
'/modules/farm_fd2_examples/src/entrypoints/' +
path.basename(path.dirname(file)) +
'/lib.*.unit.cy.js'
);
} else if (file.includes('/farm_fd2_school/')) {
schoolLibsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --school --unit --glob=' +
'/modules/farm_fd2_school/src/entrypoints/' +
path.basename(path.dirname(file)) +
'/lib.*.unit.cy.js'
);
} else {
console.log('lib.js file found in unrecognized module.');
console.log(
'All lib.js files must be in fd2 or fd2_examples or fd2_school.'
);
}
});
return testCommands;
};
/*
* Construct a test command for each entrypoint lib.*.unit.cy.js
* file that is staged.
*/
const getModuleTestsUnitCyJs = (files) => {
const testCommands = files.map((file) => {
if (file.includes('/farm_fd2/')) {
if (fd2LibsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --fd2 --unit --glob=' +
file.substring(file.indexOf('/modules'))
);
}
} else if (file.includes('/farm_fd2_examples/')) {
if (examplesLibsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --examples --unit --glob=' +
file.substring(file.indexOf('/modules'))
);
}
} else if (file.includes('/farm_fd2_school/')) {
if (schoolLibsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --school --unit --glob=' +
file.substring(file.indexOf('/modules'))
);
}
} else {
console.log('lib.*.unit.cy.js file found in unrecognized module.');
console.log(
'All lib.*.unit.cy.js files must be in fd2 or fd2_examples or fd2_school.'
);
}
});
return testCommands;
};
/*
* Construct a test command for each component .vue file that is staged.
* The command will use a glob to run all comp.cy.js component tests in the
* component directory containing the .vue file.
*/
const getCompTestsVue = (files) => {
const testCommands = files.map((file) => {
compsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --comp --glob=' +
'/components/' +
path.basename(path.dirname(file)) +
'/*.comp.cy.js'
);
});
return testCommands;
};
/*
* Construct a test command for each component comp.cy.js file that is staged.
*/
const getCompTestsCompCyJs = (files) => {
const testCommands = files.map((file) => {
if (compsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --comp --glob=' + file.substring(file.indexOf('/components'))
);
}
});
return testCommands;
};
const getCompTestsE2ECyJs = (files) => {
const testCommands = files.map((file) => {
if (compsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --e2e --fd2 --live --glob=' +
file.substring(file.indexOf('/components'))
);
}
});
return testCommands;
};
/*
* Construct a test command for each library .js file that is staged.
* The command will use a glob to run all unit.cy.js unit tests in the
* library directory containing the .js file.
*/
const getLibTestsJs = (files) => {
const testCommands = files.map((file) => {
libsTested.set(path.basename(path.dirname(file)), true);
return (
'test.bash --unit --lib --glob=' +
'/library/' +
path.basename(path.dirname(file)) +
'/*.unit.cy.js'
);
});
// remove duplicate commands
const uniqueCommands = Array.from(new Set(testCommands));
return uniqueCommands;
};
/*
* Construct a test command for each library unit.cy.js file that is staged.
*/
const getLibTestsUnitCyJs = (files) => {
const testCommands = files.map((file) => {
if (libsTested.get(path.basename(path.dirname(file)))) {
return 'skipping ' + file;
} else {
return (
'test.bash --unit --lib --glob=' +
file.substring(file.indexOf('/library'))
);
}
});
return testCommands;
};
module.exports = {
'*': [
'cspell --no-progress --no-summary --no-must-find-files --config .cspell.json',
'prettier --ignore-unknown --write',
],
'**/*.bash|.githooks/*': ['shellcheck'],
'**/*.md': async (files) => {
const filesToLint = await removeIgnoredFiles(files);
return [
`markdown-link-check --config .markdown-link-check.json --quiet ${filesToLint}`,
`eslint --max-warnings=0 --ext md ${filesToLint}`,
`vale ${filesToLint}`,
];
},
'*.vue|*.js|*.jsx|*.cjs|*.mjs|*.json': async (files) => {
const filesToLint = await removeIgnoredFiles(files);
return [`eslint --max-warnings=0 ${filesToLint}`];
},
'modules/**/entrypoints/**/*.vue': (files) => {
return getModuleTestsVue(files);
},
'modules/**/entrypoints/**/*.e2e.cy.js': (files) => {
return getModuleTestsE2ECyJs(files);
},
'modules/**/entrypoints/**/lib.js': (files) => {
return getModuleTestsJs(files);
},
'modules/**/entrypoints/**/lib.*.unit.cy.js': (files) => {
return getModuleTestsUnitCyJs(files);
},
'components/**/*.vue': (files) => {
return getCompTestsVue(files);
},
'components/**/*.comp.cy.js': (files) => {
return getCompTestsCompCyJs(files);
},
'components/**/*.e2e.cy.js': (files) => {
return getCompTestsE2ECyJs(files);
},
'library/!(cypress)/!(*unit.cy).js': (files) => {
return getLibTestsJs(files);
},
'library/**/*.unit.cy.js': (files) => {
return getLibTestsUnitCyJs(files);
},
};