diff --git a/src/core/commands/cd.ts b/src/core/commands/cd.ts index 50f2d7b..8a8c912 100644 --- a/src/core/commands/cd.ts +++ b/src/core/commands/cd.ts @@ -32,30 +32,26 @@ function main(state: ShellState, __flags: string[], args: string[]): CommandOutp let inode: number; const session = getCurrentSession(state); - switch (args[0]) { - case undefined: - inode = session.user.home; - break; - case "-": - inode = session.pwd; - break; - default: - const result = safeResolvePath(state.filesystem, session.cwd, args[0]); - if (result.type === "error") { - return { - state, - result: createErrorResult( - "PathResolutionError", - "No such file or directory", - { path: args[0]! }, - createImmutableBuffer(new Uint8Array()), - createImmutableBuffer(new Uint8Array()), - COMMAND_NAME, - ), - } satisfies CommandOutput; - } - inode = result.inode; - break; + if (args[0] === undefined) { + inode = session.user.home; + } else if (args[0] === "-") { + inode = session.pwd; + } else { + const result = safeResolvePath(state.filesystem, session.cwd, args[0]); + if (result.type === "error") { + return { + state, + result: createErrorResult( + "PathResolutionError", + "No such file or directory", + { path: args[0]! }, + createImmutableBuffer(new Uint8Array()), + createImmutableBuffer(new Uint8Array()), + COMMAND_NAME, + ), + } satisfies CommandOutput; + } + inode = result.inode; } if (state.filesystem.isFile(inode)) { diff --git a/src/shell/db_manager.ts b/src/shell/db_manager.ts new file mode 100644 index 0000000..de17c3b --- /dev/null +++ b/src/shell/db_manager.ts @@ -0,0 +1,5 @@ +/* + Create an interface for the rest of the library to use to fetch nessesary data. + To do that, implement a termos config that will contain the table and db names, aswell as other relevant information - + maybe a flag for headless mode (uses the built-in console frontend module - need to evaluate this idea more) +*/ diff --git a/src/shell/shell.ts b/src/shell/shell.ts new file mode 100644 index 0000000..7739b88 --- /dev/null +++ b/src/shell/shell.ts @@ -0,0 +1,49 @@ +import type { Result } from "@core/result"; +import { type ShellState } from "@core/shell_state"; +import type { ShellError } from "@core/util/errors"; + +export class Shell { + static #instance: Shell; + static #promise: Promise | null = null; + private readonly state: Readonly; + + private constructor(state: ShellState) { + this.state = Object.freeze(state); + } + + static async init(): Promise { + if (Shell.#instance) { + console.warn("Shell already initialized"); + return this.#instance; + } + + if (Shell.#promise) return Shell.#promise; + + const state; //Create initial shell state, + + Shell.#promise = (async () => { + try { + Shell.#instance = new Shell(); + return Shell.#instance; + } finally { + Shell.#promise = null; + } + })(); + + return Shell.#promise; + } + + public async Execute(input: string): Promise> {} + + public get State(): Readonly { + return this.state; + } + + public static get Instance(): Shell | undefined { + if (!Shell.#instance) { + console.error("Shell has not been initialised yet. Call `await Singleton.init()` first."); + return; + } + return Shell.#instance; + } +}