import { Menu } from './menu/menu'; import { MenuCommand, MenuType } from './menu/interface'; import { Display } from './display/display'; import { exec, spawn } from 'child_process'; class Main { private readonly _menu = [ { display: "df -h", type: MenuType.ExecCommand, command: {exe: "df", args: ['-h']}, }, { display: "Games", type: MenuType.SubMenu, subMenu: [ { display: "Chess", type: MenuType.ExecCommand, command: {exe: "df", args: ['-h']}, }, ], }, { display: "Reboot", type: MenuType.Reboot, }, { display: "Shutdown", type: MenuType.Shutdown, }, ]; private readonly _display: Display; private readonly _console: boolean = process.env['CONSOLE'] === '1'; constructor() { this._display = new Display(this._console); } private async runCommand(cmd: MenuCommand): Promise { return new Promise((resolve) => { this._display.displayContent( `Executing command:\n> ${cmd.exe} ${cmd.args?.join(' ')}`); let output = ''; const child = spawn(cmd.exe, cmd.args); child.stdout.on('data', (data) => { output += data; }); child.on('exit', async ()=> { await this._display.displayContent(output, cmd.wrap); resolve(); }); }); } public async runAsync(): Promise { while (true) { const selected = await new Menu(this._menu, this._console).getSelection(); switch(selected.type) { case MenuType.Shutdown: this._display.clear(true); exec('sudo shutdown now'); process.exit(); case MenuType.Reboot: this._display.clear(); exec('sudo reboot'); process.exit(); case MenuType.ExecCommand: if (selected.command) { await this.runCommand(selected.command); } break; } } } } async function run() { await new Main().runAsync(); } run();