/* import { defaultConfig, TermosConfig } from "./config"; let config: TermosConfig = { ...defaultConfig }; namespace Termos { export function init(config: Partial) { config = { ...defaultConfig, ...config }; console.log("termos initialized"); } } */ import { FileSystem } from "@core/filesystem/filesystem"; import type { TreeNode } from "@core/filesystem/treenode"; import { getCurrentSession, type GroupInfo, type UserInfo } from "@core/session_manager"; import { createInitialShellState, type ShellState } from "@core/shell_state"; import { resolveInodeToHome } from "@core/util/path_resolver"; import { deserializeFileSystem } from "@shell/serializer"; import { Shell } from "@shell/shell"; import { printTree } from "./util/tree_visualizer"; const rawJson = `{"atime": 1786341244,"children": [{"atime": 1786341264,"children": [{"atime": 1786341330,"children": [],"content": null,"ctime": 1786341330,"group": 1000,"inode": 2,"mode": 493,"mtime": 1786341330,"name": "subdir1","owner": 1000,"parent": 1,"type": "directory"}],"content": null,"ctime": 1786341330,"group": 1000,"inode": 1,"mode": 493,"mtime": 1786341330,"name": "folder1","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341269,"children": [{"atime": 1786341344,"children": [],"content": null,"ctime": 1786341344,"group": 1000,"inode": 4,"mode": 493,"mtime": 1786341344,"name": "subdir2","owner": 1000,"parent": 3,"type": "directory"},{"atime": 1786341345,"children": [{"atime": 1786341356,"children": [],"content": [],"ctime": 1786341356,"group": 1000,"inode": 6,"mode": 420,"mtime": 1786341356,"name": "empty-foo","owner": 1000,"parent": 5,"type": "regular"}],"content": null,"ctime": 1786341356,"group": 1000,"inode": 5,"mode": 493,"mtime": 1786341356,"name": "subdir3","owner": 1000,"parent": 3,"type": "directory"}],"content": null,"ctime": 1786341345,"group": 1000,"inode": 3,"mode": 493,"mtime": 1786341345,"name": "folder2","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341312,"children": [],"content": [72,101,108,108,111,44,32,87,111,114,108,100,33,10],"ctime": 1786341312,"group": 1000,"inode": 7,"mode": 420,"mtime": 1786341312,"name": "some-file","owner": 1000,"parent": 0,"type": "regular"}],"content": null,"ctime": 1786341312,"group": 1000,"inode": 0,"mode": 493,"mtime": 1786341312,"name": "root","owner": 1000,"parent": 0,"type": "directory"}`; const base = deserializeFileSystem(rawJson); const overlay: ReadonlyMap = new Map(); const fs = FileSystem.init(base, overlay, base.size - 1); console.log(printTree(fs, 0)); const users: Map = new Map(); const groups: Map = new Map(); const user: UserInfo = { username: "root", uid: 1000, gid: 1000, // <- PRIMARY GID group: [1000], // <- GROUPS ARRAY home: 3, }; const group: GroupInfo = { groupname: "root", gid: 1000, members: [1000], // <- UID }; users.set(1000, user); groups.set(1000, group); const state: ShellState = createInitialShellState(fs, users, groups, user); await Shell.init(state); //console.log(resolveInode(Shell.Instance?.State!, 6)); //console.log(resolveInode(Shell.Instance?.State!, 6, true)); //console.log(sortTreeNodes(Shell.Instance?.State!, dnTraverseTree(Shell.Instance?.State.filesystem!, 0))); Shell.Instance?.on("result", payload => { const decoded = new TextDecoder().decode(payload.result.stdout.toArray()); process.stdout.write(decoded + "\n"); process.stdout.write(resolveInodeToHome(payload.state, getCurrentSession(payload.state).cwd) + "\n> "); }); /* Cant do it like this because commands also use the stdout event right before returning a result so e.g. echo will print twice Need to use this only if a continuous process is running */ /* Shell.Instance?.on("stdout", payload => { const decoded = new TextDecoder().decode(payload.data); process.stdout.write(decoded + "\n"); }); */ //console.clear(); process.stdout.write(resolveInodeToHome(state, getCurrentSession(state).cwd) + "\n> "); process.stdin.on("data", async data => { if (Shell.Instance?.BoundPID !== null) { Shell.Instance?.sendInput(data.toString().trim()); return; } const input = data.toString().trim(); await Shell.Instance?.execute(input); }); //export { Termos };