mirror of
https://gitlab.com/jusax23/fmi.git
synced 2024-11-24 07:36:33 +01:00
169 lines
4.8 KiB
JavaScript
169 lines
4.8 KiB
JavaScript
import path from "path";
|
|
import fs from "fs";
|
|
import request from "request";
|
|
|
|
/*const path = require('path');
|
|
const fs = require("fs");
|
|
const request = require('request');*/
|
|
|
|
const dirname = path.dirname(process.argv[1]);
|
|
|
|
function F() {
|
|
var t = this;
|
|
t.data = {};
|
|
var pathfile = null;
|
|
t.connect = function(pathin, structure = false, force = false, dir=dirname) {
|
|
pathfile = jpath(dir, pathin);
|
|
if (!fs.existsSync(pathfile)||!fs.lstatSync(pathfile).isFile()) {
|
|
console.warn("Config does't exists. It will be created: "+pathfile);
|
|
}else{
|
|
t.data = t.fromTOML(fs.readFileSync(pathfile).toString());
|
|
}
|
|
if (typeof structure == "object") {
|
|
var sgroups = Object.keys(structure);
|
|
for (var i = 0; i < sgroups.length; i++) {
|
|
var sgroup = structure[sgroups[i]];
|
|
if (!t.data[sgroups[i]]) t.data[sgroups[i]] = {};
|
|
var snames = Object.keys(sgroup);
|
|
for (var j = 0; j < snames.length; j++) {
|
|
if (!t.data[sgroups[i]][snames[j]]) {
|
|
t.data[sgroups[i]][snames[j]] = sgroup[snames[j]].default || "";
|
|
}
|
|
switch (sgroup[snames[j]].type || "none") {
|
|
case "number":
|
|
t.data[sgroups[i]][snames[j]] = Number(t.data[sgroups[i]][snames[j]]);
|
|
break;
|
|
case "boolean":
|
|
t.data[sgroups[i]][snames[j]] = Boolean(t.data[sgroups[i]][snames[j]]);
|
|
break;
|
|
case "string":
|
|
t.data[sgroups[i]][snames[j]] = String(t.data[sgroups[i]][snames[j]]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (force) {
|
|
var keys = Object.keys(t.data);
|
|
for (var i = 0; i < keys.length; i++) {
|
|
let group = keys[i];
|
|
if(typeof structure[group] == "undefined"){
|
|
delete t.data[group];
|
|
continue;
|
|
}
|
|
var names = Object.keys(t.data[keys[i]]);
|
|
for (var j = 0; j < names.length; j++) {
|
|
if(typeof structure[keys[i]][names[j]] == "undefined")delete t.data[keys[i]][names[j]];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
t.save();
|
|
};
|
|
t.get = function(group, name, empty = null) {
|
|
return (t.data[group]||{})[name]||empty;
|
|
};
|
|
t.set = function(group, name, value) {
|
|
if(!t.data[group]) t.data[group] = {};
|
|
t.data[group][name] = value;
|
|
};
|
|
t.save = function() {
|
|
fs.writeFileSync(pathfile, t.toTOML(t.data));
|
|
};
|
|
t.fromTOML = function(toml) {
|
|
var out = {};
|
|
var group = "default"; //most resent group name
|
|
var data = toml.split("\n");
|
|
for (var i = 0; i < data.length; i++) {
|
|
let now = data[i].trim();
|
|
if (now.startsWith("[") && now.endsWith("]")) {
|
|
group = now.slice(1, -1);
|
|
out[group] = {};
|
|
} else if (now.includes("=")) {
|
|
let nows = now.split("=");
|
|
let name = nows.splice(0, 1)[0].trim();
|
|
let data = nows.join("=").trim();
|
|
out[group][name] = createType(data);
|
|
}
|
|
}
|
|
return out;
|
|
};
|
|
t.toTOML = function(json) {
|
|
var out = "";
|
|
var keys = Object.keys(json);
|
|
for (var i = 0; i < keys.length; i++) {
|
|
let group = keys[i];
|
|
out += `\n[${group}]\n`;
|
|
var names = Object.keys(json[keys[i]]);
|
|
for (var j = 0; j < names.length; j++) {
|
|
out += names[j] + "=" + json[keys[i]][names[j]] + "\n";
|
|
}
|
|
}
|
|
return out;
|
|
};
|
|
t.readPath = function(pathin,dir=dirname){
|
|
return new Promise((response,reject)=>{
|
|
if(pathin.startsWith("http")){
|
|
request(pathin, function(err, res, body) {
|
|
if(res.statusCode==200&&body!="error"&&!err){
|
|
response(body);
|
|
}else{
|
|
reject("Error occurred while fetching: "+pathin);
|
|
}
|
|
});
|
|
}else{
|
|
const pathf = jpath(dir, pathin);
|
|
if(!fs.existsSync(pathf)||!fs.lstatSync(pathf).isFile()) {
|
|
reject("Coud not find File: "+pathf);
|
|
}else{
|
|
try {
|
|
var content = fs.readFileSync(pathfile).toString();
|
|
response(content);
|
|
} catch (e) {
|
|
reject("Error occurred while reading: "+pathf);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
};
|
|
t.readPathes = function (...pathes){
|
|
return new Promise(async (res,rej)=>{
|
|
var out = [];
|
|
for (var i = 0; i < pathes.length; i++) {
|
|
try {
|
|
out[i] = await t.readPath(pathes[i]);
|
|
} catch (e) {
|
|
rej(e);
|
|
}
|
|
}
|
|
res(out);
|
|
});
|
|
};
|
|
|
|
function jpath(a, b) {
|
|
if (b.startsWith("/")) {
|
|
return b;
|
|
}
|
|
return path.join(a, b);
|
|
}
|
|
|
|
function createType(data) {
|
|
if (data == "true") {
|
|
return true;
|
|
}
|
|
if (data == "false") {
|
|
return false;
|
|
}
|
|
if (data.toLowerCase() == "infinity") {
|
|
return Infinity;
|
|
}
|
|
if (data.toLowerCase() == "-infinity") {
|
|
return -Infinity;
|
|
}
|
|
if (!isNaN(data)) {
|
|
return Number(data);
|
|
}
|
|
return data;
|
|
}
|
|
}
|
|
|
|
export default F;
|