Skip to content

Commit

Permalink
Updating and fixing some problems
Browse files Browse the repository at this point in the history
  • Loading branch information
koreanpanda345 committed Nov 26, 2020
1 parent c8ad767 commit e63b812
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 24 deletions.
71 changes: 67 additions & 4 deletions src/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the File System. This handles all the Log File's
* Methods
* @since 11/15/2020
* @since : 11/21/2020
*========================================================================**/
import { Config } from "./global";
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
Expand All @@ -17,12 +17,20 @@ export class FileSystem {
private _filename: string;
private _content: string;
private _config: Config;
private _type: "ERROR" | "DEBUG" | "INFO" | "WARN";
constructor(config: Config, filename: string, content: string, type: "ERROR" | "DEBUG" | "INFO" | "WARN") {
private _logType?: string;
private _type: "ERROR" | "DEBUG" | "INFO" | "WARN" | "LOG";
constructor(
config: Config,
filename: string,
content: string,
type: "ERROR" | "DEBUG" | "INFO" | "WARN" | "LOG",
logType?: string,
) {
this._filename = filename;
this._content = content;
this._config = config;
this._type = type;
this._logType = logType;
}

private checkIfPathExist() {
Expand All @@ -39,6 +47,10 @@ export class FileSystem {
case "WARN":
if (existsSync(`${this._config["log-paths"].warn}`)) return true;
return false;
case "LOG":
if (existsSync(`${this._config["custom-logs"].find((x) => x.name == this._logType)?.path}`))
return true;
return false;
default:
throw new TypeError(`${typeof this._type} is not a valid type.`);
}
Expand All @@ -59,6 +71,16 @@ export class FileSystem {
case "WARN":
if (existsSync(`${this._config["log-paths"].warn}/${this._filename}`)) return true;
return false;
case "LOG":
if (
existsSync(
`${this._config["custom-logs"].find((x) => x.name == this._logType)?.path}/${
this._filename
}`,
)
)
return true;
return false;
default:
throw new TypeError(`${typeof this._type} is not a valid type.`);
}
Expand Down Expand Up @@ -102,6 +124,22 @@ export class FileSystem {
}
if (existsSync(`${this._config["log-paths"].warn}/${this._filename}`)) return true;
return false;
case "LOG":
let _____path = "";
for (let dir of this._config["custom-logs"].find((x) => x.name == this._logType)?.path.split("/") ??
"") {
_____path += dir;
if (!existsSync(_____path)) mkdirSync(_____path);
if (
existsSync(
`${this._config["custom-logs"].find((x) => x.name == this._logType)?.path}/${
this._filename
}`,
)
)
return true;
return false;
}
default:
throw new TypeError(`${typeof this._type} is not a valid type.`);
}
Expand Down Expand Up @@ -139,6 +177,20 @@ export class FileSystem {
___data += "\n" + this._content;
writeFileSync(`${this._config["log-paths"].warn}/${this._filename}`, ___data, { flag: "w" });
break;
case "LOG":
let ____data = readFileSync(
`${this._config["custom-logs"].find((x) => x.name == this._logType)?.path}/${this._filename}`,
{
encoding: "utf-8",
},
);
____data += "\n" + this._content;
writeFileSync(
`${this._config["custom-logs"].find((x) => x.name == this._logType)?.path}/${this._filename}`,
____data,
{ flag: "w" },
);
break;
}
} else {
switch (this._type) {
Expand Down Expand Up @@ -186,6 +238,17 @@ export class FileSystem {
flag: "w",
});
break;
case "LOG":
let _____message =
new InscriberSystem().getAsciiName() +
" -------------------------------------------" +
`\n-----------------------------------------|${this._logType?.toUpperCase()} LOG FILE OF ${moment().format()}|------------------------------------------------` +
"\n -------------------------------------------" +
`\n${this._content}`;
writeFileSync(`${this._config["log-paths"].warn}/${this._filename}`, _____message, {
flag: "w",
});
break;
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/Inscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is Inscriber. Inscriber is NPM Logging Package
* that is fully customizable.
* @since : 11/15/2020
* @since : 11/21/2020
* @license
* Copyright 2020 Cody Spratford. All Rights Reserved.
*
Expand All @@ -18,6 +18,7 @@
* IMPORTS
*========================**/
import { ErrorClass, InfoClass, DebugClass, WarnClass } from "./classes/exports";
import { LogMethod } from "./classes/Log";
import { Config } from "./global";
import { InscriberConfig } from "./InscriberConfig";
/**======================
Expand Down Expand Up @@ -144,4 +145,8 @@ export class Inscriber {
require.main?.filename == undefined ? "" : require.main?.filename,
);
}

log(content: any, type: string): void {
new LogMethod(new InscriberConfig().loadConfig(), content, require.main?.filename ?? "", type);
}
}
8 changes: 5 additions & 3 deletions src/InscriberConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @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
*========================================================================**/

import { existsSync, readFileSync, writeFileSync, readdirSync } from "fs";
import { Config } from "./global";
import { InscriberSystem } from "./InscriberSystem";

export class InscriberConfig {
private _test_cases = readdirSync("./test");
private _test_cases = ["test", "test_1", "test_2", "test_3", "test_4", "test_5", "test_6"];
// SECTION DefaultConfig
/**======================================================================
* DEFAULT CONFIG
Expand Down Expand Up @@ -62,7 +63,8 @@ export class InscriberConfig {
background: "yellow",
},
},
"time-pattern": "MMMM Do YYYY, h:mm:ss a", // Time formatting.
"custom-logs": [],
"time-pattern": "MMMM DD YYYY, h:mm:ss a", // Time formatting.
};
// !SECTION
/**=======================
Expand Down
3 changes: 2 additions & 1 deletion src/InscriberSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the System file. This is not for public use.
* This is just for welcome message, and other stuff to inform the user about
* something.
* @since : 11/14/2020
*========================================================================**/
import chalk = require("chalk");

Expand Down
51 changes: 51 additions & 0 deletions src/builders/CustomLogBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**========================================================================
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/21/2020
* @description : This is a builder for custom loggers.
* @since : 11/21/2020
*========================================================================**/

import { CustomLogConfig } from "../global";

export class CustomLogBuilder {
private _config: CustomLogConfig;
constructor() {
this._config = {
name: "",
aliases: [],
path: "",
color: { text: "", background: "" },
pattern: "",
};
}

setName(name: string): CustomLogBuilder {
this._config.name = name;
return this;
}

setAliases(aliases: string[]): CustomLogBuilder {
this._config.aliases = aliases;
return this;
}

setPath(path: string): CustomLogBuilder {
this._config.path = path;
return this;
}

setPattern(pattern: string): CustomLogBuilder {
this._config.pattern = pattern;
return this;
}
setColor(color: { text: string; background: string }): CustomLogBuilder {
this._config.color = color;
return this;
}
build(): CustomLogConfig {
return this._config;
}
}
12 changes: 10 additions & 2 deletions src/builders/InscriberConfigBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the Config Builder. This Class can be used
* to make the config to be applied when creating the config file.
* @since : 11/21/2020
*========================================================================**/
/**=======================
* IMPORT
*========================**/
import { Config } from "../global";
import { Config, CustomLogConfig } from "../global";
import { InscriberConfig } from "../InscriberConfig";
import { CustomLogBuilder } from "./CustomLogBuilder";
/**=======================
* END OF IMPORTS
*========================**/
Expand Down Expand Up @@ -171,6 +173,12 @@ export class InscriberConfigBuilder {
};
return this;
}

addCustomLog(logConfig: CustomLogConfig): InscriberConfigBuilder {
this.config["custom-logs"].push(logConfig);
return this;
}

// !SECTION Log Paths
// SECTION Build
/**========================================================================
Expand Down
10 changes: 10 additions & 0 deletions src/builders/builder.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
/**========================================================================
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the builders barrel file.
* @since : 11/21/2020
*========================================================================**/
export * from "./InscriberConfigBuilder";
export * from "./CustomLogBuilder";
3 changes: 2 additions & 1 deletion src/classes/Debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the Debug Class. This handles all debug type's
* methods
* @since : 11/15/2020
*========================================================================**/

import chalk = require("chalk");
Expand Down
3 changes: 2 additions & 1 deletion src/classes/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the Error Class. This handles all the error type's
* methods.
* @since : 11/15/2020
*========================================================================**/
import chalk = require("chalk");
import moment = require("moment");
Expand Down
16 changes: 15 additions & 1 deletion src/classes/Info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
* ? ABOUT
* @author : Cody Spratford
* @email : [email protected]
* @repo :
* @repo : https://github.com/koreanpanda345/Inscriber
* @createdOn : 11/14/2020
* @description : This is the Info Class. This handles all the info type's
* methods.
* @since : 11/15/2020
*========================================================================**/

import chalk = require("chalk");
Expand All @@ -32,10 +33,23 @@ export class InfoClass implements ILog {
}
this.print(content.toString(), source, config);
}
/**==============================================
* @summary Writes to the warn logs to the log files.
* @param content - The warn log
* @param source - the source of the warn log.
* @param filepath - the file location for the log.
* @param config - The configuration of Inscriber.
*=============================================**/
write(content: string, source: string, filepath: string, config: Config): void {
let message = this.formatString(config, source, content);
new FileSystem(config, `${moment().format("YYYY_MM_D")}.log`, message, "INFO").write();
}
/**==============================================
* @summary Prints the warn log to the console.
* @param content - the warn log.
* @param source - The source of the warn log.
* @param config - the configuration of Inscriber.
*=============================================**/
print(content: string, source: string, config: Config): void {
let message = this.formatString(config, source, content);

Expand Down
Loading

0 comments on commit e63b812

Please sign in to comment.