Skip to content

Commit f5c8c16

Browse files
committed
feat: 新增force-submit选项
1 parent 9d1ad0c commit f5c8c16

7 files changed

Lines changed: 53 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Options:
3434
-V, --version output the version number
3535
-p, --platform <platform> 操作的平台 (choices: "weixin", "alipay")
3636
-a, --action <action> 提审或者发布 (choices: "review", "release")
37+
-f, --force-submit 如果存在【审核中】或【审核通过】的版本,这将强制提交新的审核版本 (choices: "true", "false")
3738
-hl, --headless [headless] 浏览器无头模式 (choices: "false", "new", default: "new")
3839
-h, --help display help for command
3940
```

src/constants/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export enum ACTION {
1515
RELEASE = 'release',
1616
}
1717

18+
export enum BOOL {
19+
TRUE = 'true',
20+
FALSE = 'false',
21+
}
22+
1823
export const VIEWPORT: Viewport = { width: 1920, height: 1080, deviceScaleFactor: 1.5 }
1924

2025
export const __DEV__ = process.env.NODE_ENV === 'development'

src/core/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { bgGreen, blue, green, red, reset } from 'kolorist'
22
import prompts from 'prompts'
33
import { ACTION, PLATFORM } from '../constants'
44
import weixinRobot from '../weixin'
5-
import { isEmpty } from '../utils'
5+
import { isEmpty, onCancel } from '../utils'
66

77
export default async function main(options: InputOptions) {
88
const result: prompts.Answers<
@@ -31,9 +31,7 @@ export default async function main(options: InputOptions) {
3131
},
3232
],
3333
{
34-
onCancel: () => {
35-
throw new Error(`${red('✖')} Operation cancelled`)
36-
},
34+
onCancel,
3735
},
3836
)
3937
const opts = {

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Option, program } from 'commander'
2-
import { ACTION, PLATFORM, description, name, version } from './constants'
2+
import { ACTION, BOOL, PLATFORM, description, name, version } from './constants'
33
import main from './core'
44
import { handleOptions } from './utils'
55

66
program.name(name).version(version).description(description)
77
.addOption(new Option('-p, --platform <platform>', '操作的平台').choices(Object.values(PLATFORM)))
88
.addOption(new Option('-a, --action <action>', '提审或者发布').choices(Object.values(ACTION)))
9+
.addOption(new Option('-f, --force-submit', '如果存在【审核中】或【审核通过】的版本,这将强制提交新的审核版本').default(BOOL.FALSE).choices(Object.values(BOOL)))
910
.addOption(new Option('-hl, --headless [headless]', '浏览器无头模式').default('new').choices(['false', 'new']))
1011

1112
program.parse()

src/types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { PuppeteerLaunchOptions } from 'puppeteer'
2-
import type { ACTION, PLATFORM } from '../constants'
2+
import type { ACTION, BOOL, PLATFORM } from '../constants'
33

44
declare global {
55
interface InputOptions {
66
platform: PLATFORM
77
action: ACTION
88
headless: PuppeteerLaunchOptions['headless']
9+
forceSubmit: BOOL
910
}
1011
}

src/utils/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url'
44
import qrcodeTerminal from 'qrcode-terminal'
55
import jsqr from 'jsqr'
66
import UPNG from 'upng-js'
7+
import { red } from 'kolorist'
78

89
export const __dirname = path.dirname(fileURLToPath(import.meta.url))
910

@@ -75,5 +76,10 @@ export function transBooleanStrToBool(value: string) {
7576

7677
export function handleOptions(opts: InputOptions) {
7778
opts.headless = transBooleanStrToBool(opts.headless as string) as never
79+
opts.forceSubmit = transBooleanStrToBool(opts.forceSubmit as string) as never
7880
return opts
7981
}
82+
83+
export function onCancel() {
84+
throw new Error(`${red('✖')} Operation cancelled`)
85+
}

src/weixin/index.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import type { Browser, Page } from 'puppeteer'
44
import puppeteer from 'puppeteer'
55
import type { Ora } from 'ora'
66
import ora from 'ora'
7+
import prompts from 'prompts'
78
import { ACTION, VIEWPORT, WEIXIN_URL, __DEV__ } from '../constants'
8-
import { pathResolve, showQrCodeToTerminal, sleep } from '../utils'
9+
import { onCancel, pathResolve, showQrCodeToTerminal, sleep } from '../utils'
910

1011
let browser: Browser
1112
let page: Page
@@ -73,18 +74,47 @@ export async function jumpToVersions() {
7374
* 跳转确认提交审核界面
7475
*/
7576
export async function jumpToConfirmPage() {
76-
const submitReviewBtn = await page.waitForSelector('.mod_default_box.code_version_dev .weui-desktop-btn.weui-desktop-btn_primary')
77+
const submitReviewBtnSelector = '.mod_default_box.code_version_dev .weui-desktop-btn.weui-desktop-btn_primary'
78+
let submitReviewBtn = await page.waitForSelector(submitReviewBtnSelector)
7779
if (!submitReviewBtn) {
7880
spinner.fail('未找到提交审核按钮')
7981
throw new Error('未找到提交审核按钮')
8082
}
83+
const isSubmitReviewBtnDisabled = await submitReviewBtn.evaluate(btn => btn.classList.contains('weui-desktop-btn_disabled'))
8184
// 判断是否有提交审核中的版本
8285
const testVersion = await page.$('.mod_default_bd.default_box.test_version')
8386
if (testVersion && !await testVersion.evaluate(el => el.textContent?.includes('你暂无提交审核的版本或者版本已发布上线'))) {
84-
spinner.warn('存在提交审核中的版本')
85-
throw new Error('存在提交审核中的版本')
87+
if (!options.forceSubmit) {
88+
spinner.stop()
89+
const result: prompts.Answers<'forceSubmit'> = await prompts([
90+
{
91+
type: 'confirm',
92+
name: 'forceSubmit',
93+
message: '当前已存在版本,是否继续强制提交审核?',
94+
initial: false,
95+
},
96+
], {
97+
onCancel,
98+
})
99+
if (!result.forceSubmit)
100+
throw new Error('退出提审')
101+
else
102+
spinner.start()
103+
}
104+
}
105+
if (isSubmitReviewBtnDisabled) {
106+
// 撤回
107+
await page.evaluate(() => {
108+
const el: HTMLButtonElement | null = document.querySelector('.mod_default_bd.default_box.test_version .weui-desktop-dropdown__list-ele__text')
109+
el!.click()
110+
})
111+
await sleep()
112+
const confirm = await page.$('body > div:nth-child(9) > div.weui-desktop-dialog__wrp.self-weui-modal > div > div.weui-desktop-dialog__ft > div > div:nth-child(2) > button')
113+
await confirm?.click()
114+
await sleep(2000)
115+
submitReviewBtn = await page.waitForSelector(submitReviewBtnSelector)
86116
}
87-
await submitReviewBtn.click()
117+
await submitReviewBtn!.click()
88118
await sleep(1200) // 可能会报错
89119
spinner.start('正在提交审核中...')
90120
const agreeCheckbox = await page.waitForSelector('.weui-desktop-icon-checkbox')

0 commit comments

Comments
 (0)