forked from belgattitude/nextjs-monorepo-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-staged.common.js
51 lines (45 loc) · 1.51 KB
/
lint-staged.common.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
// @ts-check
const path = require('path');
const escape = require('shell-quote').quote;
const isWin = process.platform === 'win32';
/**
* Concatenate and escape a list of filenames that can be passed as args to prettier cli
*
* Prettier has an issue with special characters in filenames,
* such as the ones uses for nextjs dynamic routes (ie: [id].tsx...)
*
* @link https://github.com/okonet/lint-staged/issues/676
*
* @param {string[]} filenames
* @returns {string} Return concatenated and escaped filenames
*/
const concatFilesForPrettier = (filenames) =>
filenames
.map((filename) => `"${isWin ? filename : escape([filename])}"`)
.join(' ');
const concatFilesForStylelint = concatFilesForPrettier;
/**
* Lint-staged command for running eslint in packages or apps.
* @param {{cwd: string, files: string[], fix: boolean, cache: boolean, rules?: string[], maxWarnings?: number}} params
*/
const getEslintFixCmd = ({ cwd, files, rules, fix, cache, maxWarnings }) => {
const args = [
cache ? '--cache' : '',
fix ? '--fix' : '',
maxWarnings !== undefined ? `--max-warnings=${maxWarnings}` : '',
rules !== undefined
? '--rule ' + rules.map((r) => `"${r}"`).join('--rule ')
: '',
files
// makes output cleaner by removing absolute paths from filenames
.map((f) => path.relative(cwd, f))
.map((f) => `"./${f}"`)
.join(' '),
].join(' ');
return `eslint ${args}`;
};
module.exports = {
concatFilesForPrettier,
concatFilesForStylelint,
getEslintFixCmd,
};