UNPKG

hdw2

Version:

鸿蒙前端hdc调试工具

101 lines (91 loc) 3.41 kB
/* * @Author: tankunpeng * @Date: 2024-09-16 13:57:36 * @LastEditTime: 2025-08-06 17:22:19 * @LastEditors: tankunpeng * @Description: hdc常用配置 * Come on, worker! */ const chalk = require('chalk'); const { getHdwConfig, setHdwConfig, getConfigPath, hasConfigKey, getConfigKeyMap, printTitle, shellLog, } = require('./util'); function checkKey(key, logOps = {}) { if (!key) { const keyMap = getConfigKeyMap(); shellLog(`${chalk.yellow(`请传入正确的key, 支持列表如下:`)}\n`, logOps, { hideLog: false }); shellLog(`${chalk.green(JSON.stringify(keyMap, null, 1))}\n`, logOps, { hideLog: false }); process.exit(1); } const hasKey = hasConfigKey(key); if (!hasKey) { const keyMap = getConfigKeyMap(); shellLog(`${chalk.yellow(`传入的 ${key} 不支持, 支持列表如下: `)}\n`, logOps, { hideLog: false }); shellLog(`${chalk.green(JSON.stringify(keyMap, null, 2))}\n`, logOps, { hideLog: false }); process.exit(1); } } function congigTrack(cmd, key, value, logOps = {}) { if (!cmd) { return; } printTitle('=====>hdw配置<=====', logOps, { beforeLine: true, afterLine: true }); // 配置列表 if (cmd === 'list') { const config = getHdwConfig(); shellLog(`${chalk.cyan('配置列表: \n')}${chalk.green(JSON.stringify(config, null, 1))}\n`, logOps, { hideLog: false, }); process.exit(0); } // 删除配置 if (cmd === 'delete') { checkKey(key, logOps); const config = getHdwConfig(); delete config[key]; setHdwConfig(config, true); shellLog(`${chalk.cyan('删除结果: ')}${chalk.green('成功')}\n`, logOps); shellLog(`${chalk.cyan('配置文件路径: ')}${chalk.green(getConfigPath())}\n`, logOps); shellLog(`${chalk.cyan('配置列表: \n')}${chalk.green(JSON.stringify(config, null, 1))}\n`, logOps); process.exit(0); } // 设置配置 if (cmd === 'set') { checkKey(key, logOps); if (!value) { shellLog(`${chalk.yellow(`请传入正确的value`)}\n`, logOps, { hideLog: false }); process.exit(1); } const config = {}; config[key] = value; setHdwConfig(config); shellLog(`${chalk.cyan('配置结果: ')}${chalk.green('成功')}\n`, logOps); shellLog(`${chalk.cyan('配置文件路径: ')}${chalk.green(getConfigPath())}\n`, logOps); shellLog(`${chalk.cyan('配置列表: \n')}${chalk.green(JSON.stringify(getHdwConfig(), null, 1))}\n`, logOps); process.exit(0); } // 获取配置 if (cmd === 'get') { checkKey(key, logOps); const configValue = getHdwConfig(key); if (!configValue) { shellLog(`${chalk.cyan(`${key}: `)}${chalk.green('当前未设置,使用默认配置')}\n`, logOps); process.exit(0); } shellLog(`${chalk.cyan(`${key}: `)}${chalk.green(configValue)}\n`, logOps); process.exit(0); } // 未知命令 shellLog(`${chalk.yellow(`传入的 ${cmd} 不支持, 请传入正确的命令 `)}\n`, logOps, { hideLog: false }); process.exit(1); } function config(cmd, key, value, options = {}, command = {}, logOps = {}) { congigTrack(cmd, key, value, logOps); } exports.config = config;