UNPKG

hdw2

Version:

鸿蒙前端hdc调试工具

69 lines (64 loc) 3.16 kB
/* * @Author: tankunpeng * @Date: 2024-06-26 19:47:35 * @LastEditTime: 2024-09-16 16:06:40 * @LastEditors: tankunpeng * @Description: 检查调试环境 * Come on, worker! */ const shell = require('shelljs'); const chalk = require('chalk'); const { hasHdc, getHdc } = require('./util'); function printTitle(text = '=====>环境检查结束<=====') { shell.echo(chalk.italic(chalk.bold(chalk.cyan(text)))); } function check(options = {}, command = {}) { const commandName = command._name || ''; const isCheck = commandName === 'check'; const isDevices = commandName === 'devices'; const hdc = getHdc(); isCheck && shell.echo(`${chalk.cyan('hdc路径: ')}${chalk.yellow(`${hdc}\n`)}`); isCheck && printTitle('=====>环境检查开始<====='); isCheck && shell.echo(chalk.cyan('[1/2]检查hdc命令是否存在...')); const hasHdcResult = hasHdc(); if (!hasHdcResult) { isCheck && shell.echo(chalk.red('[1/2]不通过')); shell.echo(`${chalk.cyan('环境检查结果: ')}${chalk.red('失败')}`); shell.echo( `${chalk.cyan('处理建议: ')}${chalk.yellow( `未找到hdc命令,且内部集成hdc命令行工具不可用,请尝试下载hdc命令行工具,并配置环境变量\n下载地址:${chalk.green( 'https://developer.huawei.com/consumer/cn/download/' )}` )}` ); isCheck && printTitle(); shell.exit(1); } isCheck && shell.echo(chalk.green('[1/2]通过')); isCheck && shell.echo(chalk.cyan('[2/2]检查鸿蒙设备连接状态...')); const deviceListCmd = `${hdc} list targets`; const deviceListExec = shell.exec(deviceListCmd, { silent: true }); if (deviceListExec.code !== 0) { isCheck && shell.echo(chalk.red('[2/2]不通过')); shell.echo(`${chalk.cyan('环境检查结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow(`查询设备列表命令执行失败,请手动执行命令尝试\n命令:${chalk.green(deviceListCmd)}`)}`); isCheck && printTitle(); shell.exit(1); } const deviceListStdout = deviceListExec.stdout; if (deviceListStdout.includes('[Empty]')) { isCheck && shell.echo(chalk.red('[2/2]不通过')); shell.echo(); shell.echo(`${chalk.cyan('环境检查结果: ')}${chalk.red('失败')}`); shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow('请确认设备已通过USB连接,开启开发者模式并打开USB调试开关\n')}`); isCheck && printTitle(); shell.exit(1); } isCheck && shell.echo(chalk.green('[2/2]通过')); isCheck && shell.echo(`${chalk.cyan('环境检查结果: ')}${chalk.green('通过')}\n`); (isCheck || isDevices) && shell.echo(`${chalk.cyan('\n设备列表: \n')}${chalk.green(deviceListStdout)}`); isCheck && printTitle(); isCheck && shell.echo(`${chalk.cyan('==>提示: \n')}${chalk.green(`现在可以打开需要调试的鸿蒙应用webview页面,执行${chalk.yellow('hdw debug')}命令进行调试了`)}`); } exports.check = check;