UNPKG

cadb

Version:

安卓/鸿蒙系统截图/录屏工具

82 lines (76 loc) 2.3 kB
/** * Created by Niki on 2025/2/10 14:51. * Email: m13296644326@163.com */ const path = require('path'); const fs = require('fs'); const {spawnSync} = require('child_process'); const {logRed, shellConfigs} = require('../constants'); const _ = require('lodash'); const {getSystemHomePath} = require('./index'); function maybeParseXTaroName(moduleName, ignoreDirCheck = false) { // 是否是xTaro项目的命名规律, 比如crn-1003-072 if (/crn-\d+-.+/.test(moduleName) || ignoreDirCheck) { return parseXTaroNameFromConfig(ignoreDirCheck); } return moduleName; } function parseXTaroNameFromConfig(ignoreDirCheck) { // 去读取config.json的CRNModuleName字段 const packageJsonPath = path.join(process.cwd(), 'config.json'); if (!fs.existsSync(packageJsonPath)) { if (!ignoreDirCheck) { logRed('在当前xtaro项目下未找到config.json文件'); process.exit(); } return ''; } const jsonString = fs.readFileSync(packageJsonPath, 'utf-8'); const packageJson = JSON.parse(jsonString); const {CRNModuleName: cfgName} = packageJson; // 举例: ZtripCRNTaro_flight if (!cfgName.startsWith('ZtripCRNTaro_')) { if (!ignoreDirCheck) { logRed( 'config.json中的CRNModuleName字段不符合规范, 需要以ZtripCRNTaro_开头', ); process.exit(); } return ''; } return cfgName.replace('ZtripCRNTaro_', 'rn_taro_'); } function getValueIgnoreCase(obj, key) { const lowerCaseKey = key.toLowerCase(); const keys = _.keys(obj); const foundKey = keys.find(k => k.toLowerCase() === lowerCaseKey); if (foundKey) { return obj[foundKey]; } return undefined; } function ls2Array(dirPath, adbMode) { let ls; if (adbMode) { // todo 为了方便这里直接使用adb, 而非adbPath ls = spawnSync('adb', ['shell', 'ls', dirPath], shellConfigs); } else { ls = spawnSync('ls', [dirPath], shellConfigs); } if (ls.stderr) { // const msg = adbMode ? 'bundle路径不合法: ' : '本地路径不合法'; // throw new Error(msg, ls.stderr); logRed(ls.stderr); return null; } const lsString = (ls.stdout || '').trim(); if (lsString) { return lsString.split(/\s/); } return []; } module.exports = { maybeParseXTaroName, ls2Array, getValueIgnoreCase, };