module.exports = { ReadFile: ReadFile, WriteFile: WriteFile, writeFileAsync: WriteFileAsync, AppendToFile: AppendToFile, Beep: Beep, WriteBackup: WriteBackup, FileExists: FileExists, CreatePath: CreatePath }; var fs = require('fs'); var logger = require('./logger.js'); const recievedFile = "stats/recieved"; const publicFile = "public/data/public"; const staticFile = "public/data/static"; const manFile = "public/man.html"; const logFile = "stats/logs"; function ReadFile(name) { if (!FileExists(name)) throw "No such file: " + name; return fs.readFileSync(name, "utf8"); } function FileExists(path) { return fs.existsSync(path); } function CreatePath(path, onlyPath) { if (FileExists(path)) return; var p = path.split("/"); var currDir = p[0]; for (var i = 1; i < p.length; i++) { if (!fs.existsSync(currDir)) { fs.mkdirSync(currDir); } currDir += "/" + p[i]; } if (onlyPath == undefined || onlyPath == false) fs.writeFileSync(path, ""); else fs.mkdirSync(path); } function WriteFile(content, path) { CreatePath(path); fs.writeFileSync(path, content); } function WriteFileAsync(content, path) { CreatePath(path); fs.writeFile(path, content, function(err) { if (err) { logger.Log("[ERR ]: Error writing file: " + path + " (sync)", logger.GetColor("redbg")); } }); } function AppendToFile(data, file) { CreatePath(file); fs.appendFile(file, "\n" + data, function(err) { if (err) logger.Log("[ERR ]: Error writing log file: " + file + " (sync)", logger.GetColor("redbg")); }); } function Beep() { try { process.stdout.write('\x07'); } catch (e) { console.log("error beepin"); } } function WriteBackup() { try { WriteFileAsync(ReadFile(recievedFile), 'public/backs/recieved_' + new Date().toString()); //logger.Log('[SAVE]: New questions backup wrote'); } catch (e) { logger.Log("[ERR ]: Error backing up recieved file!", logger.GetColor("redbg")); console.log(e); } try { WriteFileAsync(ReadFile(publicFile), 'public/backs/public_' + new Date().toString()); //logger.Log('[SAVE]: Public questions backup wrote'); } catch (e) { logger.Log("[ERR ]: Error backing up public file!", logger.GetColor("redbg"), true); console.log(e); } }