1 | const fs = require("fs"); |
2 | const path = require("path"); |
3 |
|
4 | class FileHandler { |
5 | static RecursiveMkdir(file) { |
6 | let currentFile = path.normalize(file); |
7 | let pathWorks = false; |
8 | let backToStart = false; |
9 | if (!fs.existsSync(path.normalize(file))) { |
10 | while (!pathWorks) { |
11 | if (fs.existsSync(path.normalize(currentFile))) { |
12 | pathWorks = true; |
13 | while (!backToStart) { |
14 | if (!fs.existsSync(path.dirname(currentFile))) { |
15 | fs.mkdirSync(path.dirname(currentFile)); |
16 | } |
17 | if (fs.existsSync(path.normalize(file))) { |
18 | backToStart = true; |
19 | break; |
20 | } |
21 | currentFile = currentFile.slice(0, -3); |
22 | } |
23 | break; |
24 | } else { |
25 | currentFile += "\\.." |
26 | } |
27 | } |
28 | } |
29 | } |
30 | static AppendFile(file, data) { |
31 | if (typeof file != "string") throw new TypeError(`Expected string, got ${typeof file} for file`); |
32 | if (typeof data != "string") throw new TypeError(`Expected string, got ${typeof data} for data`); |
33 | if (!fs.existsSync(path.dirname(file))) { |
34 | FileHandler.RecursiveMkdir(path.dirname(file)); |
35 | fs.appendFileSync(file, data); |
36 | } else fs.appendFileSync(file, data); |
37 | } |
38 | } |
39 |
|
40 | module.exports = FileHandler; |
41 |
|