From 0cd7a2d307730f5785eef2d02f6dfc756bb5a884 Mon Sep 17 00:00:00 2001 From: ewsgit Date: Sun, 21 Jun 2026 01:24:27 +0000 Subject: [PATCH] complete the non GUI terminal system --- instance/src/log.ts | 2 +- instance/src/systems/commandBackup/help.ts | 30 --- instance/src/systems/consoleCommands.ts | 21 ++- .../systems/consoleCommands/application.ts | 29 +++ instance/src/systems/terminal.ts | 31 ++- instance/src/systems/trpc.ts | 178 +++++++++--------- 6 files changed, 164 insertions(+), 127 deletions(-) delete mode 100644 instance/src/systems/commandBackup/help.ts create mode 100644 instance/src/systems/consoleCommands/application.ts diff --git a/instance/src/log.ts b/instance/src/log.ts index 1eb2a9f..aeb87ec 100644 --- a/instance/src/log.ts +++ b/instance/src/log.ts @@ -1,5 +1,5 @@ -import type { Instance } from "./index.ts"; import util from "node:util"; +import type { Instance } from "./index.ts"; export enum LogType { INFO, diff --git a/instance/src/systems/commandBackup/help.ts b/instance/src/systems/commandBackup/help.ts deleted file mode 100644 index 2aea5ef..0000000 --- a/instance/src/systems/commandBackup/help.ts +++ /dev/null @@ -1,30 +0,0 @@ -import Command, { type ICommandRuntimeParameters } from "../command.ts"; -import chalk from "chalk"; - -export default class HelpCommand extends Command { - override commandId = "help"; - flags = {}; - aliases = []; - override shortDescription = "Display this help information"; - - async run(parameters: ICommandRuntimeParameters) { - const HELP_BANNER_TITLE = " Workspaces Help "; - const LOG_WIDTH = process.stdout.getWindowSize()[0] - (this.instance.log.META_LENGTH + 8); - - this.instance.log.system.info( - "-".repeat((LOG_WIDTH - HELP_BANNER_TITLE.length) / 2) + HELP_BANNER_TITLE + "-".repeat((LOG_WIDTH - HELP_BANNER_TITLE.length) / 2), - ); - - for (const command of this.instance.sys.consoleCommands.commands) { - this.instance.log.system.info( - `${this.instance.log.system.emphasis(command.commandId)}${command.aliases.length > 0 ? ", " : ""}${command.aliases.join(", ")} - ${command.shortDescription}`, - ); - if (Object.keys(command.flags).length > 0) { - this.instance.log.system.info(` ${chalk.bold("flags")}:`); - this.instance.log.system.info(` ${JSON.stringify(command.flags)}`); - } - } - - return this.finishRun(); - } -} diff --git a/instance/src/systems/consoleCommands.ts b/instance/src/systems/consoleCommands.ts index 9ae981e..ea4d939 100644 --- a/instance/src/systems/consoleCommands.ts +++ b/instance/src/systems/consoleCommands.ts @@ -19,17 +19,30 @@ export default class ConsoleCommandsSystem extends System { const commands = await fs.readdir(path.join(this.instance.sys.filesystem.SRC_ROOT, "/systems/consoleCommands/")); for (const cmd of commands) { - const importedCommand = await import(`file://${path.join(this.instance.sys.filesystem.SRC_ROOT, "/systems/consoleCommands/", cmd)}`); + const importedCommand = (await import(`file://${path.join(this.instance.sys.filesystem.SRC_ROOT, "/systems/consoleCommands/", cmd)}`)).default; this.commands.push(importedCommand); - this.log.info(`Registered command ${cmd}`); + this.log.info(`Registered command ${this.log.emphasis(importedCommand.command)}`); } return true; } async executeCommandFromString(command: string) { - this.log.debug(`Calling yargs with "${command}"`); - await yargs(command).strict().parseAsync(); + this.log.debug(`Calling command "${command}"`); + const yar = yargs(command) + .scriptName("") + .fail(() => 0) + .exitProcess(false); + + for (const cmd of this.commands) { + yar.command(cmd); + } + + try { + await yar.parseAsync(); + } catch (_) { + // do nothing + } } async executeCommand(commandId: string, parameters: string[]) { diff --git a/instance/src/systems/consoleCommands/application.ts b/instance/src/systems/consoleCommands/application.ts new file mode 100644 index 0000000..7204147 --- /dev/null +++ b/instance/src/systems/consoleCommands/application.ts @@ -0,0 +1,29 @@ +// import Command, { type ICommandRuntimeParameters } from "../command.ts"; + +// export default class ExitCommand extends Command { +// override commandId = "exit"; +// flags = {}; +// aliases = []; +// override shortDescription = "Terminate the Workspaces instance"; + +// async run(parameters: ICommandRuntimeParameters) { +// await this.instance.shutdown(); + +// // Note: do not remove this line of code or Typescript will complain +// return this.finishRun(); +// } +// } + +import type { CommandModule } from "yargs"; + +const command: CommandModule = { + command: "application ", + aliases: ["app"], + describe: "Manage an application", + handler(args) { + switch (args.action) { + } + }, +}; + +export default command; diff --git a/instance/src/systems/terminal.ts b/instance/src/systems/terminal.ts index 535ba9b..ab9fc03 100644 --- a/instance/src/systems/terminal.ts +++ b/instance/src/systems/terminal.ts @@ -1,4 +1,14 @@ -import { BoxRenderable, CliRenderer, ConsolePosition, createCliRenderer, InputRenderable, RGBA, ScrollBoxRenderable, TextRenderable } from "@opentui/core"; +import readline from "node:readline/promises"; +import { + BoxRenderable, + type CliRenderer, + ConsolePosition, + createCliRenderer, + InputRenderable, + RGBA, + ScrollBoxRenderable, + TextRenderable, +} from "@opentui/core"; import chalk from "chalk"; import type { Instance } from "../index.ts"; import { LogMessageStyle, LogType } from "../log.ts"; @@ -117,9 +127,8 @@ export default class TerminalUISystem extends System { } break; } - - global.backup.console.log(consoleLogOutput.split("")); } + global.backup.console.log(consoleLogOutput); } for (const message of this.instance.log.allLogHistory) { @@ -130,6 +139,22 @@ export default class TerminalUISystem extends System { addLogMessage(message); }); + const rlInterface = readline.createInterface(process.stdin, process.stdout); + + rlInterface.addListener("line", (data) => { + this.instance.sys.consoleCommands.executeCommandFromString(data); + }); + + rlInterface.on("SIGINT", () => { + rlInterface.close(); + this.instance.shutdown(); + }); + + rlInterface.on("SIGTERM", () => { + rlInterface.close(); + this.instance.shutdown(); + }); + return true; } diff --git a/instance/src/systems/trpc.ts b/instance/src/systems/trpc.ts index 327b604..4d14b75 100644 --- a/instance/src/systems/trpc.ts +++ b/instance/src/systems/trpc.ts @@ -1,89 +1,89 @@ -import type { TRPCBuiltRouter } from "@trpc/server"; -import { type FetchCreateContextFnOptions, fetchRequestHandler } from "@trpc/server/adapters/fetch"; -import type { BunRequest, Server } from "bun"; -import type { Instance } from "../index.ts"; -import System from "../system.ts"; -import type { createTRPCContext } from "./trpcRouter.ts"; - -export default class TRPCSystem extends System { - registeredRouters: { - basePath: string; - router: TRPCBuiltRouter; - createContext: (opts: FetchCreateContextFnOptions, server: Server>) => object; - }[]; - - constructor(instance: Instance) { - super("trpc", instance); - - this.registeredRouters = []; - } - - override async startup(): Promise { - this.log.info("Starting up..."); - return true; - } - - private attemptTRPCRequest(req: BunRequest, server: Server>) { - const url = new URL(req.url); - - for (const router of this.registeredRouters) { - if (!url.pathname.startsWith(router.basePath)) { - continue; - } - - return fetchRequestHandler({ - createContext: (opts) => router.createContext(opts, server), - req, - endpoint: router.basePath ?? "", - router: router.router, - }); - } - - return; - } - - // private bunWebSocketHandler() {} - - serve(options: { - routes: { - [path: string]: { - GET?: (req: BunRequest) => Promise; - POST?: (req: BunRequest) => Promise; - DELETE?: (req: BunRequest) => Promise; - PUT?: (req: BunRequest) => Promise; - }; - }; - fetch(request: any, server: any): Response; - development: boolean; - }) { - const self = this; - - return { - ...options, - port: 3563, - hostname: "0.0.0.0", - async fetch(req: BunRequest, server: Server>) { - try { - const trpcResponse = await self.attemptTRPCRequest(req, server); - if (trpcResponse) { - return trpcResponse; - } else { - return new Response("Hello from Workspaces!"); - } - } catch (err) { - self.log.error(err); - console.error(new Error("tRPC -----").stack); - return new Response("TRPC failed"); - } - }, - onError: (...p: any[]) => { - // Do nothing as the error is most-likely from bun.serve for tRPC contentType, (i have no clue why as everything else is working) - if (p[0].type === "unknown") return; - if (p[0].error.code === "UNAUTHORIZED") return; - - console.error(p[0].error); - this.log.error("^"); - }, - }; - } -} +import type { TRPCBuiltRouter } from "@trpc/server"; +import { type FetchCreateContextFnOptions, fetchRequestHandler } from "@trpc/server/adapters/fetch"; +import type { BunRequest, Server } from "bun"; +import type { Instance } from "../index.ts"; +import System from "../system.ts"; +import type { createTRPCContext } from "./trpcRouter.ts"; + +export default class TRPCSystem extends System { + registeredRouters: { + basePath: string; + router: TRPCBuiltRouter; + createContext: (opts: FetchCreateContextFnOptions, server: Server>) => object; + }[]; + + constructor(instance: Instance) { + super("trpc", instance); + + this.registeredRouters = []; + } + + override async startup(): Promise { + this.log.info("Starting up..."); + return true; + } + + private attemptTRPCRequest(req: BunRequest, server: Server>) { + const url = new URL(req.url); + + for (const router of this.registeredRouters) { + if (!url.pathname.startsWith(router.basePath)) { + continue; + } + + return fetchRequestHandler({ + createContext: (opts) => router.createContext(opts, server), + req, + endpoint: router.basePath ?? "", + router: router.router, + }); + } + + return; + } + + // private bunWebSocketHandler() {} + + serve(options: { + routes: { + [path: string]: { + GET?: (req: BunRequest) => Promise; + POST?: (req: BunRequest) => Promise; + DELETE?: (req: BunRequest) => Promise; + PUT?: (req: BunRequest) => Promise; + }; + }; + fetch(request: any, server: any): Response; + development: boolean; + }) { + const self = this; + + return { + ...options, + port: 3563, + hostname: "0.0.0.0", + async fetch(req: BunRequest, server: Server>) { + try { + const trpcResponse = await self.attemptTRPCRequest(req, server); + if (trpcResponse) { + return trpcResponse; + } else { + return new Response("Hello from Workspaces!"); + } + } catch (err) { + self.log.error(err); + console.error(new Error("tRPC -----").stack); + return new Response("TRPC failed"); + } + }, + onError: (...p: any[]) => { + // Do nothing as the error is most-likely from bun.serve for tRPC contentType, (i have no clue why as everything else is working) + if (p[0].type === "unknown") return; + if (p[0].error.code === "UNAUTHORIZED") return; + + console.error(p[0].error); + this.log.error("^"); + }, + }; + } +} -- knotmirror 0.0.1