Skip to content

Commit 9a834d2

Browse files
committed
refactor(customUpdater): improve regex for version
- Updated regex for more accurate version - Removed unnecessary escape characters - Improved regex for version matching - Enhanced regex for version replacement - Simplified regex patterns
1 parent 09a681a commit 9a834d2

5 files changed

Lines changed: 48 additions & 93 deletions

File tree

dist/index.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Author: Taj <[email protected]>
77
* Homepage: https://github.com/taj54/universal-version-bump#readme
88
* License: MIT
9-
* Generated on Mon, 01 Sep 2025 13:02:11 GMT
9+
* Generated on Mon, 01 Sep 2025 13:24:50 GMT
1010
*/
1111
require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap
1212
/******/ var __webpack_modules__ = ({
@@ -32798,11 +32798,6 @@ async function initializeServices() {
3279832798
}
3279932799
async function run() {
3280032800
try {
32801-
console.log('RELEASE_TYPE:', config_1.RELEASE_TYPE);
32802-
console.log('TARGET_PLATFORM:', config_1.TARGET_PLATFORM);
32803-
console.log('GIT_TAG:', config_1.GIT_TAG);
32804-
console.log('TARGET_PATH:', config_1.TARGET_PATH);
32805-
console.log('BUMP_TARGETS:', config_1.BUMP_TARGETS);
3280632801
process.chdir(config_1.TARGET_PATH);
3280732802
const releaseType = config_1.RELEASE_TYPE;
3280832803
const targetPlatform = config_1.TARGET_PLATFORM;
@@ -33414,10 +33409,9 @@ class CustomUpdater {
3341433409
return this.currentVersion;
3341533410
}
3341633411
try {
33417-
// eslint-disable-next-line no-useless-escape
33418-
const regex = new RegExp(`(${this.variableName}\s*=\s*['"]?)([\\d.]+)(['"]?)`);
33412+
const regex = new RegExp(`"${this.variableName}"\\s*:\\s*["']((?:[0-9]+\\.){2}[0-9]+(?:-[a-zA-Z0-9_.-]+)?(?:\\+[a-zA-Z0-9_.-]+)?)["']`);
3341933413
this.currentVersion = this.manifestParser.getVersion(this.filePath, 'regex', {
33420-
regex: regex,
33414+
regex,
3342133415
});
3342233416
return this.currentVersion;
3342333417
}
@@ -33433,9 +33427,9 @@ class CustomUpdater {
3343333427
}
3343433428
const newVersion = (0, utils_1.calculateNextVersion)(oldVersion, releaseType);
3343533429
// eslint-disable-next-line no-useless-escape
33436-
const regexReplace = new RegExp(`(${this.variableName}\s*=\s*['"]?)${oldVersion}(['"]?)`);
33430+
const regexReplace = new RegExp(`"${this.variableName}"\\s*:\\s*["']${oldVersion}["']`);
3343733431
this.manifestParser.updateVersion(this.filePath, newVersion, 'regex', {
33438-
regexReplace: regexReplace,
33432+
regexReplace,
3343933433
});
3344033434
core.info(`Bumped ${this.variableName} in ${this.filePath} from ${oldVersion} to ${newVersion}`);
3344133435
return newVersion;

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.

src/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ async function initializeServices() {
2929

3030
export async function run() {
3131
try {
32-
console.log('RELEASE_TYPE:', RELEASE_TYPE);
33-
console.log('TARGET_PLATFORM:', TARGET_PLATFORM);
34-
console.log('GIT_TAG:', GIT_TAG);
35-
console.log('TARGET_PATH:', TARGET_PATH);
36-
console.log('BUMP_TARGETS:', BUMP_TARGETS);
37-
3832
process.chdir(TARGET_PATH);
3933
const releaseType = RELEASE_TYPE;
4034
const targetPlatform = TARGET_PLATFORM;

src/updaters/customUpdater.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ export class CustomUpdater implements UpdaterInterface {
2929
}
3030

3131
try {
32-
// eslint-disable-next-line no-useless-escape
33-
const regex = new RegExp(`(${this.variableName}\s*=\s*['"]?)([\\d.]+)(['"]?)`);
32+
const regex: RegExp = new RegExp(
33+
`"${this.variableName}"\\s*:\\s*["']((?:[0-9]+\\.){2}[0-9]+(?:-[a-zA-Z0-9_.-]+)?(?:\\+[a-zA-Z0-9_.-]+)?)["']`,
34+
);
3435
this.currentVersion = this.manifestParser.getVersion(this.filePath, 'regex', {
35-
regex: regex,
36+
regex,
3637
});
3738
return this.currentVersion;
3839
} catch (error) {
@@ -51,10 +52,10 @@ export class CustomUpdater implements UpdaterInterface {
5152

5253
const newVersion = calculateNextVersion(oldVersion, releaseType);
5354
// eslint-disable-next-line no-useless-escape
54-
const regexReplace = new RegExp(`(${this.variableName}\s*=\s*['"]?)${oldVersion}(['"]?)`);
55+
const regexReplace: RegExp = new RegExp(`"${this.variableName}"\\s*:\\s*["']${oldVersion}["']`);
5556

5657
this.manifestParser.updateVersion(this.filePath, newVersion, 'regex', {
57-
regexReplace: regexReplace,
58+
regexReplace,
5859
});
5960

6061
core.info(
Lines changed: 36 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,56 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
22
import { CustomUpdater } from '../../src/updaters/customUpdater';
33
import { FileHandler, ManifestParser } from '../../src/utils';
4-
import * as core from '@actions/core';
54

65
vi.mock('../../src/utils/fileHandler');
76
vi.mock('../../src/utils/manifestParser');
8-
vi.mock('@actions/core');
97

108
describe('CustomUpdater', () => {
11-
let customUpdater: CustomUpdater;
12-
let mockFileHandler: vi.Mocked<FileHandler>;
13-
let mockManifestParser: vi.Mocked<ManifestParser>;
14-
15-
const filePath = 'test.txt';
16-
const variableName = 'APP_VERSION';
9+
let fileHandler: FileHandler;
10+
let manifestParser: ManifestParser;
1711

1812
beforeEach(() => {
19-
mockFileHandler = new FileHandler() as vi.Mocked<FileHandler>;
20-
mockManifestParser = new ManifestParser(mockFileHandler) as vi.Mocked<ManifestParser>;
21-
customUpdater = new CustomUpdater(filePath, variableName);
22-
23-
// Mock the constructor's internal assignments
24-
(customUpdater as unknown).fileHandler = mockFileHandler;
25-
(customUpdater as unknown).manifestParser = mockManifestParser;
13+
fileHandler = new FileHandler();
14+
manifestParser = new ManifestParser(fileHandler);
2615
});
2716

28-
it('should always return true for canHandle', () => {
29-
expect(customUpdater.canHandle()).toBe(true);
17+
it('should always be able to handle the request', () => {
18+
const updater = new CustomUpdater('dummy.txt', 'version');
19+
expect(updater.canHandle()).toBe(true);
3020
});
3121

32-
describe('getCurrentVersion', () => {
33-
it('should return the current version if found', () => {
34-
mockManifestParser.getVersion.mockReturnValue('1.0.0');
35-
expect(customUpdater.getCurrentVersion()).toBe('1.0.0');
36-
expect(mockManifestParser.getVersion).toHaveBeenCalledWith(
37-
filePath,
38-
'regex',
39-
expect.objectContaining({ regex: expect.any(RegExp) }),
40-
);
41-
});
42-
43-
it('should return null if version is not found', () => {
44-
mockManifestParser.getVersion.mockReturnValue(null);
45-
expect(customUpdater.getCurrentVersion()).toBeNull();
46-
});
47-
48-
it('should return null and log debug message if manifestParser.getVersion throws an error', () => {
49-
const error = new Error('Parse error');
50-
mockManifestParser.getVersion.mockImplementation(() => {
51-
throw error;
52-
});
53-
const coreDebugSpy = vi.spyOn(core, 'debug').mockImplementation(() => {});
54-
55-
expect(customUpdater.getCurrentVersion()).toBeNull();
56-
expect(coreDebugSpy).toHaveBeenCalledWith(
57-
`Could not read or parse version from ${filePath}: ${error}`,
58-
);
59-
});
22+
it('should get the current version from a custom file', () => {
23+
const updater = new CustomUpdater('dummy.txt', 'version');
24+
updater['manifestParser'] = manifestParser;
25+
const getVersionSpy = vi.spyOn(manifestParser, 'getVersion').mockReturnValue('1.2.3');
26+
const version = updater.getCurrentVersion();
27+
expect(getVersionSpy).toHaveBeenCalled();
28+
expect(version).toBe('1.2.3');
6029
});
6130

62-
describe('bumpVersion', () => {
63-
it('should successfully bump the version', () => {
64-
vi.spyOn(customUpdater, 'getCurrentVersion').mockReturnValue('1.0.0');
65-
mockManifestParser.updateVersion.mockReturnValue(undefined);
66-
const coreInfoSpy = vi.spyOn(core, 'info').mockImplementation(() => {});
67-
68-
const newVersion = customUpdater.bumpVersion('patch');
69-
70-
expect(newVersion).toBe('1.0.1');
71-
expect(mockManifestParser.updateVersion).toHaveBeenCalledWith(
72-
filePath,
73-
'1.0.1',
74-
'regex',
75-
expect.objectContaining({ regexReplace: expect.any(RegExp) }),
76-
);
77-
expect(coreInfoSpy).toHaveBeenCalledWith(
78-
`Bumped ${variableName} in ${filePath} from 1.0.0 to 1.0.1`,
79-
);
80-
});
31+
it('should bump the version in a custom file', () => {
32+
const updater = new CustomUpdater('dummy.txt', 'version');
33+
updater['manifestParser'] = manifestParser;
34+
vi.spyOn(manifestParser, 'getVersion').mockReturnValue('1.2.3');
35+
const updateVersionSpy = vi.spyOn(manifestParser, 'updateVersion');
36+
const newVersion = updater.bumpVersion('patch');
37+
expect(newVersion).toBe('1.2.4');
38+
expect(updateVersionSpy).toHaveBeenCalled();
39+
});
8140

82-
it('should throw an error if current version is not found', () => {
83-
vi.spyOn(customUpdater, 'getCurrentVersion').mockReturnValue(null);
41+
it('should return null if the version is not found', () => {
42+
const updater = new CustomUpdater('dummy.txt', 'version');
43+
updater['manifestParser'] = manifestParser;
44+
vi.spyOn(manifestParser, 'getVersion').mockReturnValue(null);
45+
expect(updater.getCurrentVersion()).toBeNull();
46+
});
8447

85-
expect(() => customUpdater.bumpVersion('patch')).toThrow(
86-
`Could not find current version for variable '${variableName}' in file '${filePath}'`,
87-
);
88-
});
48+
it('should throw an error when bumping if the version is not found', () => {
49+
const updater = new CustomUpdater('dummy.txt', 'version');
50+
updater['manifestParser'] = manifestParser;
51+
vi.spyOn(manifestParser, 'getVersion').mockReturnValue(null);
52+
expect(() => updater.bumpVersion('patch')).toThrow(
53+
"Could not find current version for variable 'version' in file 'dummy.txt'",
54+
);
8955
});
9056
});

0 commit comments

Comments
 (0)