cadb
Version:
安卓/鸿蒙系统截图/录屏工具
153 lines (142 loc) • 4.41 kB
JavaScript
/**
* Created by Niki on 2025/2/10 14:12.
* Email: m13296644326@163.com
*/
const path = require('path');
const program = require('commander');
const fs = require('fs');
const _ = require('lodash');
const {execFileSync} = require('child_process');
const {
maybeParseXTaroName,
getValueIgnoreCase,
ls2Array,
} = require('../common/utils/crnBundleUtil');
const {getCurrModuleInfo, adbPath, rcPath} = require('../common/utils');
const {logRed, logGreen} = require('../common/constants');
const {shellConfigs} = require('../../scripts/common/constants');
const bundleParentDir = path.join(process.cwd(), 'bundle_output');
const bundlePath = path.join(
bundleParentDir,
'publish',
'rn_business.jsbundle',
);
const adbRootPath = '/sdcard/Android/data/';
const deviceBundleDir = 'app_ctripwebapp5';
program
// 如果加了 -c 参数, 则不会使用rcConfig的缓存了
.option('-c --isCtrip', 'insert to ctrip app')
.option('-z --isZhixing', 'insert to zhixing app')
.option('-zl --isZhixingLight', 'insert to zhixing travel app') // 智行旅行
.option('-usejs --useJsBundleMode', 'use .jsbundle instead of .hbc') // 后续使用jsbundle模式
.action(arg => {
run(arg);
})
.parse(process.argv);
function run(arg) {
checkBundle();
const packName = parsePackName(arg);
const moduleName = readModuleName();
const destPath = assembleAndCheckDestPath(packName, moduleName);
adbPush(bundlePath, path.join(destPath, 'rn_business.jsbundle'));
deleteHermesConfigFile(arg, destPath);
logGreen('crn bundle插入成功, 请重启app');
}
function checkBundle() {
if (!fs.existsSync(bundlePath)) {
logRed(`bundle文件不存在: ${bundlePath}`);
process.exit();
}
}
function parsePackName(arg) {
const {isCtrip, isZhixing, isZhixingLight} = arg;
if (isCtrip) {
return 'ctrip.android.view';
} else if (isZhixing) {
return 'com.yipiao';
} else if (isZhixingLight) {
return 'cn.suanya.zhixing';
}
// 默认返回智行
return 'com.yipiao';
}
function readModuleName() {
let rcConfig = {};
if (!fs.existsSync(rcPath)) {
fs.writeFileSync(rcPath, JSON.stringify({}));
} else {
rcConfig = JSON.parse(fs.readFileSync(rcPath, 'utf-8'));
}
let {moduleName = ''} = getCurrModuleInfo();
// 如果是xTaro项目的moduleName, 则转换为crn频道名
moduleName = maybeParseXTaroName(moduleName);
console.log('moduleName', moduleName);
if (!moduleName.startsWith('rn_')) {
const moduleMaps = require('../config/module_map.json');
let prdName = getValueIgnoreCase(moduleMaps, moduleName);
const {exModuleMap = {}} = rcConfig;
if (!prdName) {
prdName = getValueIgnoreCase(exModuleMap, moduleName);
}
if (!prdName) {
// 如果仍找不到, 则怀疑是xTaro项目
prdName = maybeParseXTaroName('', true);
}
if (!prdName) {
logRed('未找到对应的crn频道名');
process.exit();
}
return prdName;
} else {
return moduleName;
}
}
function assembleAndCheckDestPath(packName, moduleName) {
const destPath = path.join(
adbRootPath,
packName,
deviceBundleDir,
moduleName,
);
const lsResult = ls2Array(destPath, true);
if (!lsResult) {
logRed(
`crn目录不存在: ${deviceBundleDir}/${moduleName}, 请至少进入一次相关crn页面`,
);
process.exit();
}
if (_.isEmpty(lsResult)) {
logRed(
`crn目录不存在任何文件: ${deviceBundleDir}/${moduleName}, 请至少进入一次相关crn页面`,
);
process.exit();
}
return destPath;
}
function adbPush(localBundlePath, destPath) {
const cmd = ['push', localBundlePath, destPath];
console.log('execute:', `adb ${cmd.join(' ')}`);
const result = execFileSync(adbPath, cmd, shellConfigs);
console.log(result);
}
function deleteHermesConfigFile(arg, destPath) {
const {useJsBundleMode} = arg;
const cmdRmHbc = [
'shell',
'rm',
path.join(destPath, 'rn_business.hbcbundle'),
];
const hbcResult = execFileSync(adbPath, cmdRmHbc, shellConfigs);
console.log(hbcResult);
if (!useJsBundleMode) {
const cmdRmConfigV6 = [
'shell',
'rm',
path.join(destPath, '_crn_config_v6'),
];
const configV6Result = execFileSync(adbPath, cmdRmConfigV6, shellConfigs);
console.log(configV6Result);
} else {
console.log('由于设置了-usejs, 不删除_crn_config_v6文件');
}
}