|
| 1 | +import { GitService } from "./GitService"; |
| 2 | + |
| 3 | +export type ManagedGitHookCommand = |
| 4 | + | string |
| 5 | + | ((absoluteProjectDir: string) => string); |
| 6 | + |
| 7 | +export type ManagedGitHook = { |
| 8 | + name: string; |
| 9 | + command: ManagedGitHookCommand; |
| 10 | +}; |
| 11 | + |
| 12 | +export type AppliedManagedGitHook = { |
| 13 | + name: string; |
| 14 | + command: string; |
| 15 | + legacyCommands: string[]; |
| 16 | +}; |
| 17 | + |
| 18 | +export class HooksService { |
| 19 | + private constructor() {} |
| 20 | + |
| 21 | + static consolidateManagedGitHooks( |
| 22 | + absoluteProjectDir: string, |
| 23 | + managedGitHooks: ManagedGitHook[], |
| 24 | + consolidatedManagedGitHooks: Map<string, AppliedManagedGitHook>, |
| 25 | + ): void { |
| 26 | + for (const managedGitHook of managedGitHooks) { |
| 27 | + const resolvedHook = HooksService.resolveManagedGitHook( |
| 28 | + absoluteProjectDir, |
| 29 | + managedGitHook, |
| 30 | + ); |
| 31 | + const currentHook = consolidatedManagedGitHooks.get(resolvedHook.name); |
| 32 | + |
| 33 | + if (!currentHook) { |
| 34 | + consolidatedManagedGitHooks.set(resolvedHook.name, resolvedHook); |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + const legacyCommands = Array.from( |
| 39 | + new Set([...currentHook.legacyCommands, currentHook.command]), |
| 40 | + ).filter((command) => command !== resolvedHook.command); |
| 41 | + |
| 42 | + consolidatedManagedGitHooks.set(resolvedHook.name, { |
| 43 | + ...resolvedHook, |
| 44 | + legacyCommands, |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static async applyManagedGitHooks( |
| 50 | + absoluteProjectDir: string, |
| 51 | + managedGitHooks: AppliedManagedGitHook[], |
| 52 | + ): Promise<void> { |
| 53 | + const isGitRepository = |
| 54 | + await GitService.isGitRepository(absoluteProjectDir); |
| 55 | + |
| 56 | + if (!isGitRepository) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + for (const managedGitHook of managedGitHooks) { |
| 61 | + await GitService.addGitHook( |
| 62 | + absoluteProjectDir, |
| 63 | + managedGitHook.name, |
| 64 | + managedGitHook.command, |
| 65 | + ); |
| 66 | + |
| 67 | + for (const legacyCommand of managedGitHook.legacyCommands) { |
| 68 | + GitService.updateGitHook( |
| 69 | + absoluteProjectDir, |
| 70 | + managedGitHook.name, |
| 71 | + legacyCommand, |
| 72 | + managedGitHook.command, |
| 73 | + ); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static resolveManagedGitHook( |
| 79 | + absoluteProjectDir: string, |
| 80 | + managedGitHook: ManagedGitHook, |
| 81 | + ): AppliedManagedGitHook { |
| 82 | + return { |
| 83 | + name: managedGitHook.name, |
| 84 | + command: HooksService.resolveManagedGitHookCommand( |
| 85 | + absoluteProjectDir, |
| 86 | + managedGitHook.command, |
| 87 | + ), |
| 88 | + legacyCommands: [], |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + private static resolveManagedGitHookCommand( |
| 93 | + absoluteProjectDir: string, |
| 94 | + managedGitHookCommand: ManagedGitHookCommand, |
| 95 | + ): string { |
| 96 | + if (typeof managedGitHookCommand === "function") { |
| 97 | + return managedGitHookCommand(absoluteProjectDir); |
| 98 | + } |
| 99 | + |
| 100 | + return managedGitHookCommand; |
| 101 | + } |
| 102 | +} |
0 commit comments