UNPKG

hdw2

Version:

鸿蒙前端hdc调试工具

137 lines (118 loc) 5.42 kB
/* * @Author: tankunpeng * @Date: 2024-09-16 13:57:36 * @LastEditTime: 2024-09-16 16:24:25 * @LastEditors: tankunpeng * @Description: hdc常用设置 * Come on, worker! */ const shell = require('shelljs'); const chalk = require('chalk'); const { getHdc } = require('./util'); function printTitle(text = '') { shell.echo(chalk.italic(chalk.bold(chalk.blue(text)))); } function getDevices(errTitle) { const hdc = getHdc(); const cmd0 = `${hdc} list targets`; const exec0 = shell.exec(cmd0, { silent: true }); if (exec0.code !== 0) { shell.echo(`${chalk.cyan('查询设备列表结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow(`查询设备列表命令执行失败,请手动执行命令尝试\n命令:${chalk.green(cmd0)}`)}`); printTitle(errTitle); shell.exit(1); } const stout0 = exec0.stdout; if (stout0.includes('[Empty]')) { shell.echo(); shell.echo(`${chalk.cyan('查询设备列表结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow(`\n请手动执行: \n1. 设备usb重新插拔一下\n2. 手动执行hdw devices命令查看设备列表`)}`); printTitle(errTitle); shell.exit(1); } return stout0; } function setTcpMode(ip, port) { const hdc = getHdc(); printTitle('=====>设置Tcp模式开始<====='); 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)) { shell.echo(); shell.echo(`${chalk.cyan('传参错误: ')}${chalk.red('请传入手机端ip地址')}`); shell.echo(); printTitle('=====>设置Tcp模式结束<====='); shell.exit(1); } const formatPort = port || '9090'; const stdout = getDevices('=====>设置Tcp模式结束<====='); shell.echo(); shell.echo(`${chalk.cyan('切换前设备列表: \n')}${chalk.green(stdout)}`); 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)) { shell.echo(); shell.echo(`${chalk.cyan('Tcp模式切换结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow(`当前已处于Tcp模式,无需切换`)}`); shell.echo(); printTitle('=====>设置Tcp模式结束<====='); shell.exit(1); } const cmd1 = `${hdc} tmode port ${formatPort}`; const exec1 = shell.exec(cmd1, { silent: true }); if (exec1.code !== 0) { shell.echo(`${chalk.cyan('将设备通过Usb模式切换至Tcp模式结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(cmd1))}`); printTitle('=====>设置Tcp模式结束<====='); shell.exit(1); } const cmd2 = `${hdc} tconn ${ip}:${formatPort}`; const exec2 = shell.exec(cmd2, { silent: true }); if (exec2.code !== 0) { shell.echo(`${chalk.cyan('通过tcp连接设备结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(cmd2))}`); printTitle('=====>设置Tcp模式结束<====='); shell.exit(1); } shell.echo(`${chalk.cyan('Tcp模式切换结果: ')}${chalk.green('成功')}\n`); const stdout3 = getDevices('=====>设置Tcp模式结束<====='); shell.echo(`${chalk.cyan('切换后设备列表: \n')}${chalk.green(stdout3)}`); printTitle('=====>设置Tcp模式结束<====='); } function setUsbMode() { const hdc = getHdc(); printTitle('=====>设置Usb模式开始<====='); const stdout = getDevices('=====>设置Usb模式结束<====='); shell.echo(); shell.echo(`${chalk.cyan('切换前设备列表: \n')}${chalk.green(stdout)}`); 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)) { shell.echo(); shell.echo(`${chalk.cyan('Usb模式切换结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow(`当前已处于Usb模式,无需切换`)}`); shell.echo(); printTitle('=====>设置Usb模式结束<====='); shell.exit(1); } const cmd1 = `${hdc} tmode usb`; const exec1 = shell.exec(cmd1, { silent: true }); if (exec1.code !== 0) { shell.echo(`${chalk.cyan('Usb模式切换结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow('\n请手动执行命令尝试\n命令:' + chalk.green(cmd1))}`); printTitle('=====>设置Usb模式结束<====='); shell.exit(1); } shell.echo(`${chalk.cyan('Usb模式切换结果: ')}${chalk.green('成功')}\n`); const stdout2 = getDevices('=====>设置Usb模式结束<====='); shell.echo(`${chalk.cyan('切换后设备列表: \n')}${chalk.green(stdout2)}`); printTitle('=====>设置Usb模式结束<====='); } function set(arg0, arg1, args = [], options = {}, command = {}) { if (arg0 === 'tcp') { setTcpMode(arg1, ...args); return; } if (arg0 === 'usb') { setUsbMode(arg1, ...args); return; } } exports.set = set;