generated from sindresorhus/node-module-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.lintstagedrc.js
83 lines (66 loc) · 2.24 KB
/
.lintstagedrc.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
import { execSync } from "node:child_process";
import fs from "node:fs";
import { basename, dirname, join, resolve } from "node:path";
import url from "node:url";
import { packageDirectory } from "pkg-dir";
export const JS_EXTENSIONS = "{js,jsx,mjs,cjs,ts,tsx,mts}";
const __dirname = dirname(url.fileURLToPath(import.meta.url));
const root = await packageDirectory({ cwd: resolve(__dirname) });
const script = join(root, "bin/eslint-in-dir.js");
export default {
[`!*.${JS_EXTENSIONS}`]: processNonJsFiles,
[`*.${JS_EXTENSIONS}`]: processJsFiles,
};
function processNonJsFiles(filenames) {
const filteredFiles = filenames
.filter(
(filename) =>
!fs.lstatSync(filename).isSymbolicLink() &&
!basename(filename).startsWith("Dockerfile"),
)
.join(" ");
return `pnpm prettier --ignore-unknown --write ${filteredFiles}`;
}
async function processJsFiles(filenames) {
const filteredFiles = filenames.filter(
(file) => !file.includes("/docs/templates/"),
);
const eslintCommands = await Promise.all(
filteredFiles.map(async (file) => {
const pkgDir = await packageDirectory({ cwd: dirname(file) });
return `${script} ${pkgDir} ${file}`;
}),
);
const concurrentlyCommands = breakIntoCommands(
"concurrently",
eslintCommands,
);
const prettierCommands = breakIntoCommands(
"pnpm prettier --write",
filteredFiles,
);
return [...concurrentlyCommands, ...prettierCommands];
}
function breakIntoCommands(baseCommand, args) {
const ARG_MAX = parseInt(execSync("getconf ARG_MAX").toString().trim());
const SAFETY_MARGIN = 2000;
const MAX_LENGTH = ARG_MAX - SAFETY_MARGIN;
const commands = [];
let currentArgs = [];
let currentLength = baseCommand.length;
args.forEach((arg) => {
const quotedArg = `"${arg.replace(/"/g, '\\"')}"`;
const argLength = quotedArg.length + 1; // +1 for the space
if (currentLength + argLength > MAX_LENGTH) {
commands.push(`${baseCommand} ${currentArgs.join(" ")}`);
currentArgs = [];
currentLength = baseCommand.length;
}
currentArgs.push(quotedArg);
currentLength += argLength;
});
if (currentArgs.length > 0) {
commands.push(`${baseCommand} ${currentArgs.join(" ")}`);
}
return commands;
}