hdw2
Version:
鸿蒙前端hdc调试工具
88 lines (81 loc) • 3.02 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-08-29 18:36:55
* @LastEditTime: 2024-09-16 13:07:15
* @LastEditors: tankunpeng
* @Description: 拉取文件
* Come on, worker!
*/
const path = require('path');
const shell = require('shelljs');
const chalk = require('chalk');
const { getHdc } = require('./util');
function printTitle(text = '=====>拉取文件结束<=====') {
shell.echo(chalk.italic(chalk.bold(chalk.blue(text))));
}
function pull(options = {}, command = {}) {
const args = command.args || [];
const hdc = getHdc();
const remote = args[0];
if (!options.remote && !remote) {
shell.echo(`${chalk.cyan('执行结果: ')}${chalk.red('失败')}`);
shell.echo(
`${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`)
)}`
);
printTitle();
shell.exit(1);
}
printTitle('=====>拉取文件开始<=====');
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 || '');
shell.echo();
shell.echo(`${chalk.cyan('本地路径: ')}${chalk.green(localPath)}`);
shell.echo(`${chalk.cyan('手机路径: ')}${chalk.yellow(remotePath)}`);
shell.echo();
const hdcCmd = `${hdc} file recv ${remotePath} ${localPath}`;
const hdcCmdExec = shell.exec(hdcCmd, { silent: true, async: true });
let hdcStdout = '';
hdcCmdExec.stdout.once('data', () => {
shell.echo(`${chalk.cyan('命令输出: \n')}`);
});
hdcCmdExec.stdout.on('data', (data) => {
hdcStdout += data;
console.log(`${data}`);
});
let hdcStderr = '';
hdcCmdExec.stderr.on('data', (data) => {
hdcStderr += data;
if (args.includes('-h')) {
console.log(`${data}`);
} else {
console.error(`${data}`);
}
});
hdcCmdExec.on('error', () => {
shell.echo(`${chalk.cyan('命令执行结果: ')}${chalk.red('失败')}`);
shell.echo(`${chalk.cyan('处理建议: ')}${chalk.yellow('命令执行失败,请手动执行命令尝试\n命令:' + chalk.green(hdcCmd))}`);
printTitle();
shell.exit(1);
});
hdcCmdExec.on('close', () => {
const isSuccess = hdcStdout.includes('finish');
shell.echo(`${chalk.cyan('文件拉取结果: ')}${isSuccess ? chalk.green('成功') : chalk.red('失败')}`);
printTitle();
});
}
exports.pull = pull;