UNPKG

hdw2

Version:

鸿蒙前端hdc调试工具

146 lines (128 loc) 5.44 kB
/* * @Author: tankunpeng * @Date: 2024-09-16 13:57:36 * @LastEditTime: 2025-08-06 15:46:45 * @LastEditors: tankunpeng * @Description: hdc常用设置 * Come on, worker! */ const shell = require('shelljs'); const chalk = require('chalk'); const { getHdc, printTitle, shellLog } = require('./util'); function getDevices(logOps = {}) { const hdc = getHdc(); const cmd0 = `${hdc} list targets`; const exec0 = shell.exec(cmd0, { silent: true }); if (exec0.code !== 0) { shellLog(`${chalk.cyan('查询设备列表结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow( `查询设备列表命令执行失败,请手动执行命令尝试\n命令:${chalk.green(cmd0)}` )}`, logOps, { hideLog: false } ); shell.exit(1); } const stout0 = exec0.stdout; if (stout0.includes('[Empty]')) { shellLog(`${chalk.cyan('查询设备列表结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow( `\n请手动执行: \n1. 设备usb重新插拔一下\n2. 手动执行hdw devices命令查看设备列表` )}`, logOps, { hideLog: false } ); shell.exit(1); } return stout0; } function setTcpMode(ip, port, logOps = {}) { const hdc = getHdc(); printTitle('=====>设置Tcp模式<=====', logOps, { beforeLine: true, afterLine: true }); const regexIPStrict = /^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$/; if (!regexIPStrict.test(ip)) { shellLog(`${chalk.cyan('传参错误: ')}${chalk.red('请传入手机端ip地址')}`, logOps, { hideLog: false }); shell.exit(1); } const formatPort = port || '9090'; const stdout = getDevices(); shellLog(`${chalk.cyan('切换前设备列表: \n')}${chalk.green(stdout)}`, logOps); const regexIP = /((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))/; if (regexIP.test(stdout)) { shellLog(`${chalk.cyan('Tcp模式切换结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog(`${chalk.cyan('处理建议: ')}${chalk.yellow(`当前已处于Tcp模式,无需切换`)}`, logOps, { hideLog: false, }); shell.exit(1); } const cmd1 = `${hdc} tmode port ${formatPort}`; const exec1 = shell.exec(cmd1, { silent: true }); if (exec1.code !== 0) { shellLog(`${chalk.cyan('将设备通过Usb模式切换至Tcp模式结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false, }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(cmd1))}`, logOps, { hideLog: false } ); shell.exit(1); } const cmd2 = `${hdc} tconn ${ip}:${formatPort}`; const exec2 = shell.exec(cmd2, { silent: true }); if (exec2.code !== 0) { shellLog(`${chalk.cyan('通过tcp连接设备结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false, }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(cmd2))}`, logOps, { hideLog: false } ); shell.exit(1); } shellLog(`${chalk.cyan('Tcp模式切换结果: ')}${chalk.green('成功')}\n`, logOps); const stdout3 = getDevices(); shellLog(`${chalk.cyan('切换后设备列表: \n')}${chalk.green(stdout3)}`, logOps); } function setUsbMode(logOps = {}) { const hdc = getHdc(); printTitle('=====>设置Usb模式<=====', logOps, { beforeLine: true, afterLine: true }); const stdout = getDevices(); shellLog(`${chalk.cyan('切换前设备列表: \n')}${chalk.green(stdout)}`, logOps); const regexIP = /((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))/; if (!regexIP.test(stdout)) { shellLog(`${chalk.cyan('Usb模式切换结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog(`${chalk.cyan('处理建议: ')}${chalk.yellow(`当前已处于Usb模式,无需切换`)}`, logOps, { hideLog: false, }); shell.exit(1); } const cmd1 = `${hdc} tmode usb`; const exec1 = shell.exec(cmd1, { silent: true }); if (exec1.code !== 0) { shellLog(`${chalk.cyan('Usb模式切换结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow('\n请手动执行命令尝试\n命令:' + chalk.green(cmd1))}`, logOps, { hideLog: false } ); shell.exit(1); } shellLog(`${chalk.cyan('Usb模式切换结果: ')}${chalk.green('成功')}\n`, logOps); const stdout2 = getDevices(); shellLog(`${chalk.cyan('切换后设备列表: \n')}${chalk.green(stdout2)}`, logOps); } function set(name, value, value2, args, options = {}, command = {}, logOps = {}) { if (name === 'tcp') { setTcpMode(value, value2, logOps); return; } if (name === 'usb') { setUsbMode(value, logOps); return; } } exports.set = set;