hdw2
Version:
鸿蒙前端hdc调试工具
58 lines (50 loc) • 2 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-06-26 19:47:35
* @LastEditTime: 2024-09-11 20:03:07
* @LastEditors: tankunpeng
* @Description: 清空端口映射
* Come on, worker!
*/
const shell = require('shelljs');
const chalk = require('chalk');
const { list } = require('./list');
const { getHdc } = require('./util');
function printTitle(text = '=====>清空调试端口映射结束<=====') {
shell.echo(chalk.italic(chalk.bold(chalk.yellow(text))));
}
function clear(options = {}, command = {}) {
const commandName = command._name || '';
const fportLsStdout = list();
if (!fportLsStdout) {
shell.exit(0);
}
const hdc = getHdc();
printTitle('=====>清空调试端口映射开始<=====');
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);
shell.echo(`${chalk.cyan('调试端口映射列表: \n')}${chalk.green(fportLsStdout)}`);
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) {
shell.echo(`${chalk.cyan('清空调试端口映射结果: ')}${chalk.red('失败')}`);
shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow('删除映射命令执行失败,请手动执行命令尝试\n命令:' + chalk.green(fportCmd))}`);
printTitle();
shell.exit(1);
}
}
shell.echo(`${chalk.cyan('清空调试端口映射结果: ')}${chalk.green('成功')}`);
printTitle();
}
exports.clear = clear;