Feat: translate commands

This commit is contained in:
Suwings 2022-08-05 10:28:26 +08:00
parent 11d866b800
commit 37894068c4
6 changed files with 25 additions and 25 deletions

View File

@ -9,7 +9,6 @@ export interface IResizeOptions {
w: number;
}
// 适用于 Docker 终端高宽定义命令,来自 @RimuruChan
export default class DockerResizeCommand extends InstanceCommand {
constructor() {
super("ResizeTTY");

View File

@ -11,10 +11,10 @@ export default class GeneralSendCommand extends InstanceCommand {
}
async exec(instance: Instance, buf?: any): Promise<any> {
// 关服命令需要发送命令,但关服命令执行前会设置状态为关闭中状态。
// 所以这里只能通过进程是否存在来执行命令
// 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");

View File

@ -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) {

View File

@ -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"));

View File

@ -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"));

View File

@ -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 }));
}
}