UNPKG

@eventcatalog/notifier

Version:

CLI tool to detect EventCatalog changes and send notifications

81 lines 3.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitError = void 0; exports.getGitRoot = getGitRoot; exports.getChangedFiles = getChangedFiles; exports.getFileAtCommit = getFileAtCommit; const child_process_1 = require("child_process"); const path_1 = __importDefault(require("path")); function getGitRoot(catalogPath) { return (0, child_process_1.execSync)('git rev-parse --show-toplevel', { cwd: catalogPath, encoding: 'utf-8', }).trim(); } // Returns absolute paths to the changed files function getChangedFiles(catalogPath, commitRange) { try { const gitRoot = getGitRoot(catalogPath); const diff = (0, child_process_1.execSync)(`git diff --name-only ${commitRange}`, { encoding: 'utf-8', cwd: gitRoot, }).trim(); if (!diff) { return []; } // Resolve the paths to be relative to the catalog path return diff.split('\n').map((file) => path_1.default.resolve(file)); } catch (error) { // Handle common Git errors with user-friendly messages if (error.message.includes('unknown revision or path not in the working tree')) { throw new GitError('Git commit range not found', `The commit range "${commitRange}" doesn't exist in this repository.`, [ 'This usually happens when:', "• You're in a new repository with no previous commits", '• The specified commit range is invalid', "• The repository doesn't have enough commit history", '', 'Solutions:', '• For new repositories: Make at least 2 commits first', '• Use a different commit range like: --commit-range HEAD~1..HEAD', '• Check your git history with: git log --oneline', ]); } if (error.message.includes('Not a git repository')) { throw new GitError('Not a Git repository', 'The specified directory is not a Git repository.', [ "Make sure you're running this command in a Git repository.", 'Initialize Git with: git init', ]); } // Re-throw other Git errors throw error; } } class GitError extends Error { constructor(title, message, suggestions = []) { super(message); this.name = 'GitError'; this.title = title; this.suggestions = suggestions; } } exports.GitError = GitError; function getFileAtCommit(catalogPath, filePath, commit) { try { // Find the git root const gitRoot = getGitRoot(catalogPath); // Get path relative to git root (not catalog path) const relativeToGitRoot = path_1.default.relative(gitRoot, filePath); return (0, child_process_1.execSync)(`git show ${commit}:${relativeToGitRoot}`, { encoding: 'utf-8', cwd: gitRoot, }); } catch (error) { return ''; } } //# sourceMappingURL=git.js.map