UNPKG

git-release-manager

Version:

A tool to generate release notes from git commit history

195 lines 7.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProjectVersionManager = void 0; const fs_1 = require("fs"); const path_1 = require("path"); const simple_git_1 = __importDefault(require("simple-git")); const GitVersionManager_1 = require("./GitVersionManager"); class PackageJsonVersion { constructor(path = 'package.json') { this.filePath = path; } get currentVersion() { const content = JSON.parse((0, fs_1.readFileSync)(this.filePath, 'utf8')); return content.version; } update(newVersion) { const content = JSON.parse((0, fs_1.readFileSync)(this.filePath, 'utf8')); content.version = newVersion; (0, fs_1.writeFileSync)(this.filePath, JSON.stringify(content, null, 2)); } } class CsprojVersion { constructor(path) { this.filePath = path; } get currentVersion() { const content = (0, fs_1.readFileSync)(this.filePath, 'utf8'); const match = content.match(/<Version>(.*?)<\/Version>/); return match ? match[1] : '0.0.0'; } update(newVersion) { let content = (0, fs_1.readFileSync)(this.filePath, 'utf8'); content = content.replace(/<Version>.*?<\/Version>/, `<Version>${newVersion}</Version>`); (0, fs_1.writeFileSync)(this.filePath, content); } } class PyProjectVersion { constructor(path = 'pyproject.toml') { this.filePath = path; } get currentVersion() { const content = (0, fs_1.readFileSync)(this.filePath, 'utf8'); const match = content.match(/version\s*=\s*["'](.+?)["']/); return match ? match[1] : '0.0.0'; } update(newVersion) { let content = (0, fs_1.readFileSync)(this.filePath, 'utf8'); content = content.replace(/version\s*=\s*["'].+?["']/, `version = "${newVersion}"`); (0, fs_1.writeFileSync)(this.filePath, content); } } class GradleVersion { constructor(path = 'build.gradle') { this.filePath = path; } get currentVersion() { const content = (0, fs_1.readFileSync)(this.filePath, 'utf8'); const match = content.match(/version\s*=\s*['"](.+?)['"]/); return match ? match[1] : '0.0.0'; } update(newVersion) { let content = (0, fs_1.readFileSync)(this.filePath, 'utf8'); content = content.replace(/version\s*=\s*['"].+?['"]/, `version = '${newVersion}'`); (0, fs_1.writeFileSync)(this.filePath, content); } } class GoModVersion { constructor(path = 'go.mod') { this.latestVersion = '0.0.0'; this.filePath = path; this.git = (0, simple_git_1.default)(process.cwd()); // Fetch tags when the class is instantiated this.fetchLatestVersion(); } get currentVersion() { return this.latestVersion; } // Fetch the latest version from git tags fetchLatestVersion() { this.git.tags() .then(tags => { if (tags.all.length > 0) { const sortedTags = tags.all.sort(this.compareVersions); this.latestVersion = sortedTags[sortedTags.length - 1]; } }) .catch(error => { console.error('Error fetching tags:', error); }); } update(newVersion) { this.git.addTag(newVersion, (err, result) => { if (err) { console.error('Error adding tag:', err); } else { console.log(`Tag added: ${newVersion}`); // Update internal state on success this.latestVersion = newVersion; } }); } compareVersions(a, b) { return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' }); } } class ProjectVersionManager { constructor() { this.PROJECT_FILES = [ { pattern: 'package.json', handler: PackageJsonVersion }, { pattern: '*.csproj', handler: CsprojVersion }, { pattern: 'pyproject.toml', handler: PyProjectVersion }, { pattern: 'build.gradle', handler: GradleVersion }, { pattern: 'go.mod', handler: GoModVersion }, ]; } detectProjectFile(path) { var _a; // Eğer path belirtilmişse, doğrudan o dosyayı kontrol et if (path) { const absolutePath = (0, path_1.resolve)(path); if ((0, fs_1.existsSync)(absolutePath)) { // Dosya uzantısına göre handler'ı belirle const handler = (_a = this.PROJECT_FILES.find(f => absolutePath.endsWith(f.pattern.replace('*', '')))) === null || _a === void 0 ? void 0 : _a.handler; if (!handler) { throw new Error(`Unsupported project file type: ${path}`); } return { filePath: absolutePath, handler }; } throw new Error(`Project file not found at: ${path}`); } // Path belirtilmemişse, mevcut dizinde desteklenen ilk proje dosyasını bul for (const { pattern, handler } of this.PROJECT_FILES) { if (pattern.includes('*')) { // Wildcard içeren dosya isimleri için glob kullanılabilir const glob = require('glob'); const files = glob.sync(pattern, { cwd: process.cwd() }); if (files.length > 0) { return { filePath: (0, path_1.join)(process.cwd(), files[0]), handler, }; } } else { const filePath = (0, path_1.join)(process.cwd(), pattern); if ((0, fs_1.existsSync)(filePath)) { return { filePath, handler }; } } } throw new Error('No supported project file found in current directory'); } detectProjectVersion(path) { try { const { filePath, handler } = this.detectProjectFile(path); return new handler(filePath); } catch (error) { if (error instanceof Error) { throw new Error(`Failed to detect project version: ${error.message}`); } throw error; } } async updateProjectVersion(newVersion, path) { try { if (!newVersion) { const gitVersionManager = new GitVersionManager_1.GitVersionManager(); newVersion = await gitVersionManager.getLatestTag(); } const projectVersion = this.detectProjectVersion(path); const currentVersion = projectVersion.currentVersion; // Versiyon güncellemesi projectVersion.update(newVersion); console.log(`Version updated in ${projectVersion.filePath}:`); console.log(` ${currentVersion} -> ${newVersion}`); } catch (error) { if (error instanceof Error) { throw new Error(`Failed to update project version: ${error.message}`); } throw error; } } getCurrentVersion(path) { const projectVersion = this.detectProjectVersion(path); return projectVersion.currentVersion; } } exports.ProjectVersionManager = ProjectVersionManager; //# sourceMappingURL=ProjectVersion.js.map