-
Notifications
You must be signed in to change notification settings - Fork 10
/
fileutils.js
65 lines (60 loc) · 1.97 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
//@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)) {
console.error(`${src} does not exist.`)
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[]} findAndReplace - The string to find and to replace pairs.
*/
const cpSyncModify = (src, dest, ...findAndReplace) => {
if (!fs.existsSync(src)) {
console.error(`${src} does not exist.`)
return;
}
let data = fs.readFileSync(src, "utf-8");
for (let i = 0; i < findAndReplace.length; i += 2) {
const find = findAndReplace[i];
const replace = findAndReplace[i + 1];
// Create a regular expression from the 'find' string
const regex = new RegExp(find, "g");
// Replace 'find' with 'replace'
data = data.replace(regex, replace);
}
// Write the modified data to a new file
fs.writeFileSync(dest, data, "utf-8");
}
/**
* @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 };