Skip to content

Commit 85b1a68

Browse files
committed
feat: 添加浏览器无头模式选项
1 parent e0dd3df commit 85b1a68

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ Usage: tiny-app-cli [options]
3131
自动提审与发布微信、支付宝小程序, 更好的实现小程序的CI/CD
3232

3333
Options:
34-
-V, --version output the version number
35-
-p, --platform <platform> 操作的平台 (choices: "weixin", "alipay")
36-
-a, --action <action> 提审或者发布 (choices: "review", "release")
37-
-h, --help display help for command
34+
-V, --version output the version number
35+
-p, --platform <platform> 操作的平台 (choices: "weixin", "alipay")
36+
-a, --action <action> 提审或者发布 (choices: "review", "release")
37+
-hl, --headless [headless] 浏览器无头模式 (choices: "false", "new", default: "new")
38+
-h, --help display help for command
3839
```
3940

4041
示例

src/core/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import weixinRobot from '../weixin'
55
import { isEmpty } from '../utils'
66

77
export default async function main(options: InputOptions) {
8-
let result: prompts.Answers<
8+
const result: prompts.Answers<
99
'platform' | 'action'
1010
> = await prompts(
1111
[
@@ -36,12 +36,12 @@ export default async function main(options: InputOptions) {
3636
},
3737
},
3838
)
39-
result = {
39+
const opts = {
4040
...options,
4141
...result,
4242
}
43-
if (result.platform === PLATFORM.WEIXIN)
44-
await weixinRobot(result.action as ACTION)
43+
if (opts.platform === PLATFORM.WEIXIN)
44+
await weixinRobot(opts)
4545
else
4646
console.log(bgGreen('正在开发中...'))
4747
}

src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Option, program } from 'commander'
22
import { ACTION, PLATFORM, description, name, version } from './constants'
33
import main from './core'
4+
import { handleOptions } from './utils'
45

56
program.name(name).version(version).description(description)
67
.addOption(new Option('-p, --platform <platform>', '操作的平台').choices(Object.values(PLATFORM)))
78
.addOption(new Option('-a, --action <action>', '提审或者发布').choices(Object.values(ACTION)))
9+
.addOption(new Option('-hl, --headless [headless]', '浏览器无头模式').default('new').choices(['false', 'new']))
810

911
program.parse()
1012

11-
const options = program.opts<InputOptions>()
13+
const options = handleOptions(program.opts<InputOptions>())
1214

1315
main(options).catch((err) => {
1416
console.error(err)

src/types/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import type { PuppeteerLaunchOptions } from 'puppeteer'
12
import type { ACTION, PLATFORM } from '../constants'
23

34
declare global {
45
interface InputOptions {
56
platform: PLATFORM
67
action: ACTION
8+
headless: PuppeteerLaunchOptions['headless']
79
}
810
}

src/utils/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,16 @@ export function isEmpty(value: unknown) {
6464

6565
return false
6666
}
67+
68+
export function transBooleanStrToBool(value: string) {
69+
if (value === 'true')
70+
return true
71+
if (value === 'false')
72+
return false
73+
return value
74+
}
75+
76+
export function handleOptions(opts: InputOptions) {
77+
opts.headless = transBooleanStrToBool(opts.headless as string) as never
78+
return opts
79+
}

src/weixin/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ let browser: Browser
1111
let page: Page
1212

1313
let spinner: Ora
14+
let options: InputOptions
1415

1516
/**
1617
* 获取微信图片二维码
1718
*/
18-
export async function getLoginScanCode() {
19+
export async function getLoginScanCode(opts: InputOptions = options) {
1920
spinner = ora('正在获取登录二维码...').start()
20-
browser = await puppeteer.launch({ headless: false })
21+
browser = await puppeteer.launch({ headless: __DEV__ ? false : opts.headless })
2122
page = await browser.newPage()
2223
await page.setViewport(VIEWPORT)
2324
await page.goto(WEIXIN_URL)
@@ -150,11 +151,12 @@ export async function toRelease() {
150151
console.log('正在开发中...')
151152
}
152153

153-
export default async function weixinRobot(action: ACTION) {
154+
export default async function weixinRobot(opts: InputOptions) {
155+
options = opts
154156
try {
155157
await getLoginScanCode()
156158
await jumpToVersions()
157-
if (action === ACTION.REVIEW) {
159+
if (options.action === ACTION.REVIEW) {
158160
await jumpToConfirmPage()
159161
await toSubmitAudit()
160162
}
@@ -164,7 +166,6 @@ export default async function weixinRobot(action: ACTION) {
164166
process.exit(0)
165167
}
166168
catch (err) {
167-
console.log(__DEV__)
168169
if (__DEV__) {
169170
console.error(err)
170171
return

0 commit comments

Comments
 (0)