From 37894068c4eb0947c370302bd7829be043755144 Mon Sep 17 00:00:00 2001 From: Suwings Date: Fri, 5 Aug 2022 10:28:26 +0800 Subject: [PATCH] Feat: translate commands --- src/entity/commands/docker/docker_resize.ts | 1 - .../commands/general/general_command.ts | 6 ++--- .../commands/general/general_restart.ts | 3 ++- src/entity/commands/general/general_start.ts | 22 +++++++++---------- src/entity/commands/general/general_stop.ts | 2 +- src/entity/commands/general/general_update.ts | 16 +++++++------- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/entity/commands/docker/docker_resize.ts b/src/entity/commands/docker/docker_resize.ts index 43d3fb9..c2742e0 100644 --- a/src/entity/commands/docker/docker_resize.ts +++ b/src/entity/commands/docker/docker_resize.ts @@ -9,7 +9,6 @@ export interface IResizeOptions { w: number; } -// 适用于 Docker 终端高宽定义命令,来自 @RimuruChan export default class DockerResizeCommand extends InstanceCommand { constructor() { super("ResizeTTY"); diff --git a/src/entity/commands/general/general_command.ts b/src/entity/commands/general/general_command.ts index 7e5b90f..e0fae5b 100644 --- a/src/entity/commands/general/general_command.ts +++ b/src/entity/commands/general/general_command.ts @@ -11,10 +11,10 @@ export default class GeneralSendCommand extends InstanceCommand { } async exec(instance: Instance, buf?: any): Promise { - // 关服命令需要发送命令,但关服命令执行前会设置状态为关闭中状态。 - // 所以这里只能通过进程是否存在来执行命令 + // The server shutdown command needs to send a command, but before the server shutdown command is executed, the status will be set to the shutdown state. + // So here the command can only be executed by whether the process exists or not if (!instance.process) instance.failure(new Error($t("command.instanceNotOpen"))); - // instance.process.write(buf); + instance.process.write(encode(buf, instance.config.oe)); if (instance.config.crlf === 2) return instance.process.write("\r\n"); return instance.process.write("\n"); diff --git a/src/entity/commands/general/general_restart.ts b/src/entity/commands/general/general_restart.ts index 3672b5d..6300c6a 100644 --- a/src/entity/commands/general/general_restart.ts +++ b/src/entity/commands/general/general_restart.ts @@ -14,7 +14,8 @@ export default class GeneralRestartCommand extends InstanceCommand { await instance.execPreset("stop"); instance.setLock(true); const startCount = instance.startCount; - // 每秒检查实例状态,如果实例状态为已停止,则立刻重启服务器 + // Check the instance status every second, + // if the instance status is stopped, restart the server immediately const task = setInterval(async () => { try { if (startCount !== instance.startCount) { diff --git a/src/entity/commands/general/general_start.ts b/src/entity/commands/general/general_start.ts index b8fc102..bca15d0 100644 --- a/src/entity/commands/general/general_start.ts +++ b/src/entity/commands/general/general_start.ts @@ -13,14 +13,14 @@ import { ChildProcess, exec, spawn } from "child_process"; import { commandStringToArray } from "../base/command_parser"; import { killProcess } from "../../../common/process_tools"; -// 启动时错误异常 +// Error exception at startup class StartupError extends Error { constructor(msg: string) { super(msg); } } -// Docker 进程适配器 +// Docker process adapter class ProcessAdapter extends EventEmitter implements IInstanceProcess { pid?: number | string; @@ -43,7 +43,7 @@ class ProcessAdapter extends EventEmitter implements IInstanceProcess { public async destroy() { try { if (this.process && this.process.stdout && this.process.stderr) { - // 移除所有动态新增的事件监听者 + // remove all dynamically added event listeners for (const eventName of this.process.stdout.eventNames()) this.process.stdout.removeAllListeners(eventName); for (const eventName of this.process.stderr.eventNames()) this.process.stderr.removeAllListeners(eventName); for (const eventName of this.process.eventNames()) this.process.removeAllListeners(eventName); @@ -66,12 +66,12 @@ export default class GeneralStartCommand extends InstanceCommand { try { instance.setLock(true); - // 设置启动状态 + // set startup state instance.status(Instance.STATUS_STARTING); - // 启动次数增加 + // increase the number of starts instance.startCount++; - // 命令解析 + // command parsing const commandList = commandStringToArray(instance.config.startCommand); const commandExeFile = commandList[0]; const commandParameters = commandList.slice(1); @@ -86,15 +86,15 @@ export default class GeneralStartCommand extends InstanceCommand { logger.info($t("general_start.cwd", { cwd: instance.config.cwd })); logger.info("----------------"); - // 创建子进程 - // 参数1直接传进程名或路径(含空格),无需双引号 + // create child process + // Parameter 1 directly passes the process name or path (including spaces) without double quotes const process = spawn(commandExeFile, commandParameters, { cwd: instance.config.cwd, stdio: "pipe", windowsHide: true }); - // 子进程创建结果检查 + // child process creation result check if (!process || !process.pid) { instance.println( "ERROR", @@ -107,10 +107,10 @@ export default class GeneralStartCommand extends InstanceCommand { throw new StartupError($t("general_start.startErr")); } - // 创建进程适配器 + // create process adapter const processAdapter = new ProcessAdapter(process); - // 产生开启事件 + // generate open event instance.started(processAdapter); logger.info($t("general_start.startSuccess", { instanceUuid: instance.instanceUuid, pid: process.pid })); instance.println("INFO", $t("general_start.startOrdinaryTerminal")); diff --git a/src/entity/commands/general/general_stop.ts b/src/entity/commands/general/general_stop.ts index 9d4605d..5bc0a2b 100644 --- a/src/entity/commands/general/general_stop.ts +++ b/src/entity/commands/general/general_stop.ts @@ -25,7 +25,7 @@ export default class GeneralStopCommand extends InstanceCommand { instance.println("INFO", $t("general_stop.execCmd")); const cacheStartCount = instance.startCount; - // 若 10 分钟后实例还处于停止中状态,则恢复状态 + // If the instance is still in the stopped state after 10 minutes, restore the state setTimeout(() => { if (instance.status() === Instance.STATUS_STOPPING && instance.startCount === cacheStartCount) { instance.println("ERROR", $t("general_stop.stopErr")); diff --git a/src/entity/commands/general/general_update.ts b/src/entity/commands/general/general_update.ts index 2ece590..9351848 100644 --- a/src/entity/commands/general/general_update.ts +++ b/src/entity/commands/general/general_update.ts @@ -16,7 +16,7 @@ export default class GeneralUpdateCommand extends InstanceCommand { super("GeneralUpdateCommand"); } - private stoped(instance: Instance) { + private stopped(instance: Instance) { instance.asynchronousTask = null; instance.setLock(false); instance.status(Instance.STATUS_STOP); @@ -33,7 +33,7 @@ export default class GeneralUpdateCommand extends InstanceCommand { logger.info($t("general_update.updateCmd", { instanceUuid: instance.instanceUuid })); logger.info(updateCommand); - // 命令解析 + // command parsing const commandList = commandStringToArray(updateCommand); const commandExeFile = commandList[0]; const commnadParameters = commandList.slice(1); @@ -41,22 +41,22 @@ export default class GeneralUpdateCommand extends InstanceCommand { return instance.failure(new Error($t("general_update.cmdFormatErr"))); } - // 启动更新命令 + // start the update command const process = spawn(commandExeFile, commnadParameters, { cwd: instance.config.cwd, stdio: "pipe", windowsHide: true }); if (!process || !process.pid) { - this.stoped(instance); + this.stopped(instance); return instance.println($t("general_update.err"), $t("general_update.updateFailed")); } - // process & pid 保存 + // process & pid this.pid = process.pid; this.process = process; - // 设置实例正在运行的异步任务 + // Set the asynchronous task that the instance is running instance.asynchronousTask = this; instance.status(Instance.STATUS_BUSY); @@ -67,7 +67,7 @@ export default class GeneralUpdateCommand extends InstanceCommand { instance.print(iconv.decode(text, instance.config.oe)); }); process.on("exit", (code) => { - this.stoped(instance); + this.stopped(instance); if (code === 0) { instance.println($t("general_update.update"), $t("general_update.updateSuccess")); } else { @@ -75,7 +75,7 @@ export default class GeneralUpdateCommand extends InstanceCommand { } }); } catch (err) { - this.stoped(instance); + this.stopped(instance); instance.println($t("general_update.update"), $t("general_update.error", { err: err })); } }