forked from remotion-dev/remotion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpre-commit.ts
More file actions
54 lines (43 loc) · 1.31 KB
/
pre-commit.ts
File metadata and controls
54 lines (43 loc) · 1.31 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
import {readFileSync, existsSync} from 'fs';
import path from 'path';
import {$} from 'bun';
const staged = await $`git diff --cached --name-only --diff-filter=ACMR`.text();
const unstaged = await $`git diff --name-only`.text();
const stagedFiles = staged.trim().split('\n').filter(Boolean);
const changedFiles = [
...new Set([...stagedFiles, ...unstaged.trim().split('\n')]),
].filter(Boolean);
if (changedFiles.length === 0) {
process.exit(0);
}
const formatPackageNames = new Set<string>();
const lintPackageNames = new Set<string>();
for (const file of changedFiles) {
const match = file.match(/^packages\/([^/]+)\//);
if (!match) {
continue;
}
const dir = match[1];
const pkgJsonPath = path.join('packages', dir, 'package.json');
if (!existsSync(pkgJsonPath)) {
continue;
}
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
if (pkgJson.scripts?.format) {
formatPackageNames.add(pkgJson.name);
}
if (pkgJson.scripts?.lint) {
lintPackageNames.add(pkgJson.name);
}
}
if (formatPackageNames.size > 0) {
const formatFilters = [...formatPackageNames].flatMap((name) => [
'--filter',
name,
]);
await $`bun run ${formatFilters} format`;
// Re-stage originally staged files so formatting changes are included in this commit
if (stagedFiles.length > 0) {
await $`git add ${stagedFiles}`;
}
}