@reldens/utils 0.25.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/file-handler.js +26 -0
- package/package.json +1 -1
package/lib/file-handler.js
CHANGED
|
@@ -31,6 +31,11 @@ class FileHandler
|
|
|
31
31
|
fs.mkdirSync(folderPath, { recursive: true });
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
readFile(filePath)
|
|
35
|
+
{
|
|
36
|
+
return fs.readFileSync(filePath);
|
|
37
|
+
}
|
|
38
|
+
|
|
34
39
|
async writeFile(fileName, content)
|
|
35
40
|
{
|
|
36
41
|
return fs.writeFile(fileName, content, this.encoding, (err) => {
|
|
@@ -43,6 +48,27 @@ class FileHandler
|
|
|
43
48
|
});
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
removeByPath(fullPath)
|
|
52
|
+
{
|
|
53
|
+
if (!fs.existsSync(fullPath)) {
|
|
54
|
+
Logger.error(`File or folder "${fullPath}" does not exist.`);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
let stats = fs.statSync(fullPath);
|
|
58
|
+
if(stats.isFile()){
|
|
59
|
+
fs.unlinkSync(fullPath);
|
|
60
|
+
Logger.info(`File "${fullPath}" has been removed.`);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
if(stats.isDirectory()){
|
|
64
|
+
fs.rmdirSync(fullPath, { recursive: true }); // Remove folder recursively
|
|
65
|
+
Logger.info(`Folder "${fullPath}" has been removed.`);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
Logger.warning(`"${fullPath}" is neither a file nor a folder.`);
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
46
72
|
}
|
|
47
73
|
|
|
48
74
|
module.exports = FileHandler;
|