Skip to content

Commit 5da785a

Browse files
committed
added pre hook to check and fix lint error before commit
1 parent 5e00af7 commit 5da785a

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

.husky/pre-commit.mjs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,37 @@ const run = (cmd) => new Promise((resolve, reject) => exec(
1212
const changeset = await run('git diff --cached --name-only --diff-filter=ACMR');
1313
const modifiedFiles = changeset.split('\n').filter(Boolean);
1414

15-
// Check if there are any model files staged
15+
// Auto-fix and lint staged JS files
16+
const jsFiles = modifiedFiles.filter((f) => f.endsWith('.js'));
17+
if (jsFiles.length > 0) {
18+
const fileList = jsFiles.join(' ');
19+
try {
20+
await run(`npx eslint --fix ${fileList}`);
21+
} catch {
22+
// fix ran; lint check below will surface unfixable errors
23+
}
24+
await run(`git add ${fileList}`);
25+
// Fail the commit if unfixable lint errors remain
26+
const output = await run(`npx eslint ${fileList}`);
27+
if (output) console.log(output);
28+
}
29+
30+
// Auto-fix and lint staged CSS files
31+
const cssFiles = modifiedFiles.filter((f) => f.endsWith('.css'));
32+
if (cssFiles.length > 0) {
33+
const fileList = cssFiles.join(' ');
34+
try {
35+
await run(`npx stylelint --fix ${fileList}`);
36+
} catch {
37+
// fix ran; lint check below will surface unfixable errors
38+
}
39+
await run(`git add ${fileList}`);
40+
// Fail the commit if unfixable lint errors remain
41+
const output = await run(`npx stylelint ${fileList}`);
42+
if (output) console.log(output);
43+
}
44+
45+
// Rebuild UE JSON bundles when model files are staged
1646
const modifiedPartials = modifiedFiles.filter((file) => file.match(/^ue\/models\/.*\.json/));
1747
if (modifiedPartials.length > 0) {
1848
const output = await run('npm run build:json --silent');

0 commit comments

Comments
 (0)