Skip to content

Commit cba6766

Browse files
committed
feat: Centralize file handling and bump version to 0.7.1
Refactor file operations in updaters to use a new `FileHandler` utility,improving consistency and maintainability.Also, update the project version to 0.7.1.
1 parent e509fc1 commit cba6766

9 files changed

Lines changed: 73 additions & 63 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "universal-version-bump",
33
"displayName": "Universal Version Bump",
4-
"version": "0.7.0",
4+
"version": "0.7.1",
55
"description": "A GitHub Action to automatically bump versions across any app (Node, Python, PHP, Docker, etc.)",
66
"main": "dist/index.js",
77
"scripts": {

src/updaters/dockerUpdater.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import fs from 'fs';
21
import { ReleaseType } from 'semver';
32
import { Updater } from '../interface';
4-
import { calculateNextVersion } from '../utils';
3+
import { calculateNextVersion, FileHandler } from '../utils';
54

65
export class DockerUpdater implements Updater {
76
platform = 'docker';
87

98
canHandle(): boolean {
10-
return fs.existsSync('Dockerfile');
9+
return FileHandler.fileExists('Dockerfile');
1110
}
1211

1312
getCurrentVersion(): string | null {
14-
const content = fs.readFileSync('Dockerfile', 'utf8');
13+
const content = FileHandler.readFile('Dockerfile');
1514
const match = content.match(/LABEL version="([^"]+)"/);
1615
return match ? match[1] : null;
1716
}
@@ -21,9 +20,9 @@ export class DockerUpdater implements Updater {
2120
if (!current) throw new Error('Docker version not found');
2221

2322
const newVersion = calculateNextVersion(current, releaseType);
24-
let content = fs.readFileSync('Dockerfile', 'utf8');
23+
let content = FileHandler.readFile('Dockerfile');
2524
content = content.replace(/LABEL version="[^"]+"/, `LABEL version="${newVersion}"`);
26-
fs.writeFileSync('Dockerfile', content);
25+
FileHandler.writeFile('Dockerfile', content);
2726

2827
return newVersion;
2928
}

src/updaters/goUpdater.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import fs from 'fs';
21
import { ReleaseType } from 'semver';
32
import { Updater } from '../interface';
4-
import { calculateNextVersion } from '../utils';
3+
import { calculateNextVersion, FileHandler } from '../utils';
54

65
export class GoUpdater implements Updater {
76
platform = 'go';
87

98
canHandle(): boolean {
10-
return fs.existsSync('go.mod');
9+
return FileHandler.fileExists('go.mod');
1110
}
1211

1312
getCurrentVersion(): string | null {
1413
if (!this.canHandle()) return null;
15-
const content = fs.readFileSync('go.mod', 'utf8');
14+
const content = FileHandler.readFile('go.mod');
1615
const match = content.match(/module\s+.*\n.*v(\d+\.\d+\.\d+)/);
1716
return match ? match[1] : null;
1817
}
@@ -22,9 +21,9 @@ export class GoUpdater implements Updater {
2221
if (!current) throw new Error('Go version not found');
2322

2423
const newVersion = calculateNextVersion(current, releaseType);
25-
let content = fs.readFileSync('go.mod', 'utf8');
24+
let content = FileHandler.readFile('go.mod');
2625
content = content.replace(/v\d+\.\d+\.\d+/, `v${newVersion}`);
27-
fs.writeFileSync('go.mod', content);
26+
FileHandler.writeFile('go.mod', content);
2827

2928
return newVersion;
3029
}

src/updaters/nodeUpdater.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
import fs from 'fs';
21
import { ReleaseType } from 'semver';
32
import { Updater } from '../interface';
4-
import { calculateNextVersion } from '../utils';
3+
import { calculateNextVersion, FileHandler } from '../utils';
54

65
export class NodeUpdater implements Updater {
76
platform = 'node';
87

98
canHandle(): boolean {
10-
return fs.existsSync('package.json');
9+
return FileHandler.fileExists('package.json');
1110
}
1211

1312
getCurrentVersion(): string | null {
1413
if (!this.canHandle()) return null;
15-
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
14+
const pkg = JSON.parse(FileHandler.readFile('package.json'));
1615
return pkg.version;
1716
}
1817

1918
bumpVersion(releaseType: ReleaseType): string {
20-
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'));
19+
const pkg = JSON.parse(FileHandler.readFile('package.json'));
2120
const newVersion = calculateNextVersion(pkg.version, releaseType);
2221
pkg.version = newVersion;
23-
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
22+
FileHandler.writeFile('package.json', JSON.stringify(pkg, null, 2));
2423
return newVersion;
2524
}
2625
}

src/updaters/phpUpdater.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,41 @@
1-
import fs from 'fs';
21
import { ReleaseType } from 'semver';
32
import { Updater } from '../interface';
4-
import { calculateNextVersion } from '../utils';
3+
import { calculateNextVersion, FileHandler } from '../utils';
54

65
export class PHPUpdater implements Updater {
76
platform = 'php';
87

98
canHandle(): boolean {
109
return (
11-
fs.existsSync('composer.json') ||
12-
fs.existsSync('VERSION') ||
13-
fs.existsSync('version.php') ||
14-
fs.existsSync('config.php')
10+
FileHandler.fileExists('composer.json') ||
11+
FileHandler.fileExists('VERSION') ||
12+
FileHandler.fileExists('version.php') ||
13+
FileHandler.fileExists('config.php')
1514
);
1615
}
1716

1817
getCurrentVersion(): string | null {
1918
// composer.json
20-
if (fs.existsSync('composer.json')) {
21-
const composer = JSON.parse(fs.readFileSync('composer.json', 'utf8'));
19+
if (FileHandler.fileExists('composer.json')) {
20+
const composer = JSON.parse(FileHandler.readFile('composer.json'));
2221
return composer.version || null;
2322
}
2423

2524
// VERSION file
26-
if (fs.existsSync('VERSION')) {
27-
return fs.readFileSync('VERSION', 'utf8').trim();
25+
if (FileHandler.fileExists('VERSION')) {
26+
return FileHandler.readFile('VERSION').trim();
2827
}
2928

3029
// version.php
31-
if (fs.existsSync('version.php')) {
32-
const content = fs.readFileSync('version.php', 'utf8');
30+
if (FileHandler.fileExists('version.php')) {
31+
const content = FileHandler.readFile('version.php');
3332
const match = content.match(/['"]([\d.]+)['"]/);
3433
return match ? match[1] : null;
3534
}
3635

3736
// config.php
38-
if (fs.existsSync('config.php')) {
39-
const content = fs.readFileSync('config.php', 'utf8');
37+
if (FileHandler.fileExists('config.php')) {
38+
const content = FileHandler.readFile('config.php');
4039
const match = content.match(/'version'\s*=>\s*'([\d.]+)'/);
4140
return match ? match[1] : null;
4241
}
@@ -49,32 +48,32 @@ export class PHPUpdater implements Updater {
4948
const newVersion = calculateNextVersion(version, releaseType);
5049

5150
// composer.json
52-
if (fs.existsSync('composer.json')) {
53-
const composer = JSON.parse(fs.readFileSync('composer.json', 'utf8'));
51+
if (FileHandler.fileExists('composer.json')) {
52+
const composer = JSON.parse(FileHandler.readFile('composer.json'));
5453
composer.version = newVersion;
55-
fs.writeFileSync('composer.json', JSON.stringify(composer, null, 2));
54+
FileHandler.writeFile('composer.json', JSON.stringify(composer, null, 2));
5655
return newVersion;
5756
}
5857

5958
// VERSION file
60-
if (fs.existsSync('VERSION')) {
61-
fs.writeFileSync('VERSION', newVersion);
59+
if (FileHandler.fileExists('VERSION')) {
60+
FileHandler.writeFile('VERSION', newVersion);
6261
return newVersion;
6362
}
6463

6564
// version.php
66-
if (fs.existsSync('version.php')) {
67-
const content = fs.readFileSync('version.php', 'utf8');
65+
if (FileHandler.fileExists('version.php')) {
66+
const content = FileHandler.readFile('version.php');
6867
const updated = content.replace(/(['"])[\d.]+(['"])/, `$1${newVersion}$2`);
69-
fs.writeFileSync('version.php', updated);
68+
FileHandler.writeFile('version.php', updated);
7069
return newVersion;
7170
}
7271

7372
// config.php
74-
if (fs.existsSync('config.php')) {
75-
const content = fs.readFileSync('config.php', 'utf8');
73+
if (FileHandler.fileExists('config.php')) {
74+
const content = FileHandler.readFile('config.php');
7675
const updated = content.replace(/'version'\s*=>\s*'[\d.]+'/, `'version' => '${newVersion}'`);
77-
fs.writeFileSync('config.php', updated);
76+
FileHandler.writeFile('config.php', updated);
7877
return newVersion;
7978
}
8079

src/updaters/pythonUpdater.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import fs from 'fs';
21
import { ReleaseType } from 'semver';
32
import { Updater } from '../interface';
4-
import { calculateNextVersion } from '../utils';
3+
import { calculateNextVersion, FileHandler } from '../utils';
54

65
export class PythonUpdater implements Updater {
76
platform = 'python';
87

98
canHandle(): boolean {
10-
return fs.existsSync('pyproject.toml') || fs.existsSync('setup.py');
9+
return FileHandler.fileExists('pyproject.toml') || FileHandler.fileExists('setup.py');
1110
}
1211

1312
getCurrentVersion(): string | null {
14-
if (fs.existsSync('pyproject.toml')) {
15-
const content = fs.readFileSync('pyproject.toml', 'utf8');
13+
if (FileHandler.fileExists('pyproject.toml')) {
14+
const content = FileHandler.readFile('pyproject.toml');
1615
const match = content.match(/version\s*=\s*"([^"]+)"/);
1716
return match ? match[1] : null;
1817
}
19-
if (fs.existsSync('setup.py')) {
20-
const content = fs.readFileSync('setup.py', 'utf8');
18+
if (FileHandler.fileExists('setup.py')) {
19+
const content = FileHandler.readFile('setup.py');
2120
const match = content.match(/version\s*=\s*["']([^"']+)["']/);
2221
return match ? match[1] : null;
2322
}
@@ -30,14 +29,14 @@ export class PythonUpdater implements Updater {
3029

3130
const newVersion = calculateNextVersion(current, releaseType);
3231

33-
if (fs.existsSync('pyproject.toml')) {
34-
let content = fs.readFileSync('pyproject.toml', 'utf8');
32+
if (FileHandler.fileExists('pyproject.toml')) {
33+
let content = FileHandler.readFile('pyproject.toml');
3534
content = content.replace(/version\s*=\s*"[^"]+"/, `version = "${newVersion}"`);
36-
fs.writeFileSync('pyproject.toml', content);
37-
} else if (fs.existsSync('setup.py')) {
38-
let content = fs.readFileSync('setup.py', 'utf8');
35+
FileHandler.writeFile('pyproject.toml', content);
36+
} else if (FileHandler.fileExists('setup.py')) {
37+
let content = FileHandler.readFile('setup.py');
3938
content = content.replace(/version\s*=\s*["'][^"']+["']/, `version="${newVersion}"`);
40-
fs.writeFileSync('setup.py', content);
39+
FileHandler.writeFile('setup.py', content);
4140
}
4241

4342
return newVersion;

src/updaters/rustUpdater.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import fs from 'fs';
21
import { ReleaseType } from 'semver';
32
import { Updater } from '../interface';
4-
import { calculateNextVersion } from '../utils';
3+
import { calculateNextVersion, FileHandler } from '../utils';
54

65
export class RustUpdater implements Updater {
76
platform = 'rust';
87

98
canHandle(): boolean {
10-
return fs.existsSync('Cargo.toml');
9+
return FileHandler.fileExists('Cargo.toml');
1110
}
1211

1312
getCurrentVersion(): string | null {
1413
if (!this.canHandle()) return null;
15-
const content = fs.readFileSync('Cargo.toml', 'utf8');
14+
const content = FileHandler.readFile('Cargo.toml');
1615
const match = content.match(/version\s*=\s*"([^"]+)"/);
1716
return match ? match[1] : null;
1817
}
@@ -22,9 +21,9 @@ export class RustUpdater implements Updater {
2221
if (!current) throw new Error('Rust version not found');
2322

2423
const newVersion = calculateNextVersion(current, releaseType);
25-
let content = fs.readFileSync('Cargo.toml', 'utf8');
24+
let content = FileHandler.readFile('Cargo.toml');
2625
content = content.replace(/version\s*=\s*"[^"]+"/, `version = "${newVersion}"`);
27-
fs.writeFileSync('Cargo.toml', content);
26+
FileHandler.writeFile('Cargo.toml', content);
2827

2928
return newVersion;
3029
}

src/utils/fileHandler.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import fs from 'fs';
2+
3+
export class FileHandler {
4+
static fileExists(filePath: string): boolean {
5+
return fs.existsSync(filePath);
6+
}
7+
8+
static readFile(filePath: string): string {
9+
return fs.readFileSync(filePath, 'utf8');
10+
}
11+
12+
static writeFile(filePath: string, content: string): void {
13+
fs.writeFileSync(filePath, content);
14+
}
15+
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './versionUtil';
2+
export * from './fileHandler';

0 commit comments

Comments
 (0)