forked from TurboWarp/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs-utils.js
53 lines (50 loc) · 1.33 KB
/
fs-utils.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
48
49
50
51
52
53
const fs = require("fs");
const pathUtil = require("path");
/**
* Recursively read a directory.
* @param {string} directory
* @returns {Array<[string, string]>} List of tuples [name, absolutePath].
* The return result includes files in subdirectories, but not the subdirectories themselves.
*/
const recursiveReadDirectory = (directory) => {
const result = [];
for (const name of fs.readdirSync(directory)) {
if (name.startsWith(".")) {
// Ignore .eslintrc.js, .DS_Store, etc.
continue;
}
const absolutePath = pathUtil.join(directory, name);
const stat = fs.statSync(absolutePath);
if (stat.isDirectory()) {
for (const [
relativeToChildName,
childAbsolutePath,
] of recursiveReadDirectory(absolutePath)) {
// This always needs to use / on all systems
result.push([`${name}/${relativeToChildName}`, childAbsolutePath]);
}
} else {
result.push([name, absolutePath]);
}
}
return result;
};
/**
* Synchronous create a directory and any parents. Does nothing if the folder already exists.
* @param {string} directory
*/
const mkdirp = (directory) => {
try {
fs.mkdirSync(directory, {
recursive: true,
});
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
}
};
module.exports = {
recursiveReadDirectory,
mkdirp,
};