Skip to content

🎥 Make videos programmatically with React

`remotion-monorepo`: Re-stage formatted files and run lint in pre-commit hook

Jonny Burger4b7a0acf7908
diff --git a/pre-commit.ts b/pre-commit.ts
index 5f01fd5029..8b35b8f388 100644
--- a/pre-commit.ts
+++ b/pre-commit.ts
@@ -4,15 +4,17 @@ 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([...staged.trim().split('\n'), ...unstaged.trim().split('\n')]),
+ ...new Set([...stagedFiles, ...unstaged.trim().split('\n')]),
].filter(Boolean);
if (changedFiles.length === 0) {
process.exit(0);
}
-const packageNames = new Set<string>();
+const formatPackageNames = new Set<string>();
+const lintPackageNames = new Set<string>();
for (const file of changedFiles) {
const match = file.match(/^packages\/([^/]+)\//);
@@ -29,17 +31,32 @@ for (const file of changedFiles) {
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
- if (!pkgJson.scripts?.format) {
- continue;
+ if (pkgJson.scripts?.format) {
+ formatPackageNames.add(pkgJson.name);
}
- packageNames.add(pkgJson.name);
+ if (pkgJson.scripts?.lint) {
+ lintPackageNames.add(pkgJson.name);
+ }
}
-if (packageNames.size === 0) {
- process.exit(0);
-}
+if (formatPackageNames.size > 0) {
+ const formatFilters = [...formatPackageNames].flatMap((name) => [
+ '--filter',
+ name,
+ ]);
+ await $`bun run ${formatFilters} format`;
-const filters = [...packageNames].flatMap((name) => ['--filter', name]);
+ // Re-stage originally staged files so formatting changes are included in this commit
+ if (stagedFiles.length > 0) {
+ await $`git add ${stagedFiles}`;
+ }
+}
-await $`bun run ${filters} format`;
+if (lintPackageNames.size > 0) {
+ const lintFilters = [...lintPackageNames].flatMap((name) => [
+ '--filter',
+ name,
+ ]);
+ await $`bun run ${lintFilters} lint`;
+}