-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathfs-extra.js
47 lines (45 loc) · 1.08 KB
/
fs-extra.js
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
const fs = require('fs');
const path = require('path');
const fsExtra = {
unlinkSync(p) {
return fs.unlinkSync(p);
},
existsSync(p) {
return fs.existsSync(p);
},
readdirSync(dir) {
return fs.readdirSync(dir);
},
mkdirSync(dir) {
if (fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
return;
}
dir.split(path.sep).forEach((part, index) => {
if (!part) return;
const partialPath = dir
.split(path.sep)
.slice(0, index + 1)
.join(path.sep);
if (!fs.existsSync(partialPath)) {
fs.mkdirSync(partialPath, { recursive: true });
}
});
},
readFileSync(file) {
return fs.readFileSync(file, 'utf8');
},
writeFileSync(file, content) {
if (!fs.existsSync(path.dirname(file))) {
fsExtra.mkdirSync(path.dirname(file));
}
return fs.writeFileSync(file, content, {});
},
copyFileSync(src, dest) {
if (!fs.existsSync(path.dirname(dest))) {
fsExtra.mkdirSync(path.dirname(dest));
}
return fs.copyFileSync(src, dest);
},
};
module.exports = fsExtra;