hdw2
Version:
鸿蒙前端hdc调试工具
254 lines (229 loc) • 7.57 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-06-26 17:46:33
* @LastEditTime: 2025-08-07 11:46:15
* @LastEditors: tankunpeng
* @Description:
* Come on, worker!
*/
const { program } = require('commander');
const pkg = require('../package.json');
const chalk = require('chalk');
const { check } = require('./check');
const { list } = require('./list');
const { debug } = require('./debug');
const { clear } = require('./clear');
const { hdc } = require('./hdc');
const { push } = require('./push');
const { pull } = require('./pull');
const { install } = require('./install');
const { get } = require('./get');
const { set } = require('./set');
const { hdcShell } = require('./shell');
const { fileList } = require('./ls');
const { rm } = require('./rm');
const { config } = require('./config');
const { send } = require('./send');
const { CONFIG_KEY_MAP, CONFIG_BUNDLE_MAP } = require('./util');
program
.name('hdw')
.version(`→ hdw ${chalk.cyan(pkg.version)}`, '-v --version', '输出当前版本号')
.helpOption('-h, --help', '显示帮助信息')
.addHelpCommand('help [命令]', '显示命令帮助信息')
.usage('<命令> [选项]');
program.configureHelp({
sortSubcommands: true,
subcommandTerm: (cmd) => {
return cmd.name();
},
});
program.addHelpText('afterAll', ({ error, command } = {}) => {
return `\n官方参考文档:\n https://developer.huawei.com/consumer/cn/develop/`;
});
program
.command('devices')
.description('检查当前环境,获取设备列表')
.action((options, command) => {
check(options, command, { hideLog: true, showDevices: true });
});
program
.command('check')
.description('检查当前环境是否可进行webview调试')
.action((options, command) => {
check(options, command);
});
program
.command('list')
.description('查询当前webview调试端口映射列表')
.action((options, command) => {
check(options, command, { hideLog: true });
list(options, command);
});
program
.command('clear')
.description('清空当前webview调试端口映射')
.action((options, command) => {
check(options, command, { hideLog: true });
clear(options, command);
});
program
.command('debug')
.option('-p --port <port>', '自定义调试映射端口')
.option('-o --open', '自动打开浏览器调试页面')
.option('-c --chrome', '使用Chrome浏览器调试,默认使用Edge')
.description('开始webview调试')
.action((options, command) => {
check(options, command, { hideLog: true });
debug(options, command);
});
program
.command('push')
.option('-l --local <local>', '本地文件路径')
.option('-r --remote <remote>', '手机端文件路径')
.option('-d --download', '推到手机端download目录')
.description('推送文件到手机,默认推送路径为/data/local/tmp/')
.action((options, command) => {
check(options, command, { hideLog: true });
push(options, command);
});
program
.command('pull')
.option('-l --local <local>', '本地文件路径')
.option('-r --remote <remote>', '手机端文件路径')
.option('-d --download', '拉取自手机端download目录')
.description('拉取文件到本地,默认拉取路径为/data/local/tmp/')
.action((options, command) => {
check(options, command, { hideLog: true });
pull(options, command);
});
program
.command('install')
.description('HAP包安装,传参同hdc install')
.action((options, command) => {
check(options, command, { hideLog: true });
install(options, command);
});
program
.command('get')
.description('获取hdc常用数据')
.argument('[name]', '获取数据名称')
.action((name, options, command) => {
if (!name) {
command.outputHelp((str) => {
str += '\n支持数据名称:\n';
str += ' hdc: 获取hdc命令路径\n';
str += ' udid: 获取手机udid\n';
str += ' apiVersion: 获取系统api版本\n';
return str;
});
return;
}
if (!/hdc/.test(name)) {
check(options, command, { hideLog: true });
}
get(name, options, command);
});
program
.command('set')
.description('hdc常用设置')
.argument('[name]', '设置数据名称')
.argument('[value]', '设置数据值')
.argument('[value2]', '设置数据值')
.argument('[args...]')
.action((name, value, value2, args, options, command) => {
if (!name) {
command.outputHelp((str) => {
str += '\n支持数据名称:\n';
str += ' tcp [ip] [port]:设置为tcp调试模式\n';
str += ' usb: 设置为usb调试模式\n';
return str;
});
return;
}
check(options, command, { hideLog: true });
set(name, value, value2, args, options, command);
});
program
.command('hdc')
.option('-V', 'hdc版本号')
.option('-H', 'hdc帮助信息')
.description('执行hdc命令')
.allowUnknownOption()
.action((options, command) => {
if (!options.V && !options.H) {
check(options, command, { hideLog: true });
}
hdc(options, command);
});
program
.command('shell')
.description('执行hdc shell命令')
.allowUnknownOption()
.action((options, command) => {
check(options, command, { hideLog: true });
hdcShell(options, command);
});
program
.command('ls')
.option('-r --remote <remote>', '指定手机端文件路径')
.option('-d --download', '指定手机端download目录')
.description('获取手机端指定目录文件列表,默认为/data/local/tmp/')
.action((options, command) => {
check(options, command, { hideLog: true });
fileList(options, command);
});
program
.command('rm')
.option('-r --remote <remote>', '指定手机端文件路径')
.option('-d --download', '指定手机端download目录')
.description('删除手机端指定文件或目录,默认为/data/local/tmp/')
.action((options, command) => {
check(options, command, { hideLog: true });
rm(options, command);
});
program
.command('send')
.option('-b --bundle <bundle>', '手机端应用bundleName')
.option('-a --ability <ability>', '手机端应用abilityName')
.option('-t --target <target>', '推送手机端目标应用,-b和-a的快捷选项,优先级低于-b和-a')
.description('推送小程序或基础库到应用')
.addHelpText('after', () => {
let str = '\n-t --target 支持的传参列表:\n';
Object.entries(CONFIG_BUNDLE_MAP).forEach(([key, config]) => {
str += ` ${key}: ${config.bundle}\n`;
});
return str;
})
.action((options, command) => {
check(options, command, { hideLog: true });
send(options, command);
});
program
.command('config')
.description('hdw配置设置、获取、删除等')
.argument('[cmd]', '命令')
.argument('[key]', 'key')
.argument('[value]', 'value')
.action((cmd, key, value, options, command) => {
if (!cmd) {
command.outputHelp((str) => {
str += '\n支持的命令列表:\n';
str += ' list: 查看配置列表\n';
str += ' delete [key]: 删除配置\n';
str += ' get [key]: 查询配置\n';
str += ' set [key] [value]: 设置配置\n';
str += '\n支持的完整配置:\n';
Object.entries(CONFIG_KEY_MAP).forEach(([key, value]) => {
str += ` ${key}: ${value}\n`;
});
return str;
});
return;
}
config(cmd, key, value, options, command);
});
program.parse(process.argv);
if (process.argv.length <= 2) {
program.outputHelp();
}