From 3a69222ee5f05917069464c05f3e195158e0c166 Mon Sep 17 00:00:00 2001 From: greysoh Date: Tue, 7 May 2024 11:44:18 -0400 Subject: [PATCH] chore: Fix formatting. --- lom/src/libs/patchCommander.ts | 53 +++++++++++++++++++++----------- lom/src/libs/readFromKeyboard.ts | 44 +++++++++++++++++--------- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/lom/src/libs/patchCommander.ts b/lom/src/libs/patchCommander.ts index c215303..bbd7e4b 100644 --- a/lom/src/libs/patchCommander.ts +++ b/lom/src/libs/patchCommander.ts @@ -1,4 +1,4 @@ -import { Command } from "commander"; +import { Command, type ParseOptions } from "commander"; import { PrintLine } from "../commands"; export class SSHCommand extends Command { @@ -10,56 +10,73 @@ export class SSHCommand extends Command { * @param println PrintLine function to use * @param name Optional field for the name of the command */ - constructor(println: PrintLine, name?: string, doNotUseThisParamater: boolean = false) { + constructor( + println: PrintLine, + name?: string, + disableSSHHelpPatching: boolean = false, + ) { super(name); this.configureOutput({ - writeOut: (str) => println(str), - writeErr: (str) => { + writeOut: str => println(str), + writeErr: str => { if (str.includes("--help") || str.includes("-h")) return; println(str); - } + }, }); - - if (!doNotUseThisParamater) { + + if (!disableSSHHelpPatching) { const sshCommand = new SSHCommand(println, "help", true); - - sshCommand.description("Display help for command"); - sshCommand.argument("[command]", "Command to show help for"); + + sshCommand.description("display help for command"); + sshCommand.argument("[command]", "command to show help for"); sshCommand.action(() => { this.hasRecievedExitSignal = true; - println("Aborted crash\n"); + + if (process.env.NODE_ENV != "production") { + println( + "Caught irrecoverable action (command help call) in patchCommander\n", + ); + } else { + println("Aborted\n"); + } }); this.addCommand(sshCommand); } - }; + } _exit() { this.hasRecievedExitSignal = true; - }; + } _exitCallback() { this.hasRecievedExitSignal = true; - }; + } action(fn: (...args: any[]) => void | Promise): this { super.action(fn); // @ts-ignore + // prettier-ignore const oldActionHandler: (...args: any[]) => void | Promise = this._actionHandler; // @ts-ignore - this._actionHandler = async(...args: any[]): Promise => { + this._actionHandler = async (...args: any[]): Promise => { if (args[0][0] == "--help" || args[0][0] == "-h") return; await oldActionHandler(...args); }; return this; } - + + parse(argv?: readonly string[], options?: ParseOptions): this { + super.parse(["nextruntime", ...(argv ?? [])], options); + return this; + } + createCommand(name: string) { const command = new SSHCommand(this.println, name); return command; - }; -}; \ No newline at end of file + } +} diff --git a/lom/src/libs/readFromKeyboard.ts b/lom/src/libs/readFromKeyboard.ts index 861bb08..3721fb6 100644 --- a/lom/src/libs/readFromKeyboard.ts +++ b/lom/src/libs/readFromKeyboard.ts @@ -1,13 +1,16 @@ import type { ServerChannel } from "ssh2"; -export async function readFromKeyboard(stream: ServerChannel, disableEcho: boolean = false): Promise { +export async function readFromKeyboard( + stream: ServerChannel, + disableEcho: boolean = false, +): Promise { const leftEscape = "\x1B[D"; const rightEscape = "\x1B[C"; const ourBackspace = "\u0008"; // \x7F = Ascii escape code for backspace (client side) - + let line = ""; let lineIndex = 0; let isReady = false; @@ -27,7 +30,7 @@ export async function readFromKeyboard(stream: ServerChannel, disableEcho: boole if (!disableEcho) { const deltaCursor = line.length - lineIndex; - + if (deltaCursor == line.length) return setTimeout(eventLoop, 5); if (deltaCursor < 0) { @@ -38,12 +41,16 @@ export async function readFromKeyboard(stream: ServerChannel, disableEcho: boole stream.write(rightEscape.repeat(deltaCursor) + " " + ourBackspace); // Go backwards & rerender text & go backwards again (wtf?) - stream.write(leftEscape.repeat(deltaCursor + 1) + line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor + 1)); + stream.write( + leftEscape.repeat(deltaCursor + 1) + + line.substring(lineIndex - 1) + + leftEscape.repeat(deltaCursor + 1), + ); } lineIndex -= 1; } - } else if (readStreamData.includes("\x1B")) { + } else if (readStreamData.includes("\x1B")) { if (readStreamData.includes(rightEscape)) { if (lineIndex + 1 > line.length) return setTimeout(eventLoop, 5); lineIndex += 1; @@ -59,32 +66,39 @@ export async function readFromKeyboard(stream: ServerChannel, disableEcho: boole lineIndex += readStreamData.length; // There isn't a splice method for String prototypes. So, ugh: - line = line.substring(0, lineIndex - 1) + readStreamData + line.substring(lineIndex - 1); - + line = + line.substring(0, lineIndex - 1) + + readStreamData + + line.substring(lineIndex - 1); + if (!disableEcho) { let deltaCursor = line.length - lineIndex; - + // wtf? if (deltaCursor < 0) { - console.log("FIXME: somehow, our deltaCursor value is negative! please investigate me"); + console.log( + "FIXME: somehow, our deltaCursor value is negative! please investigate me", + ); deltaCursor = 0; } - - stream.write(line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor)); + + stream.write( + line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor), + ); } } setTimeout(eventLoop, 5); } - + // Yes, this is bad practice. Currently, I don't care. - return new Promise(async(resolve) => { + return new Promise(async resolve => { eventLoop(); while (!isReady) { - await new Promise((i) => setTimeout(i, 5)); + await new Promise(i => setTimeout(i, 5)); } resolve(line); }); -}; \ No newline at end of file +}