forked from grame-cncm/faustwasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileutils.js
67 lines (59 loc) · 1.9 KB
/
fileutils.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//@ts-check
import * as fs from "fs";
import * as path from "path";
/**
import * as path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const __filename = fileURLToPath(import.meta.url);
*/
/**
* @param {string} src - The source path.
* @param {string} dest - The destination path.
*/
const cpSync = (src, dest) => {
if (!fs.existsSync(src)) return;
if (fs.lstatSync(src).isDirectory()) {
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
fs.readdirSync(src).forEach(child => cpSync(path.join(src, child), path.join(dest, child)));
} else {
fs.copyFileSync(src, dest);
}
};
/**
* @param {string} src - The source path of the file to modify and copy.
* @param {string} dest - The destination path.
* @param {string} find - The string to find.
* @param {string} replace - The string to replace.
*/
const cpSyncModify = (src, dest, find, replace) => {
fs.readFile(src, 'utf8', function (err, data) {
if (err) {
console.error(err);
return;
}
// Create a regular expression from the 'find' string
const regex = new RegExp(find, 'g');
// Replace 'find' with 'replace'
let modifiedData = data.replace(regex, replace);
// Write the modified data to a new file
fs.writeFile(dest, modifiedData, 'utf8', function (err) {
if (err) {
console.error(err);
}
});
});
}
/**
* @param {string} dir - The directory to remove.
*/
const rmSync = (dir) => {
if (!fs.existsSync(dir)) return;
if (fs.lstatSync(dir).isDirectory()) {
fs.readdirSync(dir).forEach(child => rmSync(path.join(dir, child)));
fs.rmdirSync(dir);
} else {
fs.unlinkSync(dir);
}
};
export { cpSync, cpSyncModify, rmSync };