chore: linux 网络测试命令验证

This commit is contained in:
xiaojunnuo
2025-09-30 18:01:49 +00:00
parent 2bef608e07
commit b364313297
10 changed files with 304 additions and 463 deletions

View File

@@ -1,7 +1,9 @@
//转换为import
//@ts-ignore
import childProcess from "child_process";
import { safePromise } from "./util.promise.js";
import { ILogger, logger } from "./util.log.js";
//@ts-ignore
import iconv from "iconv-lite";
export type ExecOption = {
cmd: string | string[];
@@ -28,12 +30,13 @@ async function exec(opts: ExecOption): Promise<string> {
cmd,
{
env: {
//@ts-ignore
...process.env,
...opts.env,
},
...opts.options,
},
(error, stdout, stderr) => {
(error: any, stdout: { toString: (arg0: string) => any }, stderr: any) => {
if (error) {
log.error(`exec error: ${error}`);
reject(error);
@@ -57,6 +60,7 @@ export type SpawnOption = {
};
function isWindows() {
// @ts-ignore
return process.platform === "win32";
}
function convert(buffer: any) {
@@ -94,31 +98,41 @@ async function spawn(opts: SpawnOption): Promise<string> {
const ls = childProcess.spawn(cmd, {
shell: true,
env: {
//@ts-ignore
...process.env,
...opts.env,
},
...opts.options,
});
ls.stdout.on("data", data => {
ls.stdout.on("data", (data: string) => {
data = convert(data);
log.info(`stdout: ${data}`);
stdout += data;
});
ls.stderr.on("data", data => {
ls.stderr.on("data", (data: string) => {
data = convert(data);
log.warn(`stderr: ${data}`);
stderr += data;
});
ls.on("error", error => {
ls.on("error", (error: any) => {
log.error(`child process error: ${error}`);
//@ts-ignore
error.stderr = stderr;
//@ts-ignore
error.stdout = stdout;
reject(error);
});
ls.on("close", (code: number) => {
if (code !== 0) {
log.error(`child process exited with code ${code}`);
reject(new Error(stderr));
const e = new Error(stderr || "return " + code);
//@ts-ignore
error.stderr = stderr;
//@ts-ignore
error.stdout = stdout;
reject(e);
} else {
resolve(stdout);
}