UNPKG

amishell

Version:

Interactive shell for the WinUAE and FS-UAE Amiga emulators.

136 lines 5.21 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const cp = require("child_process"); const net = require("net"); const events_1 = require("events"); class AmiShell extends events_1.EventEmitter { constructor(emulator, port, scripts = null) { super(); this.eot = false; this.emulator = emulator; this.port = port; if (scripts == null) { scripts = __dirname + "/../scripts"; } this.scripts = scripts; } executeCommand(command, activate, timeout, quiet = false) { return __awaiter(this, void 0, void 0, function* () { if (activate) { this.activateEmulator(); yield this.sleep(250); } yield this.createSocket(); return yield this.sendCommand(command, timeout, quiet); }); } sleep(ms) { return new Promise(resolve => { setTimeout(resolve, ms); }); } activateEmulator() { let command = ""; switch (process.platform) { case "darwin": command = "\"" + this.scripts + "/activate.osa" + "\" " + "fs-uae"; break; case "linux": command = "\"" + this.scripts + "/activate.sh" + "\" " + "fs-uae"; break; case "win32": let winPath = this.scripts.replace(/\//g, "\\"); command = "cscript //nologo \"" + winPath + "\\activate.vbs" + "\" "; switch (this.emulator) { case "fsuae": command += "fs-uae.exe"; break; case "winuae": command += "winuae.exe winuae64.exe"; break; } break; } if (command != "") { cp.exec(command, (error, stdout, stderr) => { if (error) { this.destroySocket(); this.emit("error", error); } }); } } createSocket() { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => { this.socket = net.createConnection(this.port, "localhost", () => { this.socket.setEncoding("ascii"); resolve(); }); this.socket.on("error", (error) => { this.destroySocket(); this.emit("error", error); }); }); }); } sendCommand(command, timeout, quiet) { return __awaiter(this, void 0, void 0, function* () { return new Promise(resolve => { let result = ""; let dataLength = 0; this.eot = false; let prompt = ""; this.socket.on("data", (data) => { var response = data.toString(); // AmigaDos sends ascii char SI (15) as EOT signal if (response.charCodeAt(0) == 15) { this.eot = true; } if (!this.eot) { response = response.replace(/\r/g, ""); // filter the command itself if (dataLength > command.length) { if (!quiet) { this.emit("data", response); } result += response; } dataLength += response.length; } else { // wait for complete prompt prompt += response; if (prompt.lastIndexOf("> ") >= 0) { this.socket.setTimeout(10); } } }); // the promise will be resolved on timeout this.socket.setTimeout(timeout); this.socket.on("timeout", () => { this.destroySocket(); resolve(result); }); // send the command this.socket.write(command + "\r"); }); }); } destroySocket() { if (this.socket && !this.socket.destroyed) { this.socket.end(); this.socket.destroy(); } } } exports.AmiShell = AmiShell; //# sourceMappingURL=amishell.js.map