hdw2
Version:
鸿蒙前端hdc调试工具
85 lines (79 loc) • 2.97 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-08-29 17:21:16
* @LastEditTime: 2025-08-28 10:41:04
* @LastEditors: tankunpeng
* @Description: 推送文件
* Come on, worker!
*/
const path = require('path');
const shell = require('shelljs');
const chalk = require('chalk');
const { getHdc, printTitle, shellLog, shellError, REMOTE_PATH_MAP } = require('./util');
function push(options = {}, command = {}, logOps = {}) {
const args = command.args || [];
const hdc = getHdc();
const local = args[0];
if (!options.local && !local) {
shellLog(`${chalk.cyan('执行结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false });
shellLog(
`${chalk.cyan('处理建议: ')}${chalk.yellow(
'缺少本地文件路径或文件名,只传文件名时,默认为当前目录,目标路径可省略,默认为/data/local/tmp/\n命令示例:' +
chalk.green(`hdw push -l ./text.zip -r /data/local/tmp/`) +
' 或 ' +
chalk.green(`hdw push text.zip`)
)}`,
logOps,
{ hideLog: false }
);
shell.exit(1);
}
printTitle('=====>推送文件<=====', logOps, { beforeLine: true, afterLine: true });
if (!options.remote) {
if (options.download) {
options.remote = REMOTE_PATH_MAP.download;
} else if (options.share) {
options.remote = REMOTE_PATH_MAP.share;
} else {
options.remote = REMOTE_PATH_MAP.temp;
}
}
if (!options.local) {
options.local = './';
}
const localPath = path.resolve(options.local, local || '');
const remotePath = options.remote;
shellLog(`${chalk.cyan('本地路径: ')}${chalk.green(localPath)}`, logOps);
shellLog(`${chalk.cyan('手机路径: ')}${chalk.yellow(remotePath)}`, logOps);
const hdcCmd = `${hdc} file send "${localPath}" "${remotePath}"`;
const hdcCmdExec = shell.exec(hdcCmd, { silent: true, async: true });
let hdcStdout = '';
hdcCmdExec.stdout.once('data', () => {
shellLog(`${chalk.cyan('命令输出: \n')}`, logOps);
});
hdcCmdExec.stdout.on('data', (data) => {
hdcStdout += data;
shellLog(`${data}`, logOps);
});
hdcCmdExec.stderr.on('data', (data) => {
if (args.includes('-h')) {
shellLog(`${data}`, logOps);
} else {
shellError(`${data}`, logOps, { hideLog: false });
}
});
hdcCmdExec.on('error', () => {
shellLog(`${chalk.cyan('命令执行结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false });
shellLog(
`${chalk.cyan('处理建议: ')}${chalk.yellow('命令执行失败,请手动执行命令尝试\n命令:' + chalk.green(hdcCmd))}`,
logOps,
{ hideLog: false }
);
shell.exit(1);
});
hdcCmdExec.on('close', () => {
const isSuccess = hdcStdout.includes('finish');
shellLog(`${chalk.cyan('文件发送结果: ')}${isSuccess ? chalk.green('成功') : chalk.red('失败')}`, logOps);
});
}
exports.push = push;