@koreanpanda/inscriber
Version:
A Logger that can write logs, and print them, with full customization.
316 lines (315 loc) • 15.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSystem = void 0;
var fs_1 = require("fs");
var moment = require("moment");
var InscriberSystem_1 = require("./InscriberSystem");
var FileSystem = /** @class */ (function () {
function FileSystem(config, filename, content, type, logType) {
this._filename = filename;
this._content = content;
this._config = config;
this._type = type;
this._logType = logType;
}
FileSystem.prototype.checkIfPathExist = function () {
var _this = this;
var _a;
switch (this._type) {
case "ERROR":
if (fs_1.existsSync("" + this._config["log-paths"].error)) return true;
return false;
case "INFO":
if (fs_1.existsSync("" + this._config["log-paths"].info)) return true;
return false;
case "DEBUG":
if (fs_1.existsSync("" + this._config["log-paths"].debug)) return true;
return false;
case "WARN":
if (fs_1.existsSync("" + this._config["log-paths"].warn)) return true;
return false;
case "LOG":
if (
fs_1.existsSync(
"" +
((_a = this._config["custom-logs"].find(function (x) {
return x.name == _this._logType;
})) === null || _a === void 0
? void 0
: _a.path),
)
)
return true;
return false;
default:
throw new TypeError(typeof this._type + " is not a valid type.");
}
};
FileSystem.prototype.checkIfFileExist = function () {
var _this = this;
var _a, _b, _c, _d;
if (this.checkIfPathExist()) {
switch (this._type) {
case "ERROR":
if (fs_1.existsSync(this._config["log-paths"].error + "/" + this._filename)) return true;
return false;
case "INFO":
if (fs_1.existsSync(this._config["log-paths"].info + "/" + this._filename)) return true;
return false;
case "DEBUG":
if (fs_1.existsSync(this._config["log-paths"].debug + "/" + this._filename)) return true;
return false;
case "WARN":
if (fs_1.existsSync(this._config["log-paths"].warn + "/" + this._filename)) return true;
return false;
case "LOG":
if (
fs_1.existsSync(
((_a = this._config["custom-logs"].find(function (x) {
return x.name == _this._logType;
})) === null || _a === void 0
? void 0
: _a.path) +
"/" +
this._filename,
)
)
return true;
return false;
default:
throw new TypeError(typeof this._type + " is not a valid type.");
}
} else {
switch (this._type.toUpperCase()) {
case "ERROR":
var _path = "";
for (var _i = 0, _e = this._config["log-paths"].error.split("/"); _i < _e.length; _i++) {
var dir = _e[_i];
_path += dir;
if (!fs_1.existsSync(_path)) fs_1.mkdirSync(_path);
_path += "/";
}
if (fs_1.existsSync(this._config["log-paths"].error + "/" + this._filename)) return true;
return false;
case "INFO":
var __path = "";
for (var _f = 0, _g = this._config["log-paths"].info.split("/"); _f < _g.length; _f++) {
var dir = _g[_f];
__path += dir;
if (!fs_1.existsSync(__path)) fs_1.mkdirSync(__path);
__path += "/";
}
if (fs_1.existsSync(this._config["log-paths"].info + "/" + this._filename)) return true;
return false;
case "DEBUG":
var ___path = "";
for (var _h = 0, _j = this._config["log-paths"].debug.split("/"); _h < _j.length; _h++) {
var dir = _j[_h];
___path += dir;
if (!fs_1.existsSync(___path)) fs_1.mkdirSync(___path);
___path += "/";
}
if (fs_1.existsSync(this._config["log-paths"].debug + "/" + this._filename)) return true;
return false;
case "WARN":
var ____path = "";
for (var _k = 0, _l = this._config["log-paths"].warn.split("/"); _k < _l.length; _k++) {
var dir = _l[_k];
____path += dir;
if (!fs_1.existsSync(____path)) fs_1.mkdirSync(____path);
____path += "/";
}
if (fs_1.existsSync(this._config["log-paths"].warn + "/" + this._filename)) return true;
return false;
case "LOG":
var _____path = "";
for (
var _m = 0,
_o =
(_c =
(_b = this._config["custom-logs"].find(function (x) {
return x.name == _this._logType;
})) === null || _b === void 0
? void 0
: _b.path.split("/")) !== null && _c !== void 0
? _c
: "./Logs/customs";
_m < _o.length;
_m++
) {
var dir = _o[_m];
_____path += dir;
if (!fs_1.existsSync(_____path)) fs_1.mkdirSync(_____path);
_____path += "/";
}
if (
fs_1.existsSync(
((_d = this._config["custom-logs"].find(function (x) {
return x.name == _this._logType;
})) === null || _d === void 0
? void 0
: _d.path) +
"/" +
this._filename,
)
)
return true;
return false;
default:
throw new TypeError(typeof this._type + " is not a valid type.");
}
}
};
FileSystem.prototype.write = function () {
var _this = this;
var _a, _b, _c, _d;
if (this.checkIfFileExist()) {
switch (this._type) {
case "ERROR":
var data = fs_1.readFileSync(this._config["log-paths"].error + "/" + this._filename, {
encoding: "utf-8",
});
data += "\n" + this._content;
fs_1.writeFileSync(this._config["log-paths"].error + "/" + this._filename, data, { flag: "w" });
break;
case "INFO":
var _data = fs_1.readFileSync(this._config["log-paths"].info + "/" + this._filename, {
encoding: "utf-8",
});
_data += "\n" + this._content;
fs_1.writeFileSync(this._config["log-paths"].info + "/" + this._filename, _data, { flag: "w" });
break;
case "DEBUG":
var __data = fs_1.readFileSync(this._config["log-paths"].debug + "/" + this._filename, {
encoding: "utf-8",
});
__data += "\n" + this._content;
fs_1.writeFileSync(this._config["log-paths"].debug + "/" + this._filename, __data, { flag: "w" });
break;
case "WARN":
var ___data = fs_1.readFileSync(this._config["log-paths"].warn + "/" + this._filename, {
encoding: "utf-8",
});
___data += "\n" + this._content;
fs_1.writeFileSync(this._config["log-paths"].warn + "/" + this._filename, ___data, { flag: "w" });
break;
case "LOG":
var ____data = fs_1.readFileSync(
((_a = this._config["custom-logs"].find(function (x) {
return x.name == _this._logType;
})) === null || _a === void 0
? void 0
: _a.path) +
"/" +
this._filename,
{
encoding: "utf-8",
},
);
____data += "\n" + this._content;
fs_1.writeFileSync(
((_b = this._config["custom-logs"].find(function (x) {
return x.name == _this._logType;
})) === null || _b === void 0
? void 0
: _b.path) +
"/" +
this._filename,
____data,
{ flag: "w" },
);
break;
}
} else {
switch (this._type) {
case "ERROR":
var _message =
new InscriberSystem_1.InscriberSystem().getAsciiName() +
" -------------------------------------------" +
("\n-----------------------------------------|" +
this._type.toUpperCase() +
" LOG FILE OF " +
moment().format() +
"|------------------------------------------------") +
"\n -------------------------------------------" +
("\n" + this._content);
fs_1.writeFileSync(this._config["log-paths"].error + "/" + this._filename, _message, {
flag: "w",
});
break;
case "INFO":
var __message =
new InscriberSystem_1.InscriberSystem().getAsciiName() +
" -------------------------------------------" +
("\n-----------------------------------------|" +
this._type.toUpperCase() +
" LOG FILE OF " +
moment().format() +
"|------------------------------------------------") +
"\n -------------------------------------------" +
("\n" + this._content);
fs_1.writeFileSync(this._config["log-paths"].info + "/" + this._filename, __message, {
flag: "w",
});
break;
case "DEBUG":
var ___message =
new InscriberSystem_1.InscriberSystem().getAsciiName() +
" -------------------------------------------" +
("\n-----------------------------------------|" +
this._type.toUpperCase() +
" LOG FILE OF " +
moment().format() +
"|------------------------------------------------") +
"\n -------------------------------------------" +
("\n" + this._content);
fs_1.writeFileSync(this._config["log-paths"].debug + "/" + this._filename, ___message, {
flag: "w",
});
break;
case "WARN":
var ____message =
new InscriberSystem_1.InscriberSystem().getAsciiName() +
" -------------------------------------------" +
("\n-----------------------------------------|" +
this._type.toUpperCase() +
" LOG FILE OF " +
moment().format() +
"|------------------------------------------------") +
"\n -------------------------------------------" +
("\n" + this._content);
fs_1.writeFileSync(this._config["log-paths"].warn + "/" + this._filename, ____message, {
flag: "w",
});
break;
case "LOG":
var _____message =
new InscriberSystem_1.InscriberSystem().getAsciiName() +
" -------------------------------------------" +
("\n-----------------------------------------|" +
((_c = this._logType) === null || _c === void 0 ? void 0 : _c.toUpperCase()) +
" LOG FILE OF " +
moment().format() +
"|------------------------------------------------") +
"\n -------------------------------------------" +
("\n" + this._content);
fs_1.writeFileSync(
((_d = this._config["custom-logs"].find(function (x) {
return x.name === _this._logType;
})) === null || _d === void 0
? void 0
: _d.path) +
"/" +
this._filename,
_____message,
{
flag: "w",
},
);
break;
}
}
};
return FileSystem;
})();
exports.FileSystem = FileSystem;