diff --git a/bin/CommandBuilder.js b/bin/CommandBuilder.js index 1740393..52709ff 100644 --- a/bin/CommandBuilder.js +++ b/bin/CommandBuilder.js @@ -7,6 +7,7 @@ class CommandBuilder { constructor(name) { this.name = name; this.args = []; + this.options = []; this.description = ""; this.callback = () => {}; } @@ -21,6 +22,21 @@ class CommandBuilder { return this; } + addArgument(arg) { + this.args.push(arg); + return this; + } + + addOption(opt) { + this.options.push(opt); + return this; + } + + setOptions(opts) { + this.options = opts; + return this; + } + setDescription(description) { this.description = description; return this; @@ -36,6 +52,7 @@ class CommandBuilder { name: this.name, args: this.args, description: this.description, + options: this.options, callback: this.callback }; } diff --git a/bin/CommandsHandler.js b/bin/CommandsHandler.js index 1fb7a10..e3fe348 100644 --- a/bin/CommandsHandler.js +++ b/bin/CommandsHandler.js @@ -2,7 +2,7 @@ const { Collection } = require("discord.js"); function RegisterCommand(program, data) { - const { name, args, description, callback } = data; + const { name, args, description, options, callback } = data; if (!description) { @@ -22,6 +22,12 @@ function RegisterCommand(program, data) { l.argument(arg); } } + + if (options && options.length) { + for (const option of options) { + l.option(option); + } + } } function RegisterJSONCommand(program, builder) { diff --git a/bin/commands/InitCommand.js b/bin/commands/InitCommand.js index 6e67dab..b5c3fd7 100644 --- a/bin/commands/InitCommand.js +++ b/bin/commands/InitCommand.js @@ -3,7 +3,7 @@ const Nanospinner = require("nanospinner"); const spinner = Nanospinner.createSpinner(); const InitializationTools = require('../../globals/InitializationUtils'); -const maxSteps = 4; +const maxSteps = 5; let currentStep = 1; function startStep(text) { @@ -51,6 +51,10 @@ module.exports = () => CommandBuilder.createBuilder("init") InitializationTools.createConfigurationFiles(); + step("Installing Required dependencies") + + //InitializationTools.installRequired(); + step("Done! Created All Files"); }); \ No newline at end of file diff --git a/bin/commands/RegisterCommands.js b/bin/commands/RegisterCommands.js index 7db0192..12e474d 100644 --- a/bin/commands/RegisterCommands.js +++ b/bin/commands/RegisterCommands.js @@ -1,8 +1,12 @@ const { RegisterCommand, RegisterJSONCommand } = require("../CommandsHandler"); const InitCommand = require("./InitCommand"); -const VersionCommand = require("./VersionCommand") +const RunCommand = require("./RunCommand"); +const VersionCommand = require("./VersionCommand"); +const WatchCommand = require("./WatchCommand"); module.exports = (program) => { RegisterJSONCommand(program, VersionCommand()); RegisterJSONCommand(program, InitCommand()); + RegisterJSONCommand(program, RunCommand()); + RegisterJSONCommand(program, WatchCommand()); } \ No newline at end of file diff --git a/bin/commands/RunCommand.js b/bin/commands/RunCommand.js new file mode 100644 index 0000000..21c6334 --- /dev/null +++ b/bin/commands/RunCommand.js @@ -0,0 +1,40 @@ +const path = require("path"); +const { loadCurrentRedactConfig } = require("../../globals/ConfigurationTools"); +const CommandBuilder = require("../CommandBuilder"); +const figlet = require("figlet"); +const chalk = require("cli-color"); +const packageJSON = require("../../package.json"); +const { Logger } = require("../.."); + +let text = ` + + d8888b. d88888b d8888b. .d8b. .o88b. d888888b .o88b. .d88b. d8888b. d8888b. + 88 \`8D 88' 88 \`8D d8' \`8b d8P Y8 \`oo88oo' d8P Y8 .8P Y8. 88 \`8D 88 \`8D + 88oobY' 88ooooo 88 88 88ooo88 8P 88 8P 88 88 88oobY' 88 88 + 88\`8b 88ooooo 88 88 88ooo88 8b 88 8b 88 88 88\`8b 88 88 + 88 \`88. 88. 88 .8D 88 88 Y8b d8 88 Y8b d8 \`8b d8' 88 \`88. 88 .8D + 88 YD Y88888P Y8888D' YP YP \`Y88P' YP \`Y88P' \`Y88P' 88 YD Y8888D\' v${packageJSON.version} +` + +module.exports = () => { + return CommandBuilder.createBuilder("run") + .setDescription("Runs the current project") + .setCallback(() => { + + const config = loadCurrentRedactConfig(); + const main = config["main"]; + + const a = async () => { + console.log(`${chalk.redBright(text)}`); + } + + + a().then(() => { + if (!main || !main.length) + return Logger.getLogger().error("Cannot find main field or main field is empty. Please provide a main path to a javascript file.") + const p = path.join(process.cwd(), main); + require(p); + }) + + }); +} \ No newline at end of file diff --git a/bin/commands/WatchCommand.js b/bin/commands/WatchCommand.js new file mode 100644 index 0000000..ff08046 --- /dev/null +++ b/bin/commands/WatchCommand.js @@ -0,0 +1,26 @@ +const fs = require("fs"); +const watch = require("watch"); +const _eval = require("eval"); +const CommandBuilder = require("../CommandBuilder"); +const path = require("path"); + +function HandleWatcher() { + watch.watchTree(process.cwd(), { + filter: (path, stat) => { + console.log(path); + return stat.isFile() && (path.endsWith(".js") || path.endsWith(".ts")) + } + }, (f, curr, prev) => { + const data = fs.readdirSync(curr, "utf-8"); + _eval(data); + }); +} + +module.exports = () => { + return CommandBuilder.createBuilder("watch") + .setDescription("Watches all files in the main directory") + .setCallback((options) => { + console.log(`[${new Date().toUTCString()}]: Started Watching files in the current directory.`); + HandleWatcher(); + }); +} \ No newline at end of file diff --git a/globals/ConfigurationTools.js b/globals/ConfigurationTools.js new file mode 100644 index 0000000..6ad5142 --- /dev/null +++ b/globals/ConfigurationTools.js @@ -0,0 +1,11 @@ +const { readFileSync } = require("fs"); +const path = require("path"); +const { configName } = require("./InitializationUtils"); + +function loadCurrentRedactConfig() { + return JSON.parse(readFileSync(path.join(process.cwd(), configName), "utf-8")); +} + +module.exports = { + loadCurrentRedactConfig +}; \ No newline at end of file diff --git a/globals/InitializationUtils.js b/globals/InitializationUtils.js index a334968..a1b26f5 100644 --- a/globals/InitializationUtils.js +++ b/globals/InitializationUtils.js @@ -1,7 +1,8 @@ +const { execSync } = require("child_process"); const { writeFileSync, mkdirSync } = require("fs"); const path = require("path"); -const configName = "redact.config.json"; +const configName = "redactcord.config.json"; const environmentName = ".redact.env"; const files = [ environmentName, @@ -12,6 +13,9 @@ const directories = [ "commands", "events", ]; +const requiredDependencies = [ + "redactcord", +] const packageConfig = { name: path.basename(path.dirname(process.cwd())), @@ -43,7 +47,7 @@ function toJson(data) { function createConfigurationFiles() { writeFileSync(path.join(process.cwd(), "package.json"), toJson(packageConfig)); - writeFileSync(path.join(process.cwd(), "redactcord.config.json"), toJson(redactconfig)); + writeFileSync(path.join(process.cwd(), configName), toJson(redactconfig)); } function writeFiles() { @@ -58,9 +62,30 @@ function makeDirectories() { } } +function executeNpm(command) { + execSync("npm " + command); +} + +function install(dependency) { + executeNpm("install " + dependency); +} + +function installMultiple(...dependencies) { + executeNpm("install " + dependencies.join(" ")); +} + +function installRequired() { + installMultiple(...requiredDependencies); +} + module.exports = { createConfigurationFiles, writeFiles, toJson, - makeDirectories + makeDirectories, + configName, + install, + executeNpm, + installMultiple, + installRequired } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4a8a7de..3f909e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,10 +10,12 @@ "license": "MIT", "dependencies": { "better-sqlite3": "^9.0.0", - "chalk": "^5.3.0", + "cli-color": "^2.0.3", "commander": "^11.1.0", "discord.js": "^14.13.0", "dotenv": "^16.3.1", + "eval": "^0.1.8", + "figlet": "^1.6.0", "inquirer": "^9.2.11", "lodash": "^4.17.21", "nanospinner": "^1.1.0", @@ -21,12 +23,14 @@ "quick.db": "^9.1.7", "quickmongo": "^5.2.0", "quickpostgres": "^3.0.2", - "typescript": "^5.2.2" + "typescript": "^5.2.2", + "watch": "^1.0.2" }, "bin": { "redactcord": "bin/index.js" }, "devDependencies": { + "@types/cli-color": "^2.0.3", "@types/inquirer": "^9.0.4", "@types/lodash": "^4.14.199", "@types/node": "^20.8.6" @@ -1341,6 +1345,12 @@ "node": ">=14.0.0" } }, + "node_modules/@types/cli-color": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@types/cli-color/-/cli-color-2.0.3.tgz", + "integrity": "sha512-JcGK/IFNVt5a99Xsz4wMCK8jfylpZ5E9AxzGcTVLD5nNYPYxXkylTRV7mcqA324rKLfqT3juU5KjQbI9CAV3SA==", + "dev": true + }, "node_modules/@types/inquirer": { "version": "9.0.4", "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-9.0.4.tgz", @@ -1580,6 +1590,21 @@ "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==" }, + "node_modules/cli-color": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/cli-color/-/cli-color-2.0.3.tgz", + "integrity": "sha512-OkoZnxyC4ERN3zLzZaY9Emb7f/MhBOIpePv0Ycok0fJYT+Ouo00UBEIwsVsr0yoow++n5YWlSUgST9GKhNHiRQ==", + "dependencies": { + "d": "^1.0.1", + "es5-ext": "^0.10.61", + "es6-iterator": "^2.0.3", + "memoizee": "^0.4.15", + "timers-ext": "^0.1.7" + }, + "engines": { + "node": ">=0.10" + } + }, "node_modules/cli-cursor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", @@ -1642,6 +1667,15 @@ "node": ">=16" } }, + "node_modules/d": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz", + "integrity": "sha512-m62ShEObQ39CfralilEQRjH6oAMtNCV1xJyEx5LpRYUVN+EviphDgUc/F3hnYbADmkiNs67Y+3ylmlG7Lnu+FA==", + "dependencies": { + "es5-ext": "^0.10.50", + "type": "^1.0.1" + } + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -1757,6 +1791,50 @@ "once": "^1.4.0" } }, + "node_modules/es5-ext": { + "version": "0.10.62", + "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.62.tgz", + "integrity": "sha512-BHLqn0klhEpnOKSrzn/Xsz2UIW8j+cGmo9JLzr8BiUapV8hPL9+FliFqjwr9ngW7jWdnxv6eO+/LqyhJVqgrjA==", + "hasInstallScript": true, + "dependencies": { + "es6-iterator": "^2.0.3", + "es6-symbol": "^3.1.3", + "next-tick": "^1.1.0" + }, + "engines": { + "node": ">=0.10" + } + }, + "node_modules/es6-iterator": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", + "integrity": "sha512-zw4SRzoUkd+cl+ZoE15A9o1oQd920Bb0iOJMQkQhl3jNc03YqVjAhG7scf9C5KWRU/R13Orf588uCC6525o02g==", + "dependencies": { + "d": "1", + "es5-ext": "^0.10.35", + "es6-symbol": "^3.1.1" + } + }, + "node_modules/es6-symbol": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.3.tgz", + "integrity": "sha512-NJ6Yn3FuDinBaBRWl/q5X/s4koRHBrgKAu+yGI6JCBeiu3qrcbJhwT2GeR/EXVfylRk8dpQVJoLEFhK+Mu31NA==", + "dependencies": { + "d": "^1.0.1", + "ext": "^1.1.2" + } + }, + "node_modules/es6-weak-map": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-weak-map/-/es6-weak-map-2.0.3.tgz", + "integrity": "sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA==", + "dependencies": { + "d": "1", + "es5-ext": "^0.10.46", + "es6-iterator": "^2.0.3", + "es6-symbol": "^3.1.1" + } + }, "node_modules/escape-string-regexp": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz", @@ -1768,6 +1846,35 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/eval": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/eval/-/eval-0.1.8.tgz", + "integrity": "sha512-EzV94NYKoO09GLXGjXj9JIlXijVck4ONSr5wiCWDvhsvj5jxSrzTmRU/9C1DyB6uToszLs8aifA6NQ7lEQdvFw==", + "dependencies": { + "@types/node": "*", + "require-like": ">= 0.1.1" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/event-emitter": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", + "integrity": "sha512-D9rRn9y7kLPnJ+hMq7S/nhvoKwwvVJahBi2BPmx3bvbsEdK3W9ii8cBSGjP+72/LnM4n6fo3+dkCX5FeTQruXA==", + "dependencies": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, + "node_modules/exec-sh": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/exec-sh/-/exec-sh-0.2.2.tgz", + "integrity": "sha512-FIUCJz1RbuS0FKTdaAafAByGS0CPvU3R0MeHxgtl+djzCc//F8HakL8GzmVNZanasTbTAY/3DRFA0KpVqj/eAw==", + "dependencies": { + "merge": "^1.2.0" + } + }, "node_modules/expand-template": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", @@ -1776,6 +1883,19 @@ "node": ">=6" } }, + "node_modules/ext": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/ext/-/ext-1.7.0.tgz", + "integrity": "sha512-6hxeJYaL110a9b5TEJSj0gojyHQAmA2ch5Os+ySCiA1QGdS697XWY1pzsrSjqA9LDEEgdB/KypIlR59RcLuHYw==", + "dependencies": { + "type": "^2.7.2" + } + }, + "node_modules/ext/node_modules/type": { + "version": "2.7.2", + "resolved": "https://registry.npmjs.org/type/-/type-2.7.2.tgz", + "integrity": "sha512-dzlvlNlt6AXU7EBSfpAscydQ7gXB+pPGsPnfJnZpiNJBDj7IaJzQlBZYGdEi4R9HmPdBv2XmWJ6YUtoTa7lmCw==" + }, "node_modules/external-editor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz", @@ -1816,6 +1936,17 @@ "fxparser": "src/cli/cli.js" } }, + "node_modules/figlet": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/figlet/-/figlet-1.6.0.tgz", + "integrity": "sha512-31EQGhCEITv6+hi2ORRPyn3bulaV9Fl4xOdR169cBzH/n1UqcxsiSB/noo6SJdD7Kfb1Ljit+IgR1USvF/XbdA==", + "bin": { + "figlet": "bin/index.js" + }, + "engines": { + "node": ">= 0.4.0" + } + }, "node_modules/figures": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/figures/-/figures-5.0.0.tgz", @@ -1992,6 +2123,11 @@ "node": ">=8" } }, + "node_modules/is-promise": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.2.tgz", + "integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==" + }, "node_modules/is-unicode-supported": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz", @@ -2073,17 +2209,45 @@ "node": ">=10" } }, + "node_modules/lru-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/lru-queue/-/lru-queue-0.1.0.tgz", + "integrity": "sha512-BpdYkt9EvGl8OfWHDQPISVpcl5xZthb+XPsbELj5AQXxIC8IriDZIQYjBJPEm5rS420sjZ0TLEzRcq5KdBhYrQ==", + "dependencies": { + "es5-ext": "~0.10.2" + } + }, "node_modules/magic-bytes.js": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.5.0.tgz", "integrity": "sha512-wJkXvutRbNWcc37tt5j1HyOK1nosspdh3dj6LUYYAvF6JYNqs53IfRvK9oEpcwiDA1NdoIi64yAMfdivPeVAyw==" }, + "node_modules/memoizee": { + "version": "0.4.15", + "resolved": "https://registry.npmjs.org/memoizee/-/memoizee-0.4.15.tgz", + "integrity": "sha512-UBWmJpLZd5STPm7PMUlOw/TSy972M+z8gcyQ5veOnSDRREz/0bmpyTfKt3/51DhEBqCZQn1udM/5flcSPYhkdQ==", + "dependencies": { + "d": "^1.0.1", + "es5-ext": "^0.10.53", + "es6-weak-map": "^2.0.3", + "event-emitter": "^0.3.5", + "is-promise": "^2.2.2", + "lru-queue": "^0.1.0", + "next-tick": "^1.1.0", + "timers-ext": "^0.1.7" + } + }, "node_modules/memory-pager": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", "optional": true }, + "node_modules/merge": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/merge/-/merge-1.2.1.tgz", + "integrity": "sha512-VjFo4P5Whtj4vsLzsYBu5ayHhoHJ0UqNm7ibvShmbmoz7tGi0vXaoJbGdB+GmDMLUdg8DpQXEIeVDAe8MaABvQ==" + }, "node_modules/mimic-fn": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", @@ -2208,6 +2372,11 @@ "resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-1.0.2.tgz", "integrity": "sha512-ONmRUqK7zj7DWX0D9ADe03wbwOBZxNAfF20PlGfCWQcD3+/MakShIHrMqx9YwPTfxDdF1zLeL+RGZiR9kGMLdg==" }, + "node_modules/next-tick": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.1.0.tgz", + "integrity": "sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==" + }, "node_modules/node-abi": { "version": "3.50.0", "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.50.0.tgz", @@ -2529,6 +2698,14 @@ "node": ">= 6" } }, + "node_modules/require-like": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/require-like/-/require-like-0.1.2.tgz", + "integrity": "sha512-oyrU88skkMtDdauHDuKVrgR+zuItqr6/c//FXzvmxRGMexSDc6hNvJInGW3LL46n+8b50RykrvwSUIIQH2LQ5A==", + "engines": { + "node": "*" + } + }, "node_modules/restore-cursor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", @@ -2778,6 +2955,15 @@ "node": ">=6" } }, + "node_modules/timers-ext": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/timers-ext/-/timers-ext-0.1.7.tgz", + "integrity": "sha512-b85NUNzTSdodShTIbky6ZF02e8STtVVfD+fu4aXXShEELpozH+bCpJLYMPZbsABN2wDH7fJpqIoXxJpzbf0NqQ==", + "dependencies": { + "es5-ext": "~0.10.46", + "next-tick": "1" + } + }, "node_modules/tiny-typed-emitter": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/tiny-typed-emitter/-/tiny-typed-emitter-2.1.0.tgz", @@ -2826,6 +3012,11 @@ "node": "*" } }, + "node_modules/type": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz", + "integrity": "sha512-+5nt5AAniqsCnu2cEQQdpzCAh33kVx8n0VoFidKpB1dVVLAN/F+bgVOqOJqOnEnrhp222clB5p3vUlD+1QAnfg==" + }, "node_modules/type-fest": { "version": "0.21.3", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.21.3.tgz", @@ -2879,6 +3070,21 @@ "uuid": "dist/bin/uuid" } }, + "node_modules/watch": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/watch/-/watch-1.0.2.tgz", + "integrity": "sha512-1u+Z5n9Jc1E2c7qDO8SinPoZuHj7FgbgU1olSFoyaklduDvvtX7GMMtlE6OC9FTXq4KvNAOfj6Zu4vI1e9bAKA==", + "dependencies": { + "exec-sh": "^0.2.0", + "minimist": "^1.2.0" + }, + "bin": { + "watch": "cli.js" + }, + "engines": { + "node": ">=0.1.95" + } + }, "node_modules/wcwidth": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz", diff --git a/package.json b/package.json index 4e6ff2f..0709c68 100644 --- a/package.json +++ b/package.json @@ -16,16 +16,19 @@ "author": "Redact Team", "license": "MIT", "devDependencies": { + "@types/cli-color": "^2.0.3", "@types/inquirer": "^9.0.4", "@types/lodash": "^4.14.199", "@types/node": "^20.8.6" }, "dependencies": { "better-sqlite3": "^9.0.0", - "chalk": "^5.3.0", + "cli-color": "^2.0.3", "commander": "^11.1.0", "discord.js": "^14.13.0", "dotenv": "^16.3.1", + "eval": "^0.1.8", + "figlet": "^1.6.0", "inquirer": "^9.2.11", "lodash": "^4.17.21", "nanospinner": "^1.1.0", @@ -33,6 +36,7 @@ "quick.db": "^9.1.7", "quickmongo": "^5.2.0", "quickpostgres": "^3.0.2", - "typescript": "^5.2.2" + "typescript": "^5.2.2", + "watch": "^1.0.2" } } diff --git a/src/client/RedactClient.ts b/src/client/RedactClient.ts index 9f6dbce..4566133 100644 --- a/src/client/RedactClient.ts +++ b/src/client/RedactClient.ts @@ -5,6 +5,8 @@ import { RedactError } from "../error/RedactError"; import { CommandsManager } from "./commands/CommandsManager"; import { EventManager } from "./events/EventManager"; import path from "path"; +import { Logger } from "../logger/Logger"; +import Color from "cli-color"; type RedactOptions = ClientOptions & { token: string; @@ -22,6 +24,7 @@ export class RedactClient { private commandsManager: CommandsManager; private eventManager: EventManager; private readyEvent?: () => void; + private logger: Logger = Logger.getLogger(); constructor(options: RedactOptions) { this.client = new Client(options); @@ -133,9 +136,12 @@ export class RedactClient { this.client.on("ready", () => { this.endSpinner({ - text: "Logged in as: " + this.getBotUsername() + text: "API Connected!" }, true); + this.logger.info(`Connected to ${Color.greenBright(this.getBotUsername())} at ${Color.greenBright(new Date().toUTCString())}`); + this.logger.info(` └── Amount of servers ${Color.yellow(this.getClient().guilds.cache.size)}`); + if (this.readyEvent) this.readyEvent(); }); @@ -158,11 +164,14 @@ export class RedactClient { try { await this.client.login(token); } catch (err) { - console.log(err); - const itry = this.incrementLoginTry(); if (itry >= this.maxLoginTry) + { + this.spinner.error({ + text: `Trying to login into client... (${this.loginTry}/${this.maxLoginTry})`, + }) throw new RedactError("Maximum Tries Exceeded", "The login tries has hit the maximum limit which is " + this.maxLoginTry); + } await this.login(); } return true; diff --git a/src/environment/Environment.ts b/src/environment/Environment.ts index 7eff4cf..a30a1a8 100644 --- a/src/environment/Environment.ts +++ b/src/environment/Environment.ts @@ -3,13 +3,14 @@ import dotenv from "dotenv"; import path from "path"; import { RedactConfig } from "../configuration/RedactConfig"; import { RedactError } from "../error/RedactError"; +import { readFileSync } from "fs"; export class Environment extends Collection { private content: dotenv.DotenvParseOutput; constructor() { super(); - this.content = dotenv.parse(path.join(process.cwd(), ".redact.env")); + this.content = dotenv.parse(readFileSync(path.join(process.cwd(), ".redact.env"), "utf-8")); RedactConfig.checkRedactConfig(); const redactConfig = new RedactConfig(); const allowEnvironmentLoading: boolean = diff --git a/src/index.ts b/src/index.ts index d57064e..f927d30 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import { Configuration } from "./configuration/Configuration"; import { RedactConfig } from "./configuration/RedactConfig"; import { Environment } from "./environment/Environment"; import { RedactError } from "./error/RedactError"; +import { Logger } from "./logger/Logger"; import { Loader } from "./utils/Loader"; export { @@ -28,4 +29,5 @@ export { Environment, RedactError, Loader, + Logger }; \ No newline at end of file diff --git a/src/logger/Logger.ts b/src/logger/Logger.ts new file mode 100644 index 0000000..f938076 --- /dev/null +++ b/src/logger/Logger.ts @@ -0,0 +1,58 @@ +import Color from "cli-color"; + +export class Logger { + + public static getLogger(): Logger { + return new Logger(); + } + + constructor() {} + + public log(level: "INFO" | "WARN" | "ERROR" | "DEBUG", text: string): void { + + switch (level) { + case "INFO": + { + this.info(text); + break; + } + case "WARN": + { + this.warn(text); + break; + } + case "DEBUG": + { + this.debug(text); + break; + } + case "ERROR": + { + this.error(text); + break; + } + } + } + + private background(text: string): string { + return Color.bgWhiteBright(text); + } + + public info(text: string): void { + let tformat = `${this.background(Color.greenBright(`INFO >`))} ${text}`; + console.log(tformat); + } + public warn(text: string): void { + let tformat = `${this.background(Color.yellowBright(`INFO >`))} ${text}`; + console.log(tformat); + } + public error(text: string): void { + let tformat = `${this.background(Color.redBright(`INFO >`))} ${text}`; + console.log(tformat); + } + public debug(text: string): void { + let tformat = `${this.background(Color.magentaBright(`INFO >`))} ${text}`; + console.log(tformat); + } + +} \ No newline at end of file diff --git a/test/.redact.env b/test/.redact.env index e69de29..0672933 100644 --- a/test/.redact.env +++ b/test/.redact.env @@ -0,0 +1 @@ +token=MTE1NTgwMzg1MzUxODgwMzAzNQ.GiZJ4P.hEbpqu848YcloODTiKr0NZSQDARCPkF6bfwED0 \ No newline at end of file diff --git a/test/package-lock.json b/test/package-lock.json new file mode 100644 index 0000000..21a9d09 --- /dev/null +++ b/test/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "DiscordJBase", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "DiscordJBase", + "version": "1.0.0", + "license": "MIT" + } + } +} diff --git a/test/redactcord.config.json b/test/redactcord.config.json index 505238e..413149f 100644 --- a/test/redactcord.config.json +++ b/test/redactcord.config.json @@ -1,6 +1,6 @@ { "$schema": "https://cdn.redact.tools/libs/redactcord/RedactcordConfig.schema.json", - "main": "src/index.js", + "main": "index.js", "suspendCommandsLoading": false, "suspendEventsLoading": false, "eventsFolderPath": "events", diff --git a/typings/index.d.ts b/typings/index.d.ts index 3853502..69f1a09 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -149,4 +149,18 @@ export abstract class Event { public abstract onEvent(...args: any[]): void; public takeFirst(...args: V[]): V | undefined; +} + +export class Logger { + + public static getLogger(): Logger; + + constructor(); + + public log(level: "INFO" | "WARN" | "ERROR" | "DEBUG", text: string): void; + public info(text: string): void; + public warn(text: string): void; + public error(text: string): void; + public debug(text: string): void; + } \ No newline at end of file