Skip to content

Commit 09f4f9d

Browse files
vdiezclaude
andcommitted
Remove fs-extra dependency and centralized deps.ts
Replace fs-extra with native Node.js fs module throughout the codebase: - fsExtra.remove() -> fs.promises.rm() with recursive/force options - fsExtra.ensureDir() -> fs.promises.mkdir() with recursive option - fsExtra.exists() -> custom function using fs.promises.access() Move dependency injection interfaces from centralized deps.ts to local interfaces in each module (FileDeps, ScannerCliFsDeps, etc.), improving code locality and maintainability. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9489674 commit 09f4f9d

16 files changed

+290
-269
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"adm-zip": "0.5.16",
1919
"axios": "1.13.2",
2020
"commander": "13.1.0",
21-
"fs-extra": "11.3.3",
2221
"hpagent": "1.2.0",
2322
"node-forge": "1.3.3",
2423
"properties-file": "3.6.3",
@@ -29,7 +28,6 @@
2928
},
3029
"devDependencies": {
3130
"@types/adm-zip": "0.5.7",
32-
"@types/fs-extra": "11.0.4",
3331
"@types/node": "24.10.4",
3432
"@types/node-forge": "1.3.14",
3533
"@types/proxy-from-env": "1.0.4",

scripts/generate-package-json.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,27 @@
1717
* along with this program; if not, write to the Free Software Foundation,
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
20-
import * as path from 'path';
20+
import * as path from 'node:path';
2121
import * as toml from 'toml';
22-
import { ensureDir, readJson, writeJson } from 'fs-extra';
23-
import { readFile } from 'node:fs/promises';
22+
import { readFile, writeFile, mkdir } from 'node:fs/promises';
23+
24+
async function readJson(filePath: string): Promise<any> {
25+
const content = await readFile(filePath, 'utf-8');
26+
return JSON.parse(content);
27+
}
28+
29+
async function writeJson(
30+
filePath: string,
31+
data: any,
32+
options: { spaces?: number } = {},
33+
): Promise<void> {
34+
const content = JSON.stringify(data, null, options.spaces);
35+
await writeFile(filePath, content + '\n');
36+
}
37+
38+
async function ensureDir(dirPath: string): Promise<void> {
39+
await mkdir(dirPath, { recursive: true });
40+
}
2441

2542
async function main() {
2643
// Paths

src/deps.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/file.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,45 @@
1919
*/
2020
import AdmZip from 'adm-zip';
2121
import crypto from 'node:crypto';
22-
import fsExtra from 'fs-extra';
22+
import fs from 'node:fs';
2323
import path from 'node:path';
2424
import tarStream from 'tar-stream';
2525
import zlib from 'node:zlib';
2626
import { SONAR_CACHE_DIR, UNARCHIVE_SUFFIX } from './constants';
27-
import { defaultFsDeps, FsDeps } from './deps';
2827
import { LogLevel, log } from './logging';
2928
import { CacheFileData, ScannerProperties, ScannerProperty } from './types';
3029

30+
export interface FileDeps {
31+
existsSync: typeof fs.existsSync;
32+
readFile: typeof fs.readFile;
33+
remove: (path: string) => Promise<void>;
34+
mkdirSync: typeof fs.mkdirSync;
35+
createReadStream: typeof fs.createReadStream;
36+
createWriteStream: typeof fs.createWriteStream;
37+
}
38+
39+
const defaultFileDeps: FileDeps = {
40+
existsSync: fs.existsSync,
41+
readFile: fs.readFile,
42+
remove: (filePath: string) => fs.promises.rm(filePath, { recursive: true, force: true }),
43+
mkdirSync: fs.mkdirSync,
44+
createReadStream: fs.createReadStream,
45+
createWriteStream: fs.createWriteStream,
46+
};
47+
3148
export async function getCacheFileLocation(
3249
properties: ScannerProperties,
3350
{ checksum, filename, alias }: CacheFileData,
34-
fsDeps: FsDeps = defaultFsDeps,
51+
deps: Partial<FileDeps> = {},
3552
) {
53+
const fsDeps = { ...defaultFileDeps, ...deps };
3654
const filePath = path.join(getParentCacheDirectory(properties), checksum, filename);
3755
if (fsDeps.existsSync(filePath)) {
3856
log(LogLevel.DEBUG, alias, 'version found in cache:', filename);
3957

4058
// validate cache
4159
try {
42-
await validateChecksum(filePath, checksum, fsDeps);
60+
await validateChecksum(filePath, checksum, deps);
4361
} catch (error) {
4462
await fsDeps.remove(filePath);
4563
throw error;
@@ -55,8 +73,9 @@ export async function getCacheFileLocation(
5573
export async function extractArchive(
5674
fromPath: string,
5775
toPath: string,
58-
fsDeps: FsDeps = defaultFsDeps,
76+
deps: Partial<FileDeps> = {},
5977
) {
78+
const fsDeps = { ...defaultFileDeps, ...deps };
6079
log(LogLevel.DEBUG, `Extracting ${fromPath} to ${toPath}`);
6180
if (fromPath.endsWith('.tar.gz')) {
6281
const tarFilePath = fromPath;
@@ -68,7 +87,7 @@ export async function extractArchive(
6887
const filePath = path.join(toPath, header.name);
6988

7089
// Ensure the parent directory exists
71-
await fsExtra.ensureDir(path.dirname(filePath));
90+
fsDeps.mkdirSync(path.dirname(filePath), { recursive: true });
7291

7392
stream.pipe(fsDeps.createWriteStream(filePath, { mode: header.mode }));
7493
stream.on('end', next); // End of file, move onto next file
@@ -97,7 +116,8 @@ export async function extractArchive(
97116
}
98117
}
99118

100-
async function generateChecksum(filepath: string, fsDeps: FsDeps = defaultFsDeps) {
119+
async function generateChecksum(filepath: string, deps: Partial<FileDeps> = {}) {
120+
const fsDeps = { ...defaultFileDeps, ...deps };
101121
return new Promise((resolve, reject) => {
102122
fsDeps.readFile(filepath, (err, data) => {
103123
if (err) {
@@ -112,11 +132,11 @@ async function generateChecksum(filepath: string, fsDeps: FsDeps = defaultFsDeps
112132
export async function validateChecksum(
113133
filePath: string,
114134
expectedChecksum: string,
115-
fsDeps: FsDeps = defaultFsDeps,
135+
deps: Partial<FileDeps> = {},
116136
) {
117137
if (expectedChecksum) {
118138
log(LogLevel.DEBUG, `Verifying checksum ${expectedChecksum}`);
119-
const checksum = await generateChecksum(filePath, fsDeps);
139+
const checksum = await generateChecksum(filePath, deps);
120140

121141
log(LogLevel.DEBUG, `Checksum Value: ${checksum}`);
122142
if (checksum !== expectedChecksum) {
@@ -132,8 +152,9 @@ export async function validateChecksum(
132152
export async function getCacheDirectories(
133153
properties: ScannerProperties,
134154
{ checksum, filename }: CacheFileData,
135-
fsDeps: FsDeps = defaultFsDeps,
155+
deps: Partial<FileDeps> = {},
136156
) {
157+
const fsDeps = { ...defaultFileDeps, ...deps };
137158
const archivePath = path.join(getParentCacheDirectory(properties), checksum, filename);
138159
const unarchivePath = path.join(
139160
getParentCacheDirectory(properties),

src/java.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* along with this program; if not, write to the Free Software Foundation,
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
20+
import fs from 'node:fs';
2021
import path from 'node:path';
2122
import semver, { SemVer } from 'semver';
2223
import {
@@ -27,7 +28,6 @@ import {
2728
SONARQUBE_JRE_PROVISIONING_MIN_VERSION,
2829
UNARCHIVE_SUFFIX,
2930
} from './constants';
30-
import { defaultFsDeps, FsDeps } from './deps';
3131
import {
3232
extractArchive,
3333
getCacheDirectories,
@@ -44,8 +44,16 @@ import {
4444
ScannerProperty,
4545
} from './types';
4646

47+
export interface JavaFsDeps {
48+
remove: (path: string) => Promise<void>;
49+
}
50+
51+
const defaultFsDeps: JavaFsDeps = {
52+
remove: (filePath: string) => fs.promises.rm(filePath, { recursive: true, force: true }),
53+
};
54+
4755
export interface JavaDeps {
48-
fsDeps?: FsDeps;
56+
fsDeps?: JavaFsDeps;
4957
fetchFn?: typeof fetch;
5058
downloadFn?: typeof download;
5159
getCacheFileLocationFn?: typeof getCacheFileLocation;

0 commit comments

Comments
 (0)