UNPKG

hdw2

Version:

鸿蒙前端hdc调试工具

127 lines (118 loc) 5.53 kB
/* * @Author: tankunpeng * @Date: 2024-06-26 19:47:35 * @LastEditTime: 2025-08-06 10:31:49 * @LastEditors: tankunpeng * @Description: 调试webview页面 * Come on, worker! */ const shell = require('shelljs'); const chalk = require('chalk'); const open = require('open'); const cp = require('copy-paste'); const { getHdc, printTitle, shellLog } = require('./util'); const { clear } = require('./clear'); const { list } = require('./list'); function debug(options = {}, command = {}, logOps = {}) { const hdc = getHdc(); const fportLsStdout = list(undefined, undefined, { hideLog: true }); if (fportLsStdout) { clear(undefined, undefined, { hideLog: true }); shellLog(`${chalk.cyan('清空调试端口映射结果: ')}${chalk.green('成功')}`, logOps); } printTitle('=====>映射调试端口<=====', logOps, { beforeLine: true, afterLine: true }); shellLog(chalk.cyan('查询可调试应用PID列表...'), logOps); const catCmd = `${hdc} shell "cat /proc/net/unix | grep devtools"`; const pidListExec = shell.exec(catCmd, { silent: true }); if (pidListExec.code !== 0) { shellLog(`${chalk.cyan('调试端口映射结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog( `${chalk.cyan('处理建议: ')}${ chalk.yellow('查询webview pid命令执行失败,请手动执行命令尝试\n命令:') + chalk.green(catCmd) }`, logOps, { hideLog: false } ); shell.exit(1); } const pidListStdout = pidListExec.stdout; const lines = pidListStdout .split(/\n|\r/) .filter((item) => item.includes('webview_devtools_remote_')) .map((item) => { return item.split('webview_devtools_remote_')[1]; }); const pidArr = Array.from(new Set(lines)); if (!pidArr.length) { shellLog(`${chalk.cyan('调试端口映射结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow( `未找到可调试应用,请打开需要调试的应用,且确保该应用已打开webview调试开关\n文档地址:${chalk.green( 'https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-debugging-with-devtools-V5' )}` )}`, logOps, { hideLog: false } ); shell.exit(1); } shellLog(`${chalk.cyan('应用PID列表: \n')}${chalk.green(pidArr.join('\n'))}\n`, logOps); shellLog(chalk.cyan('进行端口映射...'), logOps); const inputPort = parseInt(options.port); const defaultPort = 9222; let port = inputPort || defaultPort; shellLog(`${chalk.cyan('映射起始端口号: ')}${chalk.green(port)}`, logOps); for (let i = 0; i < pidArr.length; i++) { const pid = pidArr[i]; const fportCmd = `${hdc} fport tcp:${port} localabstract:webview_devtools_remote_${pid}`; const pidExec = shell.exec(fportCmd, { silent: true }); if (pidExec.code !== 0 || /\[Fail\]/.test(pidExec.stdout)) { shellLog(`${chalk.cyan('调试端口映射结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog(`${chalk.cyan('报错信息:\n')}${chalk.red(pidExec.stdout)}`, logOps, { hideLog: false }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow( `端口映射命令执行失败,请手动执行命令尝试,可以尝试更换端口号,如将${port}更换为${ port + 1 }\n命令: ${chalk.green(fportCmd)}` )}`, logOps, { hideLog: false } ); shell.exit(1); } port++; } shellLog(chalk.cyan('查询调试端口映射结果...'), logOps); const fportLsCmd = `${hdc} fport ls`; const fportLsExec = shell.exec(fportLsCmd, { silent: true }); if (fportLsExec.code !== 0) { shellLog(`${chalk.cyan('调试端口映射结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false }); shellLog( `${chalk.cyan('处理建议: ')}${chalk.yellow( '查询端口映射列表命令执行失败,请手动执行命令尝试\n命令:' + chalk.green(fportLsCmd) )}`, logOps, { hideLog: false } ); shell.exit(1); } const fportLs = fportLsExec.stdout; shellLog(`${chalk.cyan('调试端口映射结果: ')}${chalk.green('成功')}`, logOps); shellLog(`${chalk.cyan('调试映射列表: \n')}${chalk.green(fportLs)}`, logOps); shellLog(chalk.green('\n=====>浏览器调试地址<====='), logOps); const edgeUrl = 'edge://inspect/#devices'; const chromeUrl = 'chrome://inspect/#devices'; const suffix = ' ==>已复制到剪贴板'; shellLog(`${chalk.cyan('edge: ')}${chalk.green(`${edgeUrl}${(!options.chrome && suffix) || ''}`)}`, logOps); shellLog(`${chalk.cyan('chrome: ')}${chalk.green(`${chromeUrl}${(options.chrome && suffix) || ''}`)}`, logOps); const curUrl = options.chrome ? chromeUrl : edgeUrl; cp.copy(curUrl); if (options.open) { const appNames = options.chrome ? [open.apps.chrome, open.apps.edge] : [open.apps.edge, open.apps.chrome]; // edge浏览器兼容chrome://协议地址 open(chromeUrl, { app: { name: appNames }, }); } } exports.debug = debug;