1 | const fs = require("fs"); |
2 | const path = require("path"); |
3 |
|
4 | class FileHandler { |
5 | static RecursiveMkdir(file) { |
6 | let paths = path.normalize(file).split(path.sep); |
7 | let curPath = (path.isAbsolute(file)) ? "" : "."; |
8 | for (var i = 0; i < paths.length; i++) { |
9 | curPath += path.sep + paths[i]; |
10 | if (!fs.existsSync(curPath)) |
11 | fs.mkdirSync(curPath); |
12 | } |
13 | } |
14 | static AppendFile(file, data) { |
15 | if (typeof file != "string") throw new TypeError(`Expected string, got ${typeof file} for file`); |
16 | if (typeof data != "string") throw new TypeError(`Expected string, got ${typeof data} for data`); |
17 | if (!fs.existsSync(path.dirname(file))) { |
18 | FileHandler.RecursiveMkdir(path.dirname(file)); |
19 | fs.appendFileSync(file, data); |
20 | } else fs.appendFileSync(file, data); |
21 | } |
22 | } |
23 |
|
24 | module.exports = FileHandler; |
25 |
|