Skip to content

Commit c359d1f

Browse files
author
Taois
committed
feat: fs-extra封装
1 parent 0f6a9ae commit c359d1f

File tree

9 files changed

+165
-8
lines changed

9 files changed

+165
-8
lines changed

controllers/admin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import path from 'path';
7-
import fs from 'fs-extra';
7+
import fs from '../utils/fsWrapper.js';
88
import { validateBasicAuth } from '../utils/api_validate.js';
99

1010
// 导入子控制器

controllers/admin/backupController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import fs from 'fs-extra';
2+
import fs from '../../utils/fsWrapper.js';
33
import { fileURLToPath } from 'url';
44

55
const __filename = fileURLToPath(import.meta.url);

controllers/admin/filesController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* 提供文件列表、读取、写入、删除功能
44
*/
55

6-
import fs from 'fs-extra';
6+
import fs from '../../utils/fsWrapper.js';
77
import path from 'path';
88
import mime from 'mime-types';
99

controllers/admin/logsController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* 提供日志读取
44
*/
55

6-
import fs from 'fs-extra';
6+
import fs from '../../utils/fsWrapper.js';
77
import path from 'path';
88

99
// 读取日志

controllers/admin/sourcesController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* 提供源列表、验证、语法检查、模板获取等功能
44
*/
55

6-
import fs from 'fs-extra';
6+
import fs from '../../utils/fsWrapper.js';
77
import path from 'path';
88
import vm from 'vm';
99
import { execFile } from 'child_process';

controllers/admin/subController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import fs from 'fs-extra';
2+
import fs from '../../utils/fsWrapper.js';
33
import { fileURLToPath } from 'url';
44

55
const __filename = fileURLToPath(import.meta.url);

controllers/admin/systemController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import os from 'os';
77
import { exec } from 'child_process';
88
import util from 'util';
99
import path from 'path';
10-
import fs from 'fs-extra';
10+
import fs from '../../utils/fsWrapper.js';
1111

1212
const execPromise = util.promisify(exec);
1313

utils/admin/logReader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs-extra';
1+
import fs from '../fsWrapper.js';
22
import path from 'path';
33

44
export async function readLogLines(lines = 50) {

utils/fsWrapper.js

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* Wrapper for native fs module to simulate fs-extra methods.
3+
* This makes it easy to switch between native fs and fs-extra.
4+
*/
5+
import fs from 'fs';
6+
import path from 'path';
7+
8+
// Promisified fs methods are available in fs.promises
9+
const fsp = fs.promises;
10+
11+
/**
12+
* Ensures that the directory exists. If the directory structure does not exist, it is created.
13+
* @param {string} dirPath
14+
*/
15+
async function ensureDir(dirPath) {
16+
try {
17+
await fsp.mkdir(dirPath, { recursive: true });
18+
} catch (err) {
19+
if (err.code !== 'EEXIST') throw err;
20+
}
21+
}
22+
23+
/**
24+
* Ensures that the directory exists synchronously.
25+
* @param {string} dirPath
26+
*/
27+
function ensureDirSync(dirPath) {
28+
try {
29+
fs.mkdirSync(dirPath, { recursive: true });
30+
} catch (err) {
31+
if (err.code !== 'EEXIST') throw err;
32+
}
33+
}
34+
35+
/**
36+
* Checks if a file or directory exists.
37+
* @param {string} fileOrDirPath
38+
* @returns {Promise<boolean>}
39+
*/
40+
async function pathExists(fileOrDirPath) {
41+
try {
42+
await fsp.access(fileOrDirPath);
43+
return true;
44+
} catch {
45+
return false;
46+
}
47+
}
48+
49+
/**
50+
* Copies a file or directory. The directory can have contents.
51+
* @param {string} src
52+
* @param {string} dest
53+
* @param {object} [options]
54+
*/
55+
async function copy(src, dest, options = {}) {
56+
const stats = await fsp.stat(src);
57+
if (stats.isDirectory()) {
58+
await ensureDir(dest);
59+
const entries = await fsp.readdir(src);
60+
for (const entry of entries) {
61+
const srcPath = path.join(src, entry);
62+
const destPath = path.join(dest, entry);
63+
await copy(srcPath, destPath, options);
64+
}
65+
} else {
66+
await ensureDir(path.dirname(dest));
67+
if (options.overwrite !== false) {
68+
await fsp.copyFile(src, dest);
69+
} else {
70+
const exists = await pathExists(dest);
71+
if (!exists) {
72+
await fsp.copyFile(src, dest);
73+
}
74+
}
75+
}
76+
}
77+
78+
/**
79+
* Removes a file or directory. The directory can have contents.
80+
* @param {string} fileOrDirPath
81+
*/
82+
async function remove(fileOrDirPath) {
83+
try {
84+
await fsp.rm(fileOrDirPath, { recursive: true, force: true });
85+
} catch (err) {
86+
if (err.code !== 'ENOENT') throw err;
87+
}
88+
}
89+
90+
/**
91+
* Reads a JSON file and parses it.
92+
* @param {string} file
93+
* @param {object|string} [options]
94+
* @returns {Promise<any>}
95+
*/
96+
async function readJson(file, options) {
97+
const content = await fsp.readFile(file, options || 'utf-8');
98+
return JSON.parse(content);
99+
}
100+
101+
/**
102+
* Writes an object to a JSON file.
103+
* @param {string} file
104+
* @param {any} object
105+
* @param {object|string} [options]
106+
*/
107+
async function writeJson(file, object, options = {}) {
108+
const spaces = options.spaces || 2;
109+
const str = JSON.stringify(object, null, spaces);
110+
await fsp.writeFile(file, str, options);
111+
}
112+
113+
/**
114+
* Reads a JSON file synchronously and parses it.
115+
* @param {string} file
116+
* @param {object|string} [options]
117+
* @returns {any}
118+
*/
119+
function readJsonSync(file, options) {
120+
const content = fs.readFileSync(file, options || 'utf-8');
121+
return JSON.parse(content);
122+
}
123+
124+
/**
125+
* Writes an object to a JSON file synchronously.
126+
* @param {string} file
127+
* @param {any} object
128+
* @param {object|string} [options]
129+
*/
130+
function writeJsonSync(file, object, options = {}) {
131+
const spaces = options.spaces || 2;
132+
const str = JSON.stringify(object, null, spaces);
133+
fs.writeFileSync(file, str, options);
134+
}
135+
136+
// Export a combined object containing both standard fsp methods and our custom fs-extra-like methods
137+
const fsWrapper = {
138+
...fsp,
139+
// Original sync methods if needed
140+
existsSync: fs.existsSync,
141+
readFileSync: fs.readFileSync,
142+
writeFileSync: fs.writeFileSync,
143+
readdirSync: fs.readdirSync,
144+
statSync: fs.statSync,
145+
// Custom fs-extra methods
146+
ensureDir,
147+
ensureDirSync,
148+
pathExists,
149+
copy,
150+
remove,
151+
readJson,
152+
writeJson,
153+
readJsonSync,
154+
writeJsonSync,
155+
};
156+
157+
export default fsWrapper;

0 commit comments

Comments
 (0)