103 lines
2.9 KiB
TypeScript
103 lines
2.9 KiB
TypeScript
/**
|
||
* CLI 入口:开发环境生成 lewan 领取 mock。
|
||
* 推荐使用后台「开发 Mock」页面。
|
||
*/
|
||
import process from 'node:process'
|
||
|
||
import { closeDb } from '../src/db/client.js'
|
||
import {
|
||
createLewanMockClaim,
|
||
isDevMockEnabled,
|
||
} from '../src/services/dev-mock/dev-mock-service.js'
|
||
|
||
type JsonObject = Record<string, unknown>
|
||
|
||
const args = parseArgs(process.argv.slice(2))
|
||
|
||
if (args.help) {
|
||
printHelp()
|
||
process.exit(0)
|
||
}
|
||
|
||
if (!isDevMockEnabled() && args.force !== true) {
|
||
console.error(
|
||
'当前是生产环境,已拒绝生成 mock 数据。如确实要执行,请追加 --force,或设置 ENABLE_DEV_MOCK=1。',
|
||
)
|
||
process.exit(1)
|
||
}
|
||
|
||
if (args.force === true) {
|
||
process.env.ENABLE_DEV_MOCK = '1'
|
||
}
|
||
|
||
try {
|
||
const result = await createLewanMockClaim({
|
||
step: args.step,
|
||
orderNo: args.orderNo,
|
||
productNo: args.productNo,
|
||
uid: args.uid,
|
||
items: args.items,
|
||
frontendBaseUrl: args.frontendBaseUrl,
|
||
})
|
||
|
||
console.log('已生成 kuaishou-lewan 领取 mock 数据:')
|
||
console.log(` 91订单号:${result.orderNo}`)
|
||
console.log(` 商品:${result.productName}`)
|
||
console.log(` 当前步骤:${result.step}`)
|
||
console.log(` 期望 UID:${result.expectedUid}`)
|
||
console.log(` 领取链接:${result.claimUrl}`)
|
||
console.log(` 前端本地链接:${result.frontendClaimUrl}`)
|
||
console.log('')
|
||
for (const tip of result.tips) {
|
||
console.log(` · ${tip}`)
|
||
}
|
||
console.log(` ${result.open91QueryHint}`)
|
||
} catch (error) {
|
||
console.error(error instanceof Error ? error.message : error)
|
||
process.exitCode = 1
|
||
} finally {
|
||
await closeDb()
|
||
}
|
||
|
||
function parseArgs(rawArgs: string[]) {
|
||
const parsed: JsonObject = {}
|
||
for (const rawArg of rawArgs) {
|
||
const arg = String(rawArg || '').trim()
|
||
if (!arg) continue
|
||
if (arg === '--help' || arg === '-h') {
|
||
parsed.help = true
|
||
continue
|
||
}
|
||
if (arg === '--force') {
|
||
parsed.force = true
|
||
continue
|
||
}
|
||
const normalized = arg.startsWith('--') ? arg.slice(2) : arg
|
||
const separatorIndex = normalized.indexOf('=')
|
||
if (separatorIndex < 0) {
|
||
parsed[normalized] = true
|
||
continue
|
||
}
|
||
const key = normalized.slice(0, separatorIndex).trim()
|
||
const value = normalized.slice(separatorIndex + 1).trim()
|
||
if (key) parsed[key] = value
|
||
}
|
||
return parsed
|
||
}
|
||
|
||
function printHelp() {
|
||
console.log(`
|
||
生成 lewan 领取 mock(CLI)。推荐使用后台 /admin/dev-mock。
|
||
|
||
npm run mock:claim -- --step=binding --uid=10001
|
||
|
||
参数:
|
||
--step uid | binding | confirm | result(默认 uid)
|
||
--uid 游戏 UID(binding 起默认 10001)
|
||
--orderNo 订单号,默认自动生成
|
||
--productNo 91 商品名,默认 套餐_1----3676797936
|
||
--frontendBaseUrl 前端 base,用于打印本地 claim 链接
|
||
--force 允许在 production 执行(仍需 ENABLE_DEV_MOCK 或 force 打开开关)
|
||
`)
|
||
}
|