mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
utils.js refactor
This commit is contained in:
parent
d4cd019f29
commit
e5fd0aba13
1 changed files with 79 additions and 90 deletions
169
utils/utils.js
169
utils/utils.js
|
@ -1,109 +1,98 @@
|
|||
module.exports = {
|
||||
ReadFile: ReadFile,
|
||||
WriteFile: WriteFile,
|
||||
writeFileAsync: WriteFileAsync,
|
||||
AppendToFile: AppendToFile,
|
||||
Beep: Beep,
|
||||
WriteBackup: WriteBackup,
|
||||
FileExists: FileExists,
|
||||
CreatePath: CreatePath,
|
||||
GetAllFilesFromFolder: GetAllFilesFromFolder
|
||||
};
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
var logger = require('./logger.js');
|
||||
|
||||
const recievedFile = "stats/recieved";
|
||||
const manFile = "public/man.html";
|
||||
const logFile = "stats/logs";
|
||||
const dataFile = "public/data.json";
|
||||
|
||||
function ReadFile(name) {
|
||||
if (!FileExists(name))
|
||||
throw "No such file: " + name;
|
||||
return fs.readFileSync(name, "utf8");
|
||||
ReadFile: ReadFile,
|
||||
WriteFile: WriteFile,
|
||||
writeFileAsync: WriteFileAsync,
|
||||
AppendToFile: AppendToFile,
|
||||
Beep: Beep,
|
||||
WriteBackup: WriteBackup,
|
||||
FileExists: FileExists,
|
||||
CreatePath: CreatePath,
|
||||
GetAllFilesFromFolder: GetAllFilesFromFolder
|
||||
}
|
||||
|
||||
function FileExists(path) {
|
||||
return fs.existsSync(path);
|
||||
var fs = require('fs')
|
||||
|
||||
var logger = require('../utils/logger.js')
|
||||
|
||||
const recievedFile = '../stats/recieved'
|
||||
const manFile = '../public/man.html'
|
||||
const logFile = '../stats/logs'
|
||||
const dataFile = '../public/data.json'
|
||||
|
||||
function ReadFile (name) {
|
||||
if (!FileExists(name)) { throw 'No such file: ' + name }
|
||||
return fs.readFileSync(name, 'utf8')
|
||||
}
|
||||
|
||||
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 (currDir != "" && !fs.existsSync(currDir)) {
|
||||
try {
|
||||
fs.mkdirSync(currDir);
|
||||
} catch (e) { console.log("Failed to make " + currDir + " directory... "); }
|
||||
}
|
||||
currDir += "/" + p[i];
|
||||
}
|
||||
if (onlyPath == undefined || onlyPath == false)
|
||||
fs.writeFileSync(path, "");
|
||||
else
|
||||
fs.mkdirSync(path);
|
||||
|
||||
function FileExists (path) {
|
||||
return fs.existsSync(path)
|
||||
}
|
||||
|
||||
function WriteFile(content, path) {
|
||||
CreatePath(path);
|
||||
fs.writeFileSync(path, content);
|
||||
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 (currDir != '' && !fs.existsSync(currDir)) {
|
||||
try {
|
||||
fs.mkdirSync(currDir)
|
||||
} catch (e) { console.log('Failed to make ' + currDir + ' directory... ') }
|
||||
}
|
||||
currDir += '/' + p[i]
|
||||
}
|
||||
if (onlyPath == undefined || onlyPath == false) { fs.writeFileSync(path, '') } else { fs.mkdirSync(path) }
|
||||
}
|
||||
|
||||
function WriteFileAsync(content, path) {
|
||||
CreatePath(path);
|
||||
fs.writeFile(path, content, function(err) {
|
||||
if (err) {
|
||||
logger.Log("Error writing file: " + path + " (sync)", logger.GetColor("redbg"));
|
||||
}
|
||||
});
|
||||
function WriteFile (content, path) {
|
||||
CreatePath(path)
|
||||
fs.writeFileSync(path, content)
|
||||
}
|
||||
|
||||
function AppendToFile(data, file) {
|
||||
CreatePath(file);
|
||||
fs.appendFile(file, "\n" + data, function(err) {
|
||||
if (err)
|
||||
logger.Log("Error writing log file: " + file + " (sync)", logger.GetColor("redbg"));
|
||||
});
|
||||
function WriteFileAsync (content, path) {
|
||||
CreatePath(path)
|
||||
fs.writeFile(path, content, function (err) {
|
||||
if (err) {
|
||||
logger.Log('Error writing file: ' + path + ' (sync)', logger.GetColor('redbg'))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function Beep() {
|
||||
try {
|
||||
process.stdout.write('\x07');
|
||||
} catch (e) {
|
||||
console.log("error beepin");
|
||||
}
|
||||
function AppendToFile (data, file) {
|
||||
CreatePath(file)
|
||||
fs.appendFile(file, '\n' + data, function (err) {
|
||||
if (err) { logger.Log('Error writing log file: ' + file + ' (sync)', logger.GetColor('redbg')) }
|
||||
})
|
||||
}
|
||||
|
||||
function WriteBackup() {
|
||||
try {
|
||||
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString());
|
||||
} catch (e) {
|
||||
logger.Log("Error backing up data json file!", logger.GetColor("redbg"));
|
||||
console.log(e);
|
||||
}
|
||||
function Beep () {
|
||||
try {
|
||||
process.stdout.write('\x07')
|
||||
} catch (e) {
|
||||
console.log('error beepin')
|
||||
}
|
||||
}
|
||||
|
||||
function GetAllFilesFromFolder(dir) {
|
||||
|
||||
var results = [];
|
||||
|
||||
fs.readdirSync(dir).forEach(function(file) {
|
||||
|
||||
file = dir + '/' + file;
|
||||
var stat = fs.statSync(file);
|
||||
|
||||
if (stat && stat.isDirectory()) {
|
||||
results = results.concat(_getAllFilesFromFolder(file));
|
||||
} else results.push(file);
|
||||
|
||||
});
|
||||
|
||||
return results;
|
||||
function WriteBackup () {
|
||||
try {
|
||||
WriteFileAsync(ReadFile(dataFile), 'public/backs/data_' + new Date().toString())
|
||||
} catch (e) {
|
||||
logger.Log('Error backing up data json file!', logger.GetColor('redbg'))
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
function GetAllFilesFromFolder (dir) {
|
||||
var results = []
|
||||
|
||||
fs.readdirSync(dir).forEach(function (file) {
|
||||
file = dir + '/' + file
|
||||
var stat = fs.statSync(file)
|
||||
|
||||
if (stat && stat.isDirectory()) {
|
||||
results = results.concat(_getAllFilesFromFolder(file))
|
||||
} else results.push(file)
|
||||
})
|
||||
|
||||
return results
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue