diff options
author | Sam Wilkins <samwilkins333@gmail.com> | 2020-01-07 14:33:39 -0500 |
---|---|---|
committer | Sam Wilkins <samwilkins333@gmail.com> | 2020-01-07 14:33:39 -0500 |
commit | 3db477fd1711e9e8aecf445f04b191fb558ccecd (patch) | |
tree | f999aaa90fd6afb2984a94ce9a97497797f7c852 /src | |
parent | 4b4c85d0dd299375b8743d2760218d08be968317 (diff) |
repl improvements, additional logging, removed duplicate arg pattern conflict for set
Diffstat (limited to 'src')
-rw-r--r-- | src/server/Session/session.ts | 5 | ||||
-rw-r--r-- | src/server/index.ts | 2 | ||||
-rw-r--r-- | src/server/repl.ts | 44 | ||||
-rw-r--r-- | src/server/server_Initialization.ts | 8 | ||||
-rw-r--r-- | src/typings/index.d.ts | 1 |
5 files changed, 35 insertions, 25 deletions
diff --git a/src/server/Session/session.ts b/src/server/Session/session.ts index 0c6718803..2a483fbab 100644 --- a/src/server/Session/session.ts +++ b/src/server/Session/session.ts @@ -71,6 +71,7 @@ export namespace Session { function loadAndValidateConfiguration(): Configuration { try { + console.log(timestamp(), cyan("validating configuration...")); const configuration: Configuration = JSON.parse(readFileSync('./session.config.json', 'utf8')); const options = { throwError: true, @@ -125,6 +126,7 @@ export namespace Session { * and spawns off an initial process that will respawn as predecessors die. */ export async function initializeMonitorThread(notifiers?: NotifierHooks): Promise<MasterExtensions> { + console.log(timestamp(), cyan("initializing session...")); let activeWorker: Worker; const childMessageHandlers: { [message: string]: ActionHandler } = {}; @@ -188,6 +190,7 @@ export namespace Session { }; const killSession = (graceful = true) => { + masterLog(cyan(`exiting session ${graceful ? "clean" : "immediate"}ly`)); tryKillActiveWorker(graceful); process.exit(0); }; @@ -263,7 +266,7 @@ export namespace Session { repl.registerCommand("exit", [/clean|force/], args => killSession(args[0] === "clean")); repl.registerCommand("restart", [], restartServer); repl.registerCommand("set", [letters, "port", number, boolean], args => setPort(args[0], Number(args[2]), args[3] === "true")); - repl.registerCommand("set", [/polling/, /interval/, number, boolean], args => { + repl.registerCommand("set", [/polling/, number, boolean], args => { const newPollingIntervalSeconds = Math.floor(Number(args[2])); if (newPollingIntervalSeconds < 0) { masterLog(red("the polling interval must be a non-negative integer")); diff --git a/src/server/index.ts b/src/server/index.ts index 28b2885a1..9bc3c7617 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -6,7 +6,7 @@ import { Database } from './database'; const serverPort = 4321; import { DashUploadUtils } from './DashUploadUtils'; import RouteSubscriber from './RouteSubscriber'; -import initializeServer from './server_Initialization'; +import initializeServer from './server_initialization'; import RouteManager, { Method, _success, _permission_denied, _error, _invalid, PublicHandler } from './RouteManager'; import * as qs from 'query-string'; import UtilManager from './ApiManagers/UtilManager'; diff --git a/src/server/repl.ts b/src/server/repl.ts index bd00e48cd..faf1eab15 100644 --- a/src/server/repl.ts +++ b/src/server/repl.ts @@ -3,7 +3,7 @@ import { red, green, white } from "colors"; export interface Configuration { identifier: () => string | string; - onInvalid?: (culprit?: string) => string | string; + onInvalid?: (command: string, validCommand: boolean) => string | string; onValid?: (success?: string) => string | string; isCaseSensitive?: boolean; } @@ -16,8 +16,8 @@ export interface Registration { export default class Repl { private identifier: () => string | string; - private onInvalid: (culprit?: string) => string | string; - private onValid: (success: string) => string | string; + private onInvalid: ((command: string, validCommand: boolean) => string) | string; + private onValid: ((success: string) => string) | string; private isCaseSensitive: boolean; private commandMap = new Map<string, Registration[]>(); public interface: Interface; @@ -34,18 +34,24 @@ export default class Repl { private resolvedIdentifier = () => typeof this.identifier === "string" ? this.identifier : this.identifier(); - private usage = () => { - const resolved = this.keys; - if (resolved) { - return resolved; - } - const members: string[] = []; - const keys = this.commandMap.keys(); - let next: IteratorResult<string>; - while (!(next = keys.next()).done) { - members.push(next.value); + private usage = (command: string, validCommand: boolean) => { + if (validCommand) { + const formatted = white(command); + const patterns = green(this.commandMap.get(command)!.map(({ argPatterns }) => `${formatted} ${argPatterns.join(" ")}`).join('\n')); + return `${this.resolvedIdentifier()}\nthe given arguments do not match any registered patterns for ${formatted}\nthe list of valid argument patterns is given by:\n${patterns}`; + } else { + const resolved = this.keys; + if (resolved) { + return resolved; + } + const members: string[] = []; + const keys = this.commandMap.keys(); + let next: IteratorResult<string>; + while (!(next = keys.next()).done) { + members.push(next.value); + } + return `${this.resolvedIdentifier()} commands: { ${members.sort().join(", ")} }`; } - return `${this.resolvedIdentifier()} commands: { ${members.sort().join(", ")} }`; } private success = (command: string) => `${this.resolvedIdentifier()} completed execution of ${white(command)}`; @@ -61,8 +67,8 @@ export default class Repl { } } - private invalid = (culprit?: string) => { - console.log(red(typeof this.onInvalid === "string" ? this.onInvalid : this.onInvalid(culprit))); + private invalid = (command: string, validCommand: boolean) => { + console.log(red(typeof this.onInvalid === "string" ? this.onInvalid : this.onInvalid(command, validCommand))); this.busy = false; } @@ -83,7 +89,7 @@ export default class Repl { } const [command, ...args] = line.split(/\s+/g); if (!command) { - return this.invalid(); + return this.invalid(command, false); } const registered = this.commandMap.get(command); if (registered) { @@ -108,8 +114,10 @@ export default class Repl { return; } } + this.invalid(command, true); + } else { + this.invalid(command, false); } - this.invalid(command); } }
\ No newline at end of file diff --git a/src/server/server_Initialization.ts b/src/server/server_Initialization.ts index 0f502e8fb..cbe070293 100644 --- a/src/server/server_Initialization.ts +++ b/src/server/server_Initialization.ts @@ -22,7 +22,7 @@ import { publicDirectory } from '.'; import { logPort, } from './ActionUtilities'; import { timeMap } from './ApiManagers/UserManager'; import { blue, yellow } from 'colors'; -var cors = require('cors'); +import * as cors from "cors"; /* RouteSetter is a wrapper around the server that prevents the server from being exposed. */ @@ -33,13 +33,11 @@ export default async function InitializeServer(routeSetter: RouteSetter) { const app = buildWithMiddleware(express()); app.use(express.static(publicDirectory, { - setHeaders: (res, path) => { - res.setHeader("Access-Control-Allow-Origin", "*"); - } + setHeaders: res => res.setHeader("Access-Control-Allow-Origin", "*") })); app.use("/images", express.static(publicDirectory)); const corsOptions = { - origin: function (origin: any, callback: any) { + origin: function (_origin: any, callback: any) { callback(null, true); } }; diff --git a/src/typings/index.d.ts b/src/typings/index.d.ts index 887f96734..cc68e8a4d 100644 --- a/src/typings/index.d.ts +++ b/src/typings/index.d.ts @@ -4,6 +4,7 @@ declare module 'googlephotos'; declare module 'react-image-lightbox-with-rotate'; declare module 'kill-port'; declare module 'ipc-event-emitter'; +declare module 'cors'; declare module '@react-pdf/renderer' { import * as React from 'react'; |