Skip to content

Commit c56ded0

Browse files
committed
refactor(services): restructure service initialization
- Created initializeServices function - Moved service instantiation - Improved error handling - Updated changelog service - Used git service for changelog
1 parent 0b4b435 commit c56ded0

7 files changed

Lines changed: 236 additions & 200 deletions

File tree

dist/index.js

Lines changed: 110 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* universal-version-bump v0.10.0
2+
* universal-version-bump v0.10.1
33
* Universal Version Bump
44
*
55
* Description: A GitHub Action to automatically bump versions across any app (Node, Python, PHP, Docker, etc.)
66
* Author: Taj <[email protected]>
77
* Homepage: https://github.com/taj54/universal-version-bump#readme
88
* License: MIT
9-
* Generated on Wed, 27 Aug 2025 09:57:56 GMT
9+
* Generated on Wed, 27 Aug 2025 10:24:47 GMT
1010
*/
1111
require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap
1212
/******/ var __webpack_modules__ = ({
@@ -32778,24 +32778,34 @@ const registry_1 = __nccwpck_require__(8378);
3277832778
const errors_1 = __nccwpck_require__(4830);
3277932779
const config_1 = __nccwpck_require__(5496);
3278032780
const core = __importStar(__nccwpck_require__(9999));
32781+
async function initializeServices() {
32782+
const updaterRegistry = new registry_1.UpdaterRegistry();
32783+
await updaterRegistry.loadUpdaters();
32784+
const updaterService = new services_1.UpdaterService(updaterRegistry);
32785+
const gitService = new services_1.GitService('main');
32786+
const fileHandler = new utils_1.FileHandler();
32787+
const changelogService = new services_1.ChangelogService(fileHandler, gitService, 'CHANGELOG.md');
32788+
return {
32789+
updaterRegistry,
32790+
updaterService,
32791+
gitService,
32792+
fileHandler,
32793+
changelogService,
32794+
};
32795+
}
3278132796
async function run() {
3278232797
try {
3278332798
process.chdir(config_1.TARGET_PATH);
3278432799
const releaseType = config_1.RELEASE_TYPE;
3278532800
const targetPlatform = config_1.TARGET_PLATFORM;
32786-
const updaterRegistry = new registry_1.UpdaterRegistry();
32787-
await updaterRegistry.loadUpdaters();
32788-
const updaterService = new services_1.UpdaterService(updaterRegistry);
32789-
const gitService = new services_1.GitService();
32790-
const fileHandler = new utils_1.FileHandler();
32791-
const changelogService = new services_1.ChangelogService(fileHandler);
32801+
const { updaterService, gitService, changelogService } = await initializeServices();
3279232802
const platform = updaterService.getPlatform(targetPlatform);
3279332803
core.info(`Detected platform: ${platform}`);
3279432804
const version = updaterService.updateVersion(platform, releaseType);
3279532805
core.setOutput('new_version', version);
3279632806
// Generate and update changelog
32797-
const latestTag = await changelogService.getLatestTag();
32798-
const commits = await changelogService.getCommitsSinceTag(latestTag);
32807+
const latestTag = await gitService.getLatestTag();
32808+
const commits = await gitService.getCommitsSinceTag(latestTag);
3279932809
const changelogContent = changelogService.generateChangelog(commits, version);
3280032810
await changelogService.updateChangelog(changelogContent);
3280132811
// Git Commit & Tag
@@ -32812,24 +32822,27 @@ async function run() {
3281232822
}
3281332823
}
3281432824
catch (error) {
32815-
if (error instanceof errors_1.PlatformDetectionError) {
32816-
core.setFailed(`Platform detection failed: ${error.message}`);
32817-
}
32818-
else if (error instanceof errors_1.VersionBumpError) {
32819-
core.setFailed(`Version bump failed: ${error.message}`);
32820-
}
32821-
else if (error instanceof errors_1.FileNotFoundError) {
32822-
core.setFailed(`File not found: ${error.message}`);
32823-
}
32824-
else if (error instanceof errors_1.InvalidManifestError) {
32825-
core.setFailed(`Invalid manifest: ${error.message}`);
32826-
}
32827-
else if (error instanceof Error) {
32828-
core.setFailed(error.message);
32829-
}
32830-
else {
32831-
core.setFailed(String(error));
32832-
}
32825+
handleError(error);
32826+
}
32827+
}
32828+
function handleError(error) {
32829+
if (error instanceof errors_1.PlatformDetectionError) {
32830+
core.setFailed(`Platform detection failed: ${error.message}`);
32831+
}
32832+
else if (error instanceof errors_1.VersionBumpError) {
32833+
core.setFailed(`Version bump failed: ${error.message}`);
32834+
}
32835+
else if (error instanceof errors_1.FileNotFoundError) {
32836+
core.setFailed(`File not found: ${error.message}`);
32837+
}
32838+
else if (error instanceof errors_1.InvalidManifestError) {
32839+
core.setFailed(`Invalid manifest: ${error.message}`);
32840+
}
32841+
else if (error instanceof Error) {
32842+
core.setFailed(error.message);
32843+
}
32844+
else {
32845+
core.setFailed(String(error));
3283332846
}
3283432847
}
3283532848
run();
@@ -32956,46 +32969,15 @@ var __importStar = (this && this.__importStar) || (function () {
3295632969
})();
3295732970
Object.defineProperty(exports, "__esModule", ({ value: true }));
3295832971
exports.ChangelogService = void 0;
32959-
const exec = __importStar(__nccwpck_require__(8872));
32972+
const core = __importStar(__nccwpck_require__(9999));
3296032973
/**
3296132974
* Service for managing the changelog file.
3296232975
*/
3296332976
class ChangelogService {
32964-
constructor(fileHandler) {
32977+
constructor(fileHandler, gitService, changelogPath = 'CHANGELOG.md') {
3296532978
this.fileHandler = fileHandler;
32966-
}
32967-
/**
32968-
* Get the latest git tag.
32969-
* @returns The latest tag as a string.
32970-
*/
32971-
async getLatestTag() {
32972-
let latestTag = '';
32973-
const options = {
32974-
listeners: {
32975-
stdout: (data) => {
32976-
latestTag += data.toString();
32977-
},
32978-
},
32979-
};
32980-
await exec.exec('git', ['describe', '--tags', '--abbrev=0'], options);
32981-
return latestTag.trim();
32982-
}
32983-
/**
32984-
* Get the commits since a specific tag.
32985-
* @param tag The tag to get commits since.
32986-
* @returns An array of commit messages.
32987-
*/
32988-
async getCommitsSinceTag(tag) {
32989-
let commits = '';
32990-
const options = {
32991-
listeners: {
32992-
stdout: (data) => {
32993-
commits += data.toString();
32994-
},
32995-
},
32996-
};
32997-
await exec.exec('git', ['log', `${tag}..HEAD`, '--oneline'], options);
32998-
return commits.split('\n').filter(Boolean);
32979+
this.gitService = gitService;
32980+
this.changelogPath = changelogPath;
3299932981
}
3300032982
/**
3300132983
* Generate a changelog from the given commits.
@@ -33034,16 +33016,26 @@ class ChangelogService {
3303433016
* @param changelogContent The new changelog content to add.
3303533017
*/
3303633018
async updateChangelog(changelogContent) {
33037-
const changelogPath = 'CHANGELOG.md';
33019+
const changelogPath = this._getChangelogPath();
3303833020
const existingChangelog = await this.fileHandler.readFile(changelogPath);
3303933021
const versionMatch = changelogContent.match(/## v(\d+\.\d+\.\d+)/);
3304033022
if (versionMatch) {
3304133023
const newVersion = versionMatch[0];
33042-
if (existingChangelog.includes(newVersion)) {
33043-
console.log(`Changelog for version ${newVersion} already exists. Skipping.`);
33024+
if (this._versionExistsInChangelog(existingChangelog, newVersion)) {
33025+
core.info(`Changelog for version ${newVersion} already exists. Skipping.`);
3304433026
return;
3304533027
}
3304633028
}
33029+
const updatedChangelog = this._insertChangelogContent(existingChangelog, changelogContent);
33030+
await this.fileHandler.writeFile(changelogPath, updatedChangelog);
33031+
}
33032+
_getChangelogPath() {
33033+
return this.changelogPath;
33034+
}
33035+
_versionExistsInChangelog(existingChangelog, newVersion) {
33036+
return existingChangelog.includes(newVersion);
33037+
}
33038+
_insertChangelogContent(existingChangelog, changelogContent) {
3304733039
const separator = '\n---\n\n';
3304833040
const headerIndex = existingChangelog.indexOf(separator);
3304933041
if (headerIndex !== -1) {
@@ -33052,19 +33044,16 @@ class ChangelogService {
3305233044
const firstVersionIndex = contentAfterHeader.search(/## v\d+\.\d+\.\d+/);
3305333045
if (firstVersionIndex !== -1) {
3305433046
const oldChangelog = contentAfterHeader.substring(firstVersionIndex);
33055-
const newChangelog = header + changelogContent + oldChangelog;
33056-
await this.fileHandler.writeFile(changelogPath, newChangelog);
33047+
return header + changelogContent + oldChangelog;
3305733048
}
3305833049
else {
3305933050
// No version found after header, so just append the new changelog
33060-
const newChangelog = header + changelogContent;
33061-
await this.fileHandler.writeFile(changelogPath, newChangelog);
33051+
return header + changelogContent;
3306233052
}
3306333053
}
3306433054
else {
3306533055
// No separator found, so prepend the new changelog
33066-
const newChangelog = changelogContent + existingChangelog;
33067-
await this.fileHandler.writeFile(changelogPath, newChangelog);
33056+
return changelogContent + existingChangelog;
3306833057
}
3306933058
}
3307033059
}
@@ -33120,12 +33109,18 @@ const github = __importStar(__nccwpck_require__(5380));
3312033109
* Service for interacting with Git.
3312133110
*/
3312233111
class GitService {
33112+
constructor(baseBranch = 'main') {
33113+
this.baseBranch = baseBranch;
33114+
}
33115+
async _execGitCommand(args, options) {
33116+
return await exec.exec('git', args, options);
33117+
}
3312333118
/**
3312433119
* Configures the Git user.
3312533120
*/
3312633121
async configureGitUser() {
33127-
await exec.exec('git', ['config', 'user.name', 'github-actions[bot]']);
33128-
await exec.exec('git', [
33122+
await this._execGitCommand(['config', 'user.name', 'github-actions[bot]']);
33123+
await this._execGitCommand([
3312933124
'config',
3313033125
'user.email',
3313133126
'github-actions[bot]@users.noreply.github.com',
@@ -33137,14 +33132,14 @@ class GitService {
3313733132
* @returns True if changes were committed, false otherwise.
3313833133
*/
3313933134
async commitChanges(message) {
33140-
await exec.exec('git', ['add', '-A']);
33135+
await this._execGitCommand(['add', '-A']);
3314133136
try {
33142-
await exec.exec('git', ['diff-index', '--quiet', 'HEAD']);
33137+
await this._execGitCommand(['diff-index', '--quiet', 'HEAD']);
3314333138
core.info('⚠️ No changes to commit');
3314433139
return false;
3314533140
}
3314633141
catch {
33147-
await exec.exec('git', ['commit', '-m', message]);
33142+
await this._execGitCommand(['commit', '-m', message]);
3314833143
return true;
3314933144
}
3315033145
}
@@ -33155,22 +33150,55 @@ class GitService {
3315533150
*/
3315633151
async createReleaseBranch(version) {
3315733152
const branch = `version/v${version}`;
33158-
await exec.exec('git', ['checkout', '-b', branch]);
33153+
await this._execGitCommand(['checkout', '-b', branch]);
3315933154
const committed = await this.commitChanges(`chore(release): bump version to v${version}`);
3316033155
if (!committed) {
3316133156
core.info('⚠️ Skipping branch push, no changes detected');
3316233157
return null;
3316333158
}
33164-
await exec.exec('git', ['push', 'origin', branch, '--force']);
33159+
await this._execGitCommand(['push', 'origin', branch, '--force']);
3316533160
return branch;
3316633161
}
3316733162
/**
3316833163
* Creates a Git tag for the specified version.
3316933164
* @param version The version to create the tag for.
3317033165
*/
3317133166
async createAndPushTag(version) {
33172-
await exec.exec('git', ['tag', `v${version}`]);
33173-
await exec.exec('git', ['push', 'origin', 'HEAD', '--tags']);
33167+
await this._execGitCommand(['tag', `v${version}`]);
33168+
await this._execGitCommand(['push', 'origin', 'HEAD', '--tags']);
33169+
}
33170+
/**
33171+
* Get the latest git tag.
33172+
* @returns The latest tag as a string.
33173+
*/
33174+
async getLatestTag() {
33175+
let latestTag = '';
33176+
const options = {
33177+
listeners: {
33178+
stdout: (data) => {
33179+
latestTag += data.toString();
33180+
},
33181+
},
33182+
};
33183+
await this._execGitCommand(['describe', '--tags', '--abbrev=0'], options);
33184+
return latestTag.trim();
33185+
}
33186+
/**
33187+
* Get the commits since a specific tag.
33188+
* @param tag The tag to get commits since.
33189+
* @returns An array of commit messages.
33190+
*/
33191+
async getCommitsSinceTag(tag) {
33192+
let commits = '';
33193+
const options = {
33194+
listeners: {
33195+
stdout: (data) => {
33196+
commits += data.toString();
33197+
},
33198+
},
33199+
};
33200+
await this._execGitCommand(['log', `${tag}..HEAD`, '--oneline'], options);
33201+
return commits.split('\n').filter(Boolean);
3317433202
}
3317533203
/**
3317633204
* Creates a pull request for the specified branch and version.
@@ -33192,7 +33220,7 @@ class GitService {
3319233220
repo,
3319333221
title: prTitle,
3319433222
head: branch,
33195-
base: 'main',
33223+
base: this.baseBranch,
3319633224
body: prBody,
3319733225
});
3319833226
return pr.html_url;

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@
5353
"vitest": "^3.2.4",
5454
"yaml": "^2.8.1"
5555
}
56-
}
56+
}

0 commit comments

Comments
 (0)