Skip to content

Commit f04d947

Browse files
committed
refactor(registry): replace fs with fileHandler
- Improved file handling using FileHandler - Enhanced error handling for file operations - Simplified updater loading process - Added readDir method to FileHandler - Updated tests for UpdaterRegistry
1 parent 344beb5 commit f04d947

5 files changed

Lines changed: 61 additions & 11 deletions

File tree

dist/index.js

Lines changed: 17 additions & 5 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 Wed, 27 Aug 2025 08:13:44 GMT
9+
* Generated on Wed, 27 Aug 2025 09:27:33 GMT
1010
*/
1111
require('./sourcemap-register.js');/******/ (() => { // webpackBootstrap
1212
/******/ var __webpack_modules__ = ({
@@ -32903,20 +32903,20 @@ var __importStar = (this && this.__importStar) || (function () {
3290332903
Object.defineProperty(exports, "__esModule", ({ value: true }));
3290432904
exports.UpdaterRegistry = void 0;
3290532905
const path = __importStar(__nccwpck_require__(6928));
32906-
const fs = __importStar(__nccwpck_require__(9896));
32906+
const fileHandler_1 = __nccwpck_require__(1013);
3290732907
/**
3290832908
* Registry for managing updaters.
3290932909
*/
3291032910
class UpdaterRegistry {
3291132911
constructor() {
3291232912
this.updaters = new Map();
32913+
this.fileHandler = new fileHandler_1.FileHandler();
3291332914
}
3291432915
/**
3291532916
* Dynamically loads and registers all updaters from the updaters directory.
3291632917
*/
32917-
async loadUpdaters() {
32918-
const updatersPath = path.join(__dirname, '../updaters');
32919-
const files = fs.readdirSync(updatersPath);
32918+
async loadUpdaters(updatersPath = path.join(__dirname, '../updaters')) {
32919+
const files = this.fileHandler.readDir(updatersPath);
3292032920
for (const file of files) {
3292132921
if (file.endsWith('Updater.ts')) {
3292232922
const modulePath = path.join(updatersPath, file);
@@ -33374,6 +33374,18 @@ class FileHandler {
3337433374
writeFile(filePath, content) {
3337533375
fs_1.default.writeFileSync(filePath, content);
3337633376
}
33377+
/**
33378+
* Reads the contents of a directory.
33379+
* @param dirPath The path to the directory.
33380+
* @returns An array of file names in the directory.
33381+
* @throws Error if the directory does not exist.
33382+
*/
33383+
readDir(dirPath) {
33384+
if (!this.fileExists(dirPath)) {
33385+
throw new errors_1.FileNotFoundError(`Directory not found: ${dirPath}`);
33386+
}
33387+
return fs_1.default.readdirSync(dirPath);
33388+
}
3337733389
}
3337833390
exports.FileHandler = FileHandler;
3337933391

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/registry/updaterRegistry.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { UpdaterInterface as IUpdater } from '../interface';
22
import * as path from 'path';
3-
import * as fs from 'fs';
3+
import { FileHandler } from '../utils/fileHandler';
44

55
/**
66
* Registry for managing updaters.
77
*/
88
export class UpdaterRegistry {
99
private updaters: Map<string, IUpdater> = new Map();
10+
private fileHandler: FileHandler;
11+
12+
constructor() {
13+
this.fileHandler = new FileHandler();
14+
}
1015

1116
/**
1217
* Dynamically loads and registers all updaters from the updaters directory.
1318
*/
14-
async loadUpdaters(): Promise<void> {
15-
const updatersPath = path.join(__dirname, '../updaters');
16-
const files = fs.readdirSync(updatersPath);
17-
19+
async loadUpdaters(updatersPath: string = path.join(__dirname, '../updaters')): Promise<void> {
20+
const files = this.fileHandler.readDir(updatersPath);
1821
for (const file of files) {
1922
if (file.endsWith('Updater.ts')) {
2023
const modulePath = path.join(updatersPath, file);

src/utils/fileHandler.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,17 @@ export class FileHandler {
3636
writeFile(filePath: string, content: string): void {
3737
fs.writeFileSync(filePath, content);
3838
}
39+
40+
/**
41+
* Reads the contents of a directory.
42+
* @param dirPath The path to the directory.
43+
* @returns An array of file names in the directory.
44+
* @throws Error if the directory does not exist.
45+
*/
46+
readDir(dirPath: string): string[] {
47+
if (!this.fileExists(dirPath)) {
48+
throw new FileNotFoundError(`Directory not found: ${dirPath}`);
49+
}
50+
return fs.readdirSync(dirPath);
51+
}
3952
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { UpdaterRegistry } from '../../src/registry';
3+
4+
describe('UpdaterRegistry', () => {
5+
it('should load all updaters from the updaters directory', async () => {
6+
const registry = new UpdaterRegistry();
7+
await registry.loadUpdaters();
8+
const allUpdaters = registry.getAllUpdaters();
9+
10+
// There are 6 updaters in the src/updaters directory
11+
expect(allUpdaters).toHaveLength(6);
12+
13+
// Check if all platforms are loaded
14+
const platforms = allUpdaters.map((updater) => updater.platform);
15+
expect(platforms).toContain('node');
16+
expect(platforms).toContain('python');
17+
expect(platforms).toContain('rust');
18+
expect(platforms).toContain('go');
19+
expect(platforms).toContain('docker');
20+
expect(platforms).toContain('php');
21+
});
22+
});

0 commit comments

Comments
 (0)