94 lines
2.2 KiB
TypeScript
94 lines
2.2 KiB
TypeScript
import { Menu } from './menu/menu';
|
|
import { MenuCommand, MenuType } from './menu/interface';
|
|
import { Display } from './display/display';
|
|
|
|
import { Chess } from './chess/chess';
|
|
|
|
import { exec, spawn } from 'child_process';
|
|
|
|
class Main {
|
|
private readonly _menuConfig = [
|
|
{
|
|
display: "df -h",
|
|
type: MenuType.ExecCommand,
|
|
command: {exe: "df", args: ['-h']},
|
|
},
|
|
{
|
|
display: "Games",
|
|
type: MenuType.SubMenu,
|
|
subMenu: [
|
|
{
|
|
display: "Chess",
|
|
type: MenuType.ExecModule,
|
|
module: new Chess().runAsync,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
display: "Reboot",
|
|
type: MenuType.Reboot,
|
|
},
|
|
{
|
|
display: "Shutdown",
|
|
type: MenuType.Shutdown,
|
|
},
|
|
];
|
|
|
|
private readonly _menu: Menu;
|
|
private readonly _display: Display;
|
|
private readonly _console: boolean = process.env['CONSOLE'] === '1';
|
|
|
|
constructor() {
|
|
this._menu = new Menu(this._menuConfig, this._console);
|
|
this._display = new Display(this._console);
|
|
}
|
|
|
|
private async runCommand(cmd: MenuCommand): Promise<void> {
|
|
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<void> {
|
|
while (true) {
|
|
const selected = await this._menu.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;
|
|
case MenuType.ExecModule:
|
|
if (selected.module) {
|
|
await selected.module();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function run() {
|
|
await new Main().runAsync();
|
|
}
|
|
|
|
run();
|