cadb
Version:
安卓/鸿蒙系统截图/录屏工具
220 lines (203 loc) • 6.01 kB
JavaScript
/**
* Created by Niki on 2025/2/10 14:12.
* Email: m13296644326@163.com
*/
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const {execFileSync} = require('child_process');
const {adbPath, deletePath, hdcPath} = require('../../common/utils');
const {logRed, logGreen, shellConfigs} = require('../../common/constants');
const qrcode = require('qrcode-terminal');
const bundleParentDir = path.join(process.cwd(), 'bundle_output');
const businessBundleName = 'rn_business.jsbundle';
const bundlePublishPath = path.join(
bundleParentDir,
'publish',
businessBundleName,
);
let finalBundlePath = '';
let isAndroid = false;
let isHarmony = false;
/**
* 发起PackPush
* 安卓和鸿蒙会先push到app的download目录, app扫码后从download copy到work目录
* @param isCtrip
* @param isZhixing
* @param isZhixingLight 是否智行旅行
* @param productName crn频道名
* @param platform
*/
function doPackPush({
isCtrip,
isZhixing,
isZhixingLight,
productName,
platform,
}) {
if (!isCtrip && !isZhixing && !isZhixingLight) {
logRed('PackPush: 未指定app平台');
process.exit(1);
return;
}
if (!productName) {
logRed('PackPush: 缺少productName参数');
process.exit(1);
return;
}
if (!platform) {
logRed('PackPush: 缺少platform参数');
process.exit(1);
return;
}
isAndroid = platform.toLowerCase() === 'android';
isHarmony = platform.toLowerCase() === 'harmony';
run({isCtrip, isZhixing, isZhixingLight, productName});
}
function run(arg) {
const packName = parsePackName(arg);
const moduleName = arg.productName;
checkBundle(moduleName);
if (isAndroid) {
doAdbPush(packName, moduleName);
} else {
// 鸿蒙
doHdcPush(packName);
}
createQrCode(moduleName);
}
function checkBundle(moduleName) {
const isRnCommon = moduleName === 'rn_common';
let tPath = bundlePublishPath;
if (isRnCommon) {
tPath = getRNCommonBundlePath();
}
if (!fs.existsSync(tPath)) {
logRed(`bundle文件不存在: ${tPath}`);
process.exit();
} else if (isRnCommon) {
// 删除publish目录下除common_android.hbc和common_android.js以外的所有文件
const publishDir = path.dirname(tPath);
const files = fs.readdirSync(publishDir);
files.forEach(file => {
if (!getRNCommonBundleName().includes(file)) {
const filePath = path.join(publishDir, file);
// fs.unlinkSync(filePath);
deletePath(filePath);
}
});
console.log(
`已删除publish目录下除${getRNCommonBundleName().join(
'和',
)}以外的所有文件`,
);
}
const finalPath = path.join(bundleParentDir, moduleName);
if (fs.existsSync(finalPath)) {
deletePath(finalPath);
}
fs.renameSync(path.join(bundleParentDir, 'publish'), finalPath);
finalBundlePath = finalPath;
}
function getRNCommonBundleName() {
if (isAndroid) {
return ['common_android.hbc', 'common_android.js'];
} else if (isHarmony) {
return ['common_harmony.hbc', 'common_harmony.js'];
} else {
// ios
return ['common_ios.hbc', 'common_ios.js'];
}
}
function getRNCommonBundlePath() {
return path.join(bundleParentDir, 'publish', getRNCommonBundleName()[0]);
}
function parsePackName(arg) {
const {isCtrip, isZhixing, isZhixingLight} = arg;
if (isAndroid) {
if (isCtrip) {
return 'ctrip.android.view';
} else if (isZhixing) {
return 'com.yipiao';
} else if (isZhixingLight) {
return 'cn.suanya.zhixing';
}
// 默认返回智行
return 'com.yipiao';
} else if (isHarmony) {
if (isCtrip) {
return 'com.ctrip.harmonynext';
} else if (isZhixing) {
return 'com.suanya.harmonynext';
} else if (isZhixingLight) {
return 'com.suanya.travel.harmonynext';
}
// 默认返回智行
return 'com.suanya.harmonynext';
}
return 'com.yipiao';
}
/**
* 安卓
* push到设备上app内部cache目录
* @param packName
* @param moduleName
*/
function doAdbPush(packName, moduleName) {
const destPath = path.join('/sdcard/Android/data', packName, 'cache');
const modulePath = path.join(destPath, moduleName);
// const cmd = ['push', localBundlePath, destPath];
// 去掉删除旧文件的逻辑, 某些场景会导致问题
/*const exist = ls2Array(modulePath, true);
if (exist) {
const cmd = ['shell', 'rm', '-r', modulePath];
console.log('execute:', `adb ${cmd.join(' ')}`);
const result = execFileSync(adbPath, cmd, shellConfigs);
console.log(result);
}*/
const mkDirCmd = ['shell', 'mkdir', '-p', modulePath];
console.log('execute:', `adb ${mkDirCmd.join(' ')}`);
const result = execFileSync(adbPath, mkDirCmd, shellConfigs);
console.log(result);
adbPush(finalBundlePath, destPath);
}
function adbPush(localBundlePath, destPath) {
const cmd = ['push', localBundlePath, destPath];
console.log('execute:', `adb ${cmd.join(' ')}`);
const result = execFileSync(adbPath, cmd, shellConfigs);
logGreen(result);
}
/**
* 鸿蒙
* push到设备download目录的app包名目录
*/
function doHdcPush(packName) {
// /storage/media/100/local/files/Docs/Download/com.suanya.harmonynext/webapp
const destPath = path.join(
'/storage/media/100/local/files/Docs/Download',
packName,
'webapp',
);
hdcPush(finalBundlePath, destPath);
}
function hdcPush(localBundlePath, destPath) {
const cmd = ['file', 'send', localBundlePath, destPath];
console.log('execute:', `hdc ${cmd.join(' ')}`);
const result = execFileSync(hdcPath, cmd, shellConfigs);
logGreen(result);
}
function createQrCode(moduleName) {
// 在终端输出二维码
const qrText = `_copyRnBundle_://${JSON.stringify({
op: 'copy',
name: moduleName,
})}`;
console.log(`\r\n${qrText}`);
qrcode.generate(qrText, {small: true}, qrBinary => {
console.log(qrBinary);
});
logGreen('请使用手机扫码, 将bundle复制到work目录');
}
module.exports = {
doPackPush,
};