Skip to content

Commit

Permalink
feat: 添加浏览器无头模式选项
Browse files Browse the repository at this point in the history
  • Loading branch information
phk422 committed Dec 28, 2023
1 parent e0dd3df commit 85b1a68
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ Usage: tiny-app-cli [options]
自动提审与发布微信、支付宝小程序, 更好的实现小程序的CI/CD

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

示例
Expand Down
8 changes: 4 additions & 4 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import weixinRobot from '../weixin'
import { isEmpty } from '../utils'

export default async function main(options: InputOptions) {
let result: prompts.Answers<
const result: prompts.Answers<
'platform' | 'action'
> = await prompts(
[
Expand Down Expand Up @@ -36,12 +36,12 @@ export default async function main(options: InputOptions) {
},
},
)
result = {
const opts = {
...options,
...result,
}
if (result.platform === PLATFORM.WEIXIN)
await weixinRobot(result.action as ACTION)
if (opts.platform === PLATFORM.WEIXIN)
await weixinRobot(opts)
else
console.log(bgGreen('正在开发中...'))
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Option, program } from 'commander'
import { ACTION, PLATFORM, description, name, version } from './constants'
import main from './core'
import { handleOptions } from './utils'

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

program.parse()

const options = program.opts<InputOptions>()
const options = handleOptions(program.opts<InputOptions>())

main(options).catch((err) => {
console.error(err)
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { PuppeteerLaunchOptions } from 'puppeteer'
import type { ACTION, PLATFORM } from '../constants'

declare global {
interface InputOptions {
platform: PLATFORM
action: ACTION
headless: PuppeteerLaunchOptions['headless']
}
}
13 changes: 13 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,16 @@ export function isEmpty(value: unknown) {

return false
}

export function transBooleanStrToBool(value: string) {
if (value === 'true')
return true
if (value === 'false')
return false
return value
}

export function handleOptions(opts: InputOptions) {
opts.headless = transBooleanStrToBool(opts.headless as string) as never
return opts
}
11 changes: 6 additions & 5 deletions src/weixin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ let browser: Browser
let page: Page

let spinner: Ora
let options: InputOptions

/**
* 获取微信图片二维码
*/
export async function getLoginScanCode() {
export async function getLoginScanCode(opts: InputOptions = options) {
spinner = ora('正在获取登录二维码...').start()
browser = await puppeteer.launch({ headless: false })
browser = await puppeteer.launch({ headless: __DEV__ ? false : opts.headless })
page = await browser.newPage()
await page.setViewport(VIEWPORT)
await page.goto(WEIXIN_URL)
Expand Down Expand Up @@ -150,11 +151,12 @@ export async function toRelease() {
console.log('正在开发中...')
}

export default async function weixinRobot(action: ACTION) {
export default async function weixinRobot(opts: InputOptions) {
options = opts
try {
await getLoginScanCode()
await jumpToVersions()
if (action === ACTION.REVIEW) {
if (options.action === ACTION.REVIEW) {
await jumpToConfirmPage()
await toSubmitAudit()
}
Expand All @@ -164,7 +166,6 @@ export default async function weixinRobot(action: ACTION) {
process.exit(0)
}
catch (err) {
console.log(__DEV__)
if (__DEV__) {
console.error(err)
return
Expand Down

0 comments on commit 85b1a68

Please sign in to comment.