XuqmGroup-RNSDK/packages/update/scripts/package-options.mjs

49 行
1.8 KiB
JavaScript

const BUGCOLLECT_MODES = new Set(['platform', 'disabled'])
/**
* Release 打包参数的唯一解析入口。CLI 同时接收标准的 `--bugcollect=disabled`
* 和命令行工具常见的空格写法,但后续流程只消费归一化结果。
*/
export function parsePackageOptions(optionArgs) {
const remaining = [...optionArgs]
const apkIndex = remaining.indexOf('--apk')
const apk = apkIndex >= 0
if (apk) remaining.splice(apkIndex, 1)
const parsedBugCollect = parseBugCollectOption(remaining)
if (parsedBugCollect.remaining.length) {
throw new Error(`unknown option(s): ${parsedBugCollect.remaining.join(' ')}`)
}
return { apk, bugCollectMode: parsedBugCollect.bugCollectMode }
}
export function parseBugCollectOption(optionArgs, defaultMode = 'platform') {
const remaining = [...optionArgs]
const equalsOptions = remaining.filter(option => option.startsWith('--bugcollect='))
const spaceIndexes = remaining
.map((option, index) => (option === '--bugcollect' ? index : -1))
.filter(index => index >= 0)
if (equalsOptions.length + spaceIndexes.length > 1) {
throw new Error('--bugcollect must be specified only once')
}
let bugCollectMode = defaultMode
if (equalsOptions.length === 1) {
const index = remaining.indexOf(equalsOptions[0])
bugCollectMode = equalsOptions[0].slice('--bugcollect='.length)
remaining.splice(index, 1)
} else if (spaceIndexes.length === 1) {
const index = spaceIndexes[0]
if (remaining[index + 1] === undefined) {
throw new Error('--bugcollect requires a value')
}
bugCollectMode = remaining[index + 1]
remaining.splice(index, 2)
}
if (!BUGCOLLECT_MODES.has(bugCollectMode)) {
throw new Error('--bugcollect must be platform or disabled')
}
return { bugCollectMode, remaining }
}