-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
97 lines (82 loc) · 4.74 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SeekHub
// Copyright (C) 2022 Oscar
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import { bold, red, cyan, yellow } from "https://deno.land/[email protected]/fmt/colors.ts";
import { parse } from "https://deno.land/x/[email protected]/mod.ts"
import { config } from "./config.ts"
import { composer } from "./composer.ts";
const mainRenderer = new composer.main;
export class renderer {
static showRenderTime = false;
static renderInformer = (pageRendered: string, initialTime: number) => console.log(yellow(bold("(Renderer)")), `Rendered '${pageRendered}'. Elapsed time: ${((Date.now()) - initialTime)}ms`);
static main = class {
static clearMasterPool = () => mainRenderer.clearMasterPool();
static async render() {
const renderStartTime = Date.now();
const render = (await mainRenderer.compose()).toString();
if (renderer.showRenderTime) renderer.renderInformer("Main", renderStartTime);
return (render);
}
}
static async manage() {
const renderStartTime = Date.now();
const render = (await composer.manage()).toString();
if (renderer.showRenderTime) renderer.renderInformer("Manage", renderStartTime);
return (render);
}
static async setup() {
const renderStartTime = Date.now();
const render = (await composer.setup()).toString();
if (renderer.showRenderTime) renderer.renderInformer("Setup", renderStartTime);
return (render);
}
}
export class cli {
static showHelp = () => { console.log(bold("Usage:"), `\n${cyan(" --help (-h)")}: Show this message (If this is passed, any other argument passed won't take effect!)`, `\n${cyan(" --hostname")}: Set the hostname (Will be saved)`, `\n${cyan(" --port")}: Set the port to listen (Will be saved)`, `\n${cyan(" --publicAPI")}: ${((config.getData("publicAPI") as boolean) ? "Disable" : "Activate")} public API (Will be saved)`, `\n${cyan(" --renderTime")}: Show the amount of time elapsed rendering`, `\n${cyan(" --debug")}: Activate debug messages :o`); Deno.exit(0); }
static updateHostname = async (hostname: string) => { await config.updateConfig(["hostname", (hostname as string)]); }
static updatePort = async (port: number) => { await config.updateConfig(["port", (port as number)]); }
static togglePublicAPIAccess = async () => { await config.updateConfig(["publicAPI", ((config.getData("publicAPI") as boolean) ? false : true)]); }
static args2Parse: { name: string, flags: string[], type: BooleanConstructor | StringConstructor | NumberConstructor, stop: boolean }[] = [{ name: "help", flags: ["h"], type: Boolean, stop: true }, { name: "hostname", flags: [], type: String, stop: false }, { name: "port", flags: [], type: Number, stop: false }, { name: "publicAPI", flags: [], type: Boolean, stop: false }, { name: "renderTime", flags: [], type: Boolean, stop: false }, { name: "debug", flags: [], type: Boolean, stop: false }];
static async parse() {
const parsedArgs = parse(Deno.args, cli.args2Parse)
if (parsedArgs.help) cli.showHelp();
if (parsedArgs.hostname != undefined) await cli.updateHostname(parsedArgs.hostname);
if (parsedArgs.port != undefined) await cli.updatePort(parsedArgs.port);
if (parsedArgs.publicAPI) await cli.togglePublicAPIAccess();
if (parsedArgs.renderTime) renderer.showRenderTime = true;
}
static preParse() {
let parsedArgs;
try { parsedArgs = parse(Deno.args, cli.args2Parse) }
catch { console.log(red("Invalid argument passed!")); Deno.exit(1); }
if (parsedArgs.debug) debug.status = true;
}
}
export class debug {
static status = false;
static tell = (text: string) => { if (debug.status) console.log(yellow(bold("(Debug)")), text) }
}
export class special {
static formatNavbar(navbarText: string) {
let buff1 = "", buff2 = "";
for (let i = 0, encountered = false; i < navbarText.length; i++) {
const char = navbarText.charAt(i);
if (char == " ") { if (!encountered) { encountered = true; continue } }
if (!encountered) buff1 += char;
else buff2 += char;
}
return [buff1, buff2]
}
}