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