-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit-check.mjs
More file actions
64 lines (52 loc) · 1.67 KB
/
Copy pathpre-commit-check.mjs
File metadata and controls
64 lines (52 loc) · 1.67 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
import { execSync } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
/** @returns {string[]} */
function getStagedFiles() {
try {
return execSync('git diff --cached --name-only --diff-filter=ACMR', {
cwd: root,
encoding: 'utf8',
})
.trim()
.split('\n')
.filter(Boolean);
} catch {
return [];
}
}
/** @param {string} command @param {string} label */
function run(command, label) {
console.log(`\n▶ ${label}`);
execSync(command, { cwd: root, stdio: 'inherit' });
}
const staged = getStagedFiles();
if (staged.length === 0) {
process.exit(0);
}
const needsEmDash = staged.some((file) => /\.(php|[cm]?js|vue|md|mdc)$/.test(file));
const needsFrontendChecks = staged.some(
(file) =>
/\.(vue|[cm]?js|tsx?)$/.test(file) ||
file === 'eslint.config.js' ||
file === 'tsconfig.json' ||
file.startsWith('resources/'),
);
const needsPhpLint = staged.some((file) => file.endsWith('.php'));
try {
if (needsEmDash) {
run('npm run em-dash:check', 'Em dash check');
}
if (needsFrontendChecks) {
run('npm run format:check', 'Prettier');
run('npm run lint:check:ci', 'ESLint (CI mode, no Wayfinder output)');
}
if (needsPhpLint) {
run('vendor/bin/pint --dirty --test --format agent', 'Pint');
}
} catch {
console.error('\nPre-commit checks failed. Fix the issues above and try again.');
console.error('See .cursor/rules/pre-commit-quality.mdc for fixes.');
process.exit(1);
}