@koreanpanda/inscriber
Version:
A Logger that can write logs, and print them, with full customization.
160 lines (159 loc) • 6.11 kB
JavaScript
;
/**========================================================================
* ? ABOUT
* @author : Cody Spratford
* @email : koreanpanda345@gmail.com
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the Config file of Inscriber. This handles
* loading, creating, and adding configurations to "inscriber.config.json".
* @since : 11/21/2020
*========================================================================**/
Object.defineProperty(exports, "__esModule", { value: true });
exports.InscriberConfig = void 0;
var fs_1 = require("fs");
var InscriberSystem_1 = require("./InscriberSystem");
var InscriberConfig = /** @class */ (function () {
function InscriberConfig() {
this._test_cases = ["test", "test_1", "test_2", "test_3", "test_4", "test_5", "test_6"];
// SECTION DefaultConfig
/**======================================================================
* DEFAULT CONFIG
* - STUB Default Configurations
* - LINK src\global.ts#config
*
* DESIGN NOTES
* NOTE : one-file option might now work. I haven't worked to much on it.
* TODO Work more on the on-file option.
*======================================================================**/
this._defaultConfig = {
"overwrite-config": false,
"write-logs": false,
"one-file": false,
"log-paths": {
// The path from the root folder of where to store the files in.
// If one-file is true, then it will use info's log path.
error: this.CheckPaths("error"),
info: this.CheckPaths("info"),
debug: this.CheckPaths("debug"),
warn: this.CheckPaths("warn"),
},
"log-patterns": {
// Log Patterns.
error: "(<time>)\t[<type>]\t<message>",
info: "(<time>)\t[<type>]\t<message>",
debug: "(<time>)\t[<type>]\t<message>",
warn: "(<time>)\t[<type>]\t<message>",
},
"log-colors": {
// Log Colors.
error: {
text: "black",
background: "red",
},
info: {
text: "black",
background: "white",
},
debug: {
text: "black",
background: "green",
},
warn: {
text: "black",
background: "yellow",
},
},
"custom-logs": [],
"time-pattern": "MMMM DD YYYY, h:mm:ss a",
};
// !SECTION
/**=======================
* END OF DEFAULT CONFIG
*========================**/
// SECTION Constants
/**=======================
* CONSTANTS
*========================**/
this.config = {};
this._config = this._defaultConfig;
this._filename = "inscriber.config.json";
// !SECTION
}
/**=======================
* END OF CONSTANTS
*========================**/
// !SECTION
// SECTION Methods
/**
* @summary Checks if the root folder as a config file.
* @method CheckIfConfigExist
*/
InscriberConfig.prototype.checkIfConfigExist = function () {
var _a;
if (fs_1.existsSync(((_a = require.main) === null || _a === void 0 ? void 0 : _a.path) + "/" + this._filename))
return true;
return false;
};
InscriberConfig.prototype.getConfig = function () {
var _a;
if (!this.checkIfConfigExist()) this.createConfig();
return fs_1.readFileSync(
((_a = require.main) === null || _a === void 0 ? void 0 : _a.path) + "/" + this._filename,
{ encoding: "utf-8" },
);
};
InscriberConfig.prototype.createConfig = function () {
var _a;
var json = JSON.stringify(this._config, null, 2);
fs_1.writeFileSync(
((_a = require.main) === null || _a === void 0 ? void 0 : _a.path) + "/" + this._filename,
json,
{ flag: "w" },
);
console.log(new InscriberSystem_1.InscriberSystem().makeMessage());
};
/**
* @summary Loads the config into an object.
* @method loadConfig
* @returns {Config}
*/
InscriberConfig.prototype.loadConfig = function () {
return JSON.parse(this.getConfig());
};
InscriberConfig.prototype.CheckPaths = function (type) {
var paths = [];
if (require.main !== undefined) paths = require.main.path.split("\\");
for (var _i = 0, paths_1 = paths; _i < paths_1.length; _i++) {
var path = paths_1[_i];
if (this._test_cases.includes(path)) return "./LogTests/Logs/" + type;
}
return "./Logs/" + type;
};
InscriberConfig.prototype.UseBuilder = function (config) {
this.config = config;
if (config["overwrite-config"]) {
if (!this.checkIfConfigExist()) {
this._config = config;
this.createConfig();
} else {
this.overwriteConfig();
}
}
};
InscriberConfig.prototype.overwriteConfig = function () {
var _a;
var json = JSON.stringify(this.config, null, 2);
fs_1.writeFileSync(
(((_a = require.main) === null || _a === void 0 ? void 0 : _a.path) == undefined
? "./"
: require.main.path) + "/inscriber.config.json",
json,
{
flag: "w",
},
);
};
return InscriberConfig;
})();
exports.InscriberConfig = InscriberConfig;