UNPKG

hdw2

Version:

鸿蒙前端hdc调试工具

233 lines (210 loc) 7.11 kB
#!/usr/bin/env node /* * @Author: tankunpeng * @Date: 2024-06-26 17:46:33 * @LastEditTime: 2025-03-27 09:49:17 * @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 { sandbox } = require('./sandbox'); const { config } = require('./config'); const { CONFIG_KEY_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); }); program .command('check') .description('检查当前环境是否可进行webview调试') .action((options, command) => { check(options, command); }); program .command('list') .description('查询当前webview调试端口映射列表') .action((options, command) => { check(options, command); list(options, command); }); program .command('clear') .description('清空当前webview调试端口映射') .action((options, command) => { check(options, command); 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); 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); 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); pull(options, command); }); program .command('install') .description('HAP包安装,传参同hdc install') .action((options, command) => { check(options, command); install(options, command); }); program .command('get') .description('获取hdc常用数据') .argument('[hdc]', '获取hdc命令路径') .argument('[udid]', '获取手机udid') .argument('[args...]') .action((arg0, arg1, args, options, command) => { if (!arg0) { command.outputHelp(); return; } if (!/hdc/.test(arg0)) { check(options, command); } get(arg0, arg1, args, options, command); }); program .command('set') .description('hdc常用设置') .argument('[tcp] [ip] [port]', '设置为tcp调试模式') .argument('[usb]', '设置为usb调试模式') .argument('[args...]') .action((arg0, arg1, args, options, command) => { if (!arg0) { command.outputHelp(); return; } check(options, command); set(arg0, arg1, 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); } hdc(options, command); }); program .command('shell') .description('执行hdc shell命令') .allowUnknownOption() .action((options, command) => { check(options, command); hdcShell(options, command); }); program .command('ls') .option('-r --remote <remote>', '指定手机端文件路径') .option('-d --download', '指定手机端download目录') .description('获取手机端指定目录文件列表,默认为/data/local/tmp/') .action((options, command) => { check(options, command); fileList(options, command); }); program .command('rm') .option('-r --remote <remote>', '指定手机端文件路径') .option('-d --download', '指定手机端download目录') .description('删除手机端指定文件或目录,默认为/data/local/tmp/') .action((options, command) => { check(options, command); rm(options, command); }); program .command('sandbox') .option('-l --local <local>', '本地文件路径') .option('-r --remote <remote>', '手机端沙箱路径') .option('-c --common', '推送基础库zip包,传入-c时-r可省略') .option('-m --miniapp', '推送小程序zip包,传入-m时-r可省略') .option('-b --bundle <bundle>', '手机端应用bundleName') .option('-a --ability <ability>', '手机端应用abilityName') .description('推送文件到应用沙箱') .action((options, command) => { check(options, command); sandbox(options, command); }); program .command('config') .description('hdw配置设置、获取、删除等') .argument('[list]', '查看配置列表') .argument('[delete] [key]', '删除配置') .argument('[key]', '查询配置') .argument('[key] [value]', '设置配置') .addHelpText('after', ({ error, command } = {}) => { return `\n支持的完整配置:\n ${JSON.stringify(CONFIG_KEY_MAP, null, 1).replace(/[{}"]/g, '').replace(/,\n/g, '\n')}`; }) .action((arg0, ...args) => { const command = args.pop(); const options = args.pop(); if (!arg0) { command.outputHelp(); return; } config(arg0, args, options, command); }); program.parse(process.argv); if (process.argv.length <= 2) { program.outputHelp(); }