UNPKG

hdw2

Version:

鸿蒙前端hdc调试工具

75 lines (69 loc) 2.98 kB
/* * @Author: tankunpeng * @Date: 2024-06-26 19:47:35 * @LastEditTime: 2025-08-06 15:17:47 * @LastEditors: tankunpeng * @Description: 获取hdc命令路径 * Come on, worker! */ const shell = require('shelljs'); const chalk = require('chalk'); const { getHdc, printTitle, shellLog } = require('./util'); function getHdcPath(logOps = {}) { const hdc = getHdc(); printTitle('=====>获取hdc命令路径<=====', logOps, { beforeLine: true, afterLine: true }); shellLog(`${chalk.cyan('hdc命令路径: ')}${chalk.green(hdc)}`); } function getUDID(logOps = {}) { const hdc = getHdc(); printTitle('=====>获取手机udid<=====', logOps, { beforeLine: true, afterLine: true }); const udidCmd = `${hdc} shell bm get --udid`; const udidExec = shell.exec(udidCmd, { silent: true }); if (udidExec.code !== 0) { shellLog(`${chalk.cyan('获取手机udid结果: ')}${chalk.red('失败')}`); shellLog(`${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(udidCmd))}`); shell.exit(1); } const udid = udidExec.stdout.split(/[\r\n]/).find((item) => /^[a-zA-z0-9]{64,}$/.test(item)); if (!udid) { shellLog(`${chalk.cyan('获取手机udid结果: ')}${chalk.red('失败')}`); shellLog(`${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(udidCmd))}`); shellLog(`${chalk.cyan('失败输出: \n')}${chalk.yellow(udidExec.stdout)}`); shell.exit(1); } shellLog(`${chalk.cyan('手机udid: ')}${chalk.green(udid)}`); } function getApiVersion(logOps = {}) { const hdc = getHdc(); printTitle('=====>获取系统API版本<=====', logOps, { beforeLine: true, afterLine: true }); const apiCmd = `${hdc} shell param get const.ohos.apiversion`; const apiExec = shell.exec(apiCmd, { silent: true }); if (apiExec.code !== 0) { shellLog(`${chalk.cyan('获取系统API版本结果: ')}${chalk.red('失败')}`); shellLog(`${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(apiCmd))}`); shell.exit(1); } const apiVersion = apiExec.replace(/\s*/g, ''); if (!apiVersion) { shellLog(`${chalk.cyan('获取系统API版本结果: ')}${chalk.red('失败')}`); shellLog(`${chalk.cyan('处理建议: ')}${chalk.yellow('请手动执行命令尝试\n命令:' + chalk.green(apiCmd))}`); shellLog(`${chalk.cyan('失败输出: \n')}${chalk.yellow(apiExec.stdout)}`); shell.exit(1); } shellLog(`${chalk.cyan('系统API版本: ')}${chalk.green(apiVersion)}`); } function get(name, options = {}, command = {}, logOps = {}) { if (name === 'hdc') { getHdcPath(logOps); return; } if (name === 'udid') { getUDID(logOps); return; } if (name === 'apiVersion') { getApiVersion(logOps); return; } } exports.get = get;