UNPKG

@ts-dev-tools/core

Version:
49 lines (48 loc) 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HooksService = void 0; const GitService_1 = require("./GitService"); class HooksService { constructor() { } static consolidateManagedGitHooks(absoluteProjectDir, managedGitHooks, consolidatedManagedGitHooks) { for (const managedGitHook of managedGitHooks) { const resolvedHook = HooksService.resolveManagedGitHook(absoluteProjectDir, managedGitHook); const currentHook = consolidatedManagedGitHooks.get(resolvedHook.name); if (!currentHook) { consolidatedManagedGitHooks.set(resolvedHook.name, resolvedHook); continue; } const legacyCommands = Array.from(new Set([...currentHook.legacyCommands, currentHook.command])).filter((command) => command !== resolvedHook.command); consolidatedManagedGitHooks.set(resolvedHook.name, { ...resolvedHook, legacyCommands, }); } } static async applyManagedGitHooks(absoluteProjectDir, managedGitHooks) { const isGitRepository = await GitService_1.GitService.isGitRepository(absoluteProjectDir); if (!isGitRepository) { return; } for (const managedGitHook of managedGitHooks) { await GitService_1.GitService.addGitHook(absoluteProjectDir, managedGitHook.name, managedGitHook.command); for (const legacyCommand of managedGitHook.legacyCommands) { GitService_1.GitService.updateGitHook(absoluteProjectDir, managedGitHook.name, legacyCommand, managedGitHook.command); } } } static resolveManagedGitHook(absoluteProjectDir, managedGitHook) { return { name: managedGitHook.name, command: HooksService.resolveManagedGitHookCommand(absoluteProjectDir, managedGitHook.command), legacyCommands: [], }; } static resolveManagedGitHookCommand(absoluteProjectDir, managedGitHookCommand) { if (typeof managedGitHookCommand === "function") { return managedGitHookCommand(absoluteProjectDir); } return managedGitHookCommand; } } exports.HooksService = HooksService;