hdw2
Version:
鸿蒙前端hdc调试工具
57 lines (51 loc) • 2.15 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-06-26 19:47:35
* @LastEditTime: 2025-08-06 10:32:00
* @LastEditors: tankunpeng
* @Description: 清空端口映射
* Come on, worker!
*/
const shell = require('shelljs');
const chalk = require('chalk');
const { list } = require('./list');
const { getHdc, printTitle, shellLog } = require('./util');
function clear(options = {}, command = {}, logOps = {}) {
const hdc = getHdc();
printTitle('=====>清空调试端口映射<=====', logOps, { beforeLine: true, afterLine: true });
const fportLsStdout = list(undefined, undefined, { hideLog: true });
if (!fportLsStdout) {
shellLog(`${chalk.cyan('清空调试端口映射结果: ')}${chalk.green('成功')}`, logOps);
shell.exit(0);
}
const fportList = Array.from(new Set(fportLsStdout.split(/\n|\r/).filter((item) => item)));
const localRemoteArr = fportList
.map((item) => {
const arr = item.split(/\s+/);
const localRemoteArr = arr.slice(1, 3);
if (localRemoteArr.length === 2) {
return localRemoteArr;
}
return null;
})
.filter((item) => item);
shellLog(`${chalk.cyan('调试端口映射列表: \n')}${chalk.green(fportLsStdout)}`, logOps);
for (let i = 0; i < localRemoteArr.length; i++) {
const localRemote = localRemoteArr[i];
const fportCmd = `${hdc} fport rm ${localRemote[0]} ${localRemote[1]}`;
const pidExec = shell.exec(fportCmd, { silent: true });
if (pidExec.code !== 0) {
shellLog(`${chalk.cyan('清空调试端口映射结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false });
shellLog(
`${chalk.cyan('处理建议: ')}${chalk.yellow(
'删除映射命令执行失败,请手动执行命令尝试\n命令:' + chalk.green(fportCmd)
)}`,
logOps,
{ hideLog: false }
);
shell.exit(1);
}
}
shellLog(`${chalk.cyan('清空调试端口映射结果: ')}${chalk.green('成功')}`, logOps);
}
exports.clear = clear;