git-release-manager
Version:
A tool to generate release notes from git commit history
159 lines • 6.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitVersionManager = void 0;
const simple_git_1 = __importDefault(require("simple-git"));
const semver_1 = __importDefault(require("semver"));
const versionFormatter_1 = require("./versionFormatter");
const git = (0, simple_git_1.default)();
class GitVersionManager {
async getLatestTag(prefix, channel) {
let defaultTag = '0.0.0';
if (prefix) {
defaultTag = `${prefix}${defaultTag}`;
}
try {
const tags = await git.tags();
let filteredTags = tags.all;
if (prefix) {
filteredTags = filteredTags.filter(tag => tag.startsWith(prefix));
}
if (channel) {
const channelTags = filteredTags.filter(tag => tag.includes(`-${channel}`));
filteredTags = channelTags.length > 0 ? channelTags : filteredTags;
}
const latestTag = filteredTags
.map(tag => (prefix ? tag.replace(prefix, '') : tag))
.filter(tag => semver_1.default.valid(tag))
.sort(semver_1.default.rcompare)[0] || defaultTag;
return prefix && !latestTag.startsWith(prefix) ? `${prefix}${latestTag}` : latestTag;
}
catch (error) {
console.error('Could not retrieve tags:', error);
return defaultTag;
}
}
async listVersions(count = 10) {
const tags = await git.tags();
const versions = tags.all
.filter(tag => semver_1.default.valid(tag))
.sort((a, b) => semver_1.default.rcompare(a, b))
.slice(0, count);
console.log('\nVersion History:');
console.log('================');
for (const version of versions) {
const tagDetails = await git.show(['--quiet', version]);
console.log(`\nVersion: ${version}`);
console.log(`Date: ${tagDetails.split('\n')[2]}`);
console.log('-----------------');
}
}
async showLatestVersion() {
const version = await this.getLatestTag();
console.log(`Latest version: ${version}`);
}
async compareVersions(compareVersion) {
// Fetch the latest version tag from the repository
const latestVersion = await this.getLatestTag();
// Use semver to compare the two versions
const comparison = semver_1.default.compare(latestVersion, compareVersion);
// Determine the relationship between the two versions
let comparisonText = '';
if (comparison > 0) {
// latestVersion is greater than compareVersion
comparisonText = `${compareVersion} is behind ${latestVersion}`;
}
else if (comparison < 0) {
// compareVersion is greater than latestVersion
comparisonText = `${compareVersion} is ahead of ${latestVersion}`;
}
else {
// Both versions are equal
comparisonText = `${compareVersion} is the same as ${latestVersion}`;
}
// Print the comparison result
console.log(`Comparing ${compareVersion} with ${latestVersion}`);
console.log(comparisonText);
// Print the changes between the compared versions
console.log('\nChanges:');
const diff = await git.diff([`${compareVersion}...${latestVersion}`]);
console.log(diff);
}
async revertToVersion(version, push) {
if (!semver_1.default.valid(version)) {
throw new Error(`Invalid version format: ${version}`);
}
await git.reset(['--hard', version]);
console.log(`Reverted to version ${version}`);
if (push) {
await git.push('origin', 'HEAD', ['--force']);
console.log('Force pushed revert to remote');
}
}
async syncVersions(push) {
await git.fetch(['--tags', '--force']);
console.log('Synced tags with remote');
if (push) {
const localTags = await git.tags();
await git.pushTags('origin');
console.log('Pushed local tags to remote');
}
}
async createGitTag(version) {
try {
await git.addTag(version);
// await git.pushTags()
}
catch (error) {
console.error('Could not create git tag:', error);
}
}
async initVersion(options) {
var _a;
const prefix = (_a = options.prefix) !== null && _a !== void 0 ? _a : '';
const initialVersion = `${prefix}${typeof options.init === 'string' ? options.init : '0.0.0'}`;
const tags = await git.tags();
if (tags.all.length > 0) {
throw new Error('Repository already has tags. Use --reset if needed.');
}
return initialVersion;
}
async generateNewVersion(options) {
let newVersion;
const latestTag = await this.getLatestTag(options.prefix, options.channel);
const type = options.major ? 'major' : options.minor ? 'minor' : options.patch ? 'patch' : undefined;
newVersion = await (0, versionFormatter_1.incrementVersion)(latestTag, type, {
channel: options.channel,
channelNumber: options.channelNumber,
prefix: options.prefix,
prerelease: options.prerelease,
build: options.build,
});
return newVersion;
}
async resetVersion(channel, prefix) {
try {
const { all: tags } = await git.tags();
if (tags.length === 0) {
console.log('No local tags found.');
return;
}
await git.raw(['tag', '-d', ...tags]);
console.log('All local tags deleted:', tags);
}
catch (error) {
console.error('Error deleting tags:', error);
}
}
async pushChanges(version, hasBranch) {
if (hasBranch) {
const branchName = `release/v${version}`;
await git.push('origin', branchName);
}
await git.pushTags();
}
}
exports.GitVersionManager = GitVersionManager;
//# sourceMappingURL=GitVersionManager.js.map