some useless bs work on the indexed db and json fetching functions, shell done, can successfully call commands via the main Shell singleton

This commit is contained in:
2026-08-12 16:02:49 +02:00
parent 3ec3de71f4
commit a7798436d2
9 changed files with 443 additions and 19 deletions
+72 -17
View File
@@ -1,18 +1,21 @@
import { FileSystem } from "@core/filesystem/filesystem";
import type { Result } from "@core/result";
import { createInitialShellState, type ShellState } from "@core/shell_state";
import type { ShellError } from "@core/util/errors";
import { COMMAND_REGISTRY } from "@core/command_registry";
import { createErrorResult, createSuccessResult, type Result, type ResultError } from "@core/result";
import { getCurrentSession } from "@core/session_manager";
import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
import type { CommandNotFoundError, ParseError, PermissionDeniedError, ShellError } from "@core/util/errors";
import { createImmutableBuffer } from "@core/util/immutable_buffer";
import { parseCommand } from "@core/util/parser";
export class Shell {
static #instance: Shell;
static #promise: Promise<Shell> | null = null;
private readonly state: Readonly<ShellState>;
private _state: Readonly<ShellState>;
private constructor(state: ShellState) {
this.state = Object.freeze(state);
this._state = Object.freeze(state);
}
static async init(): Promise<Shell> {
static async init(initialState: ShellState): Promise<Shell> {
if (Shell.#instance) {
console.warn("Shell already initialized");
return this.#instance;
@@ -20,15 +23,9 @@ export class Shell {
if (Shell.#promise) return Shell.#promise;
const fs = FileSystem.init()
const users =
const sessionStack =
const state: ShellState = createInitialShellState(); //Create initial shell state,
Shell.#promise = (async () => {
try {
Shell.#instance = new Shell();
Shell.#instance = new Shell(initialState);
return Shell.#instance;
} finally {
Shell.#promise = null;
@@ -38,15 +35,73 @@ export class Shell {
return Shell.#promise;
}
public async Execute(input: string): Promise<Result<ShellError>> {}
public async Execute(input: string): Promise<Result<ShellError>> {
let parsed;
try {
parsed = parseCommand(input);
} catch {
return createErrorResult(
"ParseError",
`Parsing error - ${input}`,
{},
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
"",
) satisfies ResultError<ParseError>;
}
if (parsed.command === "") {
return createSuccessResult(
"",
0,
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
);
}
this._state = withHistory(this._state, input);
const command = COMMAND_REGISTRY.get(parsed.command);
if (!command)
return createErrorResult(
"CommandNotFound",
`Command ${parsed.command} not found`,
{ command: parsed.command },
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
parsed.command,
) satisfies ResultError<CommandNotFoundError>;
if (command.requiresRoot && getCurrentSession(this._state).user.uid != 0) {
return createErrorResult(
"PermissionDenied",
`Permission Denied`,
{ resource: `Execution of the ${command.name} command` },
createImmutableBuffer(new Uint8Array()),
createImmutableBuffer(new Uint8Array()),
parsed.command,
) satisfies ResultError<PermissionDeniedError>;
}
const out = command.fn(this._state, parsed.flags, parsed.args);
this._state = out.state;
if (out.result.type === "error") {
this._state = withExitCode(this._state, out.result.error.exitCode);
} else {
this._state = withExitCode(this._state, out.result.exitCode);
}
return out.result;
}
public get State(): Readonly<ShellState> {
return this.state;
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.");
console.error("Shell has not been initialised yet. Call `await Shell.init()` first.");
return;
}
return Shell.#instance;