hdw2
Version:
鸿蒙前端hdc调试工具
87 lines (81 loc) • 3.14 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-08-29 18:36:55
* @LastEditTime: 2025-08-06 10:42:09
* @LastEditors: tankunpeng
* @Description: 拉取文件
* Come on, worker!
*/
const path = require('path');
const shell = require('shelljs');
const chalk = require('chalk');
const { getHdc, printTitle, shellLog, shellError } = require('./util');
function pull(options = {}, command = {}, logOps = {}) {
const args = command.args || [];
const hdc = getHdc();
const remote = args[0];
if (!options.remote && !remote) {
shellLog(`${chalk.cyan('执行结果: ')}${chalk.red('失败')}`, logOps, { hideLog: false });
shellLog(
`${chalk.cyan('处理建议: ')}${chalk.yellow(
'缺少手机端文件路径或文件名,只传文件名时,默认为/data/local/tmp/,本地路径可省略,默认为当前目录\n命令示例:' +
chalk.green(`hdw pull -l ./ -r /data/local/tmp/text.zip`) +
' 或 ' +
chalk.green(`hdw pull text.zip`)
)}`,
logOps,
{ hideLog: false }
);
shell.exit(1);
}
printTitle('=====>拉取文件<=====', logOps, { beforeLine: true, afterLine: true });
if (!options.remote) {
if (options.download) {
options.remote = '/storage/media/100/local/files/Docs/Download';
} else {
options.remote = '/data/local/tmp/';
}
}
if (!options.local) {
options.local = './';
}
const localPath = options.local;
const remotePath = path.posix.resolve(options.remote, remote || '');
shellLog(`${chalk.cyan('本地路径: ')}${chalk.green(localPath)}`, logOps);
shellLog(`${chalk.cyan('手机路径: ')}${chalk.yellow(remotePath)}`, logOps);
const hdcCmd = `${hdc} file recv ${remotePath} ${localPath}`;
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);
});
let hdcStderr = '';
hdcCmdExec.stderr.on('data', (data) => {
hdcStderr += 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.pull = pull;