| 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 | let loopCount = 0; | 
| 10 | if (!fs.existsSync(path.normalize(file))) { | 
| 11 | while (!pathWorks) { | 
| 12 | loopCount++; | 
| 13 | if (loopCount >= 256) throw new Error("Input file path cannot be created"); | 
| 14 | if (fs.existsSync(path.normalize(currentFile))) { | 
| 15 | pathWorks = true; | 
| 16 | loopCount = 0; | 
| 17 | while (!backToStart) { | 
| 18 | loopCount++; | 
| 19 | if (loopCount >= 256) throw new Error("Input file path cannot be created"); | 
| 20 | if (!fs.existsSync(path.dirname(currentFile))) { | 
| 21 | fs.mkdirSync(path.dirname(currentFile)); | 
| 22 | } | 
| 23 | if (fs.existsSync(path.normalize(file))) { | 
| 24 | backToStart = true; | 
| 25 | break; | 
| 26 | } | 
| 27 | currentFile = currentFile.slice(0, -3); | 
| 28 | } | 
| 29 | break; | 
| 30 | } else { | 
| 31 | currentFile += "\\.." | 
| 32 | } | 
| 33 | } | 
| 34 | } | 
| 35 | } | 
| 36 | static AppendFile(file, data) { | 
| 37 | if (typeof file != "string") throw new TypeError(`Expected string, got ${typeof file} for file`); | 
| 38 | if (typeof data != "string") throw new TypeError(`Expected string, got ${typeof data} for data`); | 
| 39 | if (!fs.existsSync(path.dirname(file))) { | 
| 40 | FileHandler.RecursiveMkdir(path.dirname(file)); | 
| 41 | fs.appendFileSync(file, data); | 
| 42 | } else fs.appendFileSync(file, data); | 
| 43 | } | 
| 44 | } | 
| 45 |  | 
| 46 | module.exports = FileHandler; | 
| 47 |  |