hdw2
Version:
鸿蒙前端hdc调试工具
88 lines (81 loc) • 3 kB
JavaScript
/*
* @Author: tankunpeng
* @Date: 2024-08-29 17:21:16
* @LastEditTime: 2024-11-19 15:06:07
* @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 push(options = {}, command = {}) {
const args = command.args || [];
const hdc = getHdc();
const local = args[0];
if (!options.local && !local) {
shell.echo();
shell.echo(`${chalk.cyan('执行结果: ')}${chalk.red('失败')}`);
shell.echo(
`${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`)
)}`
);
shell.echo();
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 = path.resolve(options.local, local || '');
const remotePath = options.remote;
shell.echo();
shell.echo(`${chalk.cyan('本地路径: ')}${chalk.green(localPath)}`);
shell.echo(`${chalk.cyan('手机路径: ')}${chalk.yellow(remotePath)}`);
shell.echo();
const hdcCmd = `${hdc} file send ${localPath} ${remotePath}`;
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}`);
});
hdcCmdExec.stderr.on('data', (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('失败')}`);
shell.echo();
printTitle();
});
}
exports.push = push;