hdw2
Version:
鸿蒙前端hdc调试工具
270 lines (248 loc) • 5.76 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-07-10 17:38:37
* @LastEditTime: 2025-08-07 15:01:47
* @LastEditors: tankunpeng
* @Description: 工具方法
* Come on, worker!
*/
const os = require('os');
const path = require('path');
const shell = require('shelljs');
const fs = require('fs');
const chalk = require('chalk');
const { isObject } = require('shelljs/src/common');
const HOME = process.env.HOME || os.homedir();
const CONFIG_PATH = path.join(HOME, './.hdwrc');
const CONFIG_KEY_MAP = {
useGlobalHdc: '全局hdc存在时,是否使用全局hdc',
bundleName: '目标应用bundleName',
abilityName: '目标应用abilityName',
sandboxRootPath: '目标应用沙箱根目录',
};
exports.CONFIG_KEY_MAP = CONFIG_KEY_MAP;
const osType = os.type();
exports.osType = osType;
// 常用包名配置
const CONFIG_BUNDLE_MAP = {
sirius: { bundle: 'com.zybank.siriusdemo', ability: 'EntryAbility' },
pmobile: { bundle: 'com.zybank.pmobile.hmos', ability: 'EntryAbility' },
yuanxin: { bundle: 'com.zyb.yuanxin.hmos', ability: 'EntryAbility' },
emobile: { bundle: 'com.zyebank.emobile.hmos', ability: 'EntryAbility' },
};
exports.CONFIG_BUNDLE_MAP = CONFIG_BUNDLE_MAP;
/**
* 环境判断
*/
const isMac = osType === 'Darwin';
const isWin = osType === 'Windows_NT';
const isLinux = osType === 'Linux';
exports.isMac = isMac;
exports.isWin = isWin;
exports.isLinux = isLinux;
/**
* 判断是否存在全局hdc命令
* @returns
*/
function hasGlobalHdc() {
const hasHdcShell = shell.which('hdc');
return hasHdcShell && hasHdcShell.code === 0;
}
/**
* 判断是否存在hdc命令
* @returns
*/
function hasHdc() {
if (hasGlobalHdc()) {
return true;
}
const hdc = getHdc();
if (!hdc) {
return false;
}
const hasHdcShell2 = shell.which(hdc);
return hasHdcShell2 && hasHdcShell2.code === 0;
}
exports.hasHdc = hasHdc;
/**
* 获取hdc
* @returns
*/
function getHdc() {
const useGlobalHdc = getHdwConfig('useGlobalHdc');
if (useGlobalHdc && hasGlobalHdc()) {
return 'hdc';
}
if (isWin) {
return path.join(__dirname, '../toolchains/win/hdc');
}
if (isMac) {
return path.join(__dirname, '../toolchains/mac/hdc');
}
return '';
}
exports.getHdc = getHdc;
/**
* 获取config key map
* @returns
*/
function getConfigKeyMap() {
return CONFIG_KEY_MAP;
}
exports.getConfigKeyMap = getConfigKeyMap;
/**
* 判断是否支持设置key
* @param {*} key
* @returns
*/
function hasConfigKey(key) {
if (!key) {
return false;
}
const keyMap = getConfigKeyMap();
if (keyMap[key]) {
return true;
}
return false;
}
exports.hasConfigKey = hasConfigKey;
/**
* 获取home路径
* @returns
*/
function getHOME() {
return HOME;
}
exports.getHOME = getHOME;
/**
* 获取配置路径地址
* @returns
*/
function getConfigPath() {
return CONFIG_PATH;
}
exports.getConfigPath = getConfigPath;
/**
* 判断配置文件是否存在
* @returns
*/
function isConfigExist() {
const configPath = getConfigPath();
return fs.existsSync(configPath);
}
exports.isConfigExist = isConfigExist;
/**
* 格式化value
* @param {*} value string
* @returns
*/
function formatValue(value) {
if (value === 'true' || value === 'false') {
return value === 'true';
}
if (/^\d+$/.test(value)) {
return Number(value);
}
return value;
}
exports.formatValue = formatValue;
/**
* 获取hwd配置
* @returns
*/
function getHdwConfig(key) {
if (!isConfigExist()) {
if (key) {
return;
}
return {};
}
const configPath = getConfigPath();
const configStr = fs.readFileSync(configPath, 'utf-8');
try {
const hdwConfig = JSON.parse(configStr);
if (key) {
const value = hdwConfig[key];
return value;
}
return hdwConfig;
} catch (error) {
console.log(chalk.red(`hdw配置文件格式错误,请修改未标准json格式,路径为: ${configPath}`));
process.exit(1);
}
}
exports.getHdwConfig = getHdwConfig;
/**
* 设置hwd配置
* @returns
*/
function setHdwConfig(config, force) {
if (!config || !isObject(config)) {
console.log(chalk.red(`hdw配置文件格式错误,请修改未标准json格式`));
process.exit(1);
}
const newConfig = {};
Object.entries(config).forEach(([key, value]) => {
newConfig[key] = formatValue(value);
});
let oldConfig = {};
if (!force) {
oldConfig = getHdwConfig();
}
const hdwConfig = {
...oldConfig,
...newConfig,
};
const configPath = getConfigPath();
fs.writeFileSync(configPath, JSON.stringify(hdwConfig, null, 2));
}
exports.setHdwConfig = setHdwConfig;
/**
* shell打印日志
* @param {*} text
* @param {*} ops
* @param {*} extOps
* @returns
*/
function shellLog(text, ops = {}, extOps = {}) {
const options = {
...ops,
...extOps,
};
if (!text || options.hideLog) {
return;
}
if (options.beforeLine) {
shell.echo('');
}
shell.echo(text);
if (options.afterLine) {
shell.echo('');
}
}
exports.shellLog = shellLog;
/**
* shell打印错误日志
* @param {*} text
* @param {*} ops
* @param {*} extOps
* @returns
*/
function shellError(text, ops = {}, extOps = {}) {
shellLog(chalk.red(text), ops, extOps);
}
exports.shellError = shellError;
/**
* 打印标题
* @param {*} text
* @param {*} ops
* @param {*} extOps
* @returns
*/
function printTitle(text, ops = {}, extOps = {}) {
if (!text) {
return;
}
shellLog(chalk.italic(chalk.bold(chalk.blue(text))), ops, extOps);
}
exports.printTitle = printTitle;