Skip to content

Commit dbe980e

Browse files
committed
test: add publish test
1 parent 4729472 commit dbe980e

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/actions/pack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function createPackContext(options: Options) {
6868
}
6969
if (hasTask) {
7070
kill()
71-
reject(new Error('cli 暂不支持重新排队,请等待云端打包完成后重试,或使用 HBuildX 编辑器打包'))
71+
reject(new Error('cli 暂不支持重新排队,请等待云端打包完成后重试,或使用 HBuilder X 编辑器打包'))
7272
}
7373
const matches = optput.matchAll(/:\s(https:\/\/.*?)\s/gm)
7474
const match = [...matches].map(v => v[1])[0]

src/actions/publish.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { ExecaReturnBase } from 'execa'
21
import { $ } from 'execa'
32
import type { Options } from '../types'
4-
import { parseArgs } from '../utils'
3+
import { decodeGbk, parseArgs } from '../utils'
54

65
export interface PublishAppResourcePayload {
76
/**
@@ -122,19 +121,48 @@ export interface PublishH5Payload {
122121
}
123122

124123
export interface PublishFunc {
125-
(payload: PublishAppResourcePayload | PublishWgtPayload | PublishMpWeixinPayload | PublishH5Payload): ExecaReturnBase<string>
126-
appResource: (payload: Omit<PublishAppResourcePayload, 'type' | 'platform'>) => ExecaReturnBase<string>
127-
wgt: (payload: Omit<PublishWgtPayload, 'type' | 'platform'>) => ExecaReturnBase<string>
128-
mpWeixin: (payload: Omit<PublishMpWeixinPayload, 'platform'>) => ExecaReturnBase<string>
129-
h5: (payload: Omit<PublishH5Payload, 'platform'>) => ExecaReturnBase<string>
124+
(payload: PublishAppResourcePayload | PublishWgtPayload | PublishMpWeixinPayload | PublishH5Payload): Promise<string>
125+
appResource: (payload: Omit<PublishAppResourcePayload, 'type' | 'platform'>) => Promise<string>
126+
wgt: (payload: Omit<PublishWgtPayload, 'type' | 'platform'>) => Promise<string>
127+
mpWeixin: (payload: Omit<PublishMpWeixinPayload, 'platform'>) => Promise<string>
128+
h5: (payload: Omit<PublishH5Payload, 'platform'>) => Promise<string>
130129
}
131130

132131
export function createPublishContext(options: Options) {
133132
const cli = `${options.cli}`
134133
const publish: PublishFunc = (
135134
payload: PublishAppResourcePayload | PublishWgtPayload | PublishMpWeixinPayload | PublishH5Payload,
136135
) => {
137-
return $.sync`${cli} open ${parseArgs(payload)}`
136+
const { stdout } = $({
137+
encoding: null,
138+
buffer: true,
139+
stdin: 'pipe',
140+
})`${cli} publish ${parseArgs(payload)}`
141+
return new Promise<string>((resolve, reject) => {
142+
let outputDir = ''
143+
stdout?.on('data', (data) => {
144+
const optput = decodeGbk(data)
145+
const isFail = optput.includes('失败.')
146+
const isSuccess = optput.includes('成功,路径为')
147+
if (isFail) {
148+
reject(optput)
149+
}
150+
151+
if (isSuccess) {
152+
const matches = optput.matchAll(/(.*?)\s/gm)
153+
const match = [...matches].map(v => v[1])[0]
154+
if (match) {
155+
outputDir = match
156+
}
157+
}
158+
159+
// eslint-disable-next-line no-console
160+
console.log(`${optput}`)
161+
})
162+
stdout?.on('close', () => {
163+
resolve(outputDir)
164+
})
165+
})
138166
}
139167

140168
// add alias

test/actions/publish.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { createPublishContext } from '../../src/actions/publish'
3+
import { resolveOptions } from '../../src/options'
4+
5+
const options = resolveOptions({})
6+
const publish = createPublishContext(options)
7+
8+
describe('publish action', () => {
9+
it.skip('publish h5', async () => {
10+
const output = await publish.h5({
11+
project: 'test-pack',
12+
})
13+
14+
expect(output).toContain('/test-pack/unpackage/dist/build/h5')
15+
}, 1000 * 30)
16+
})

0 commit comments

Comments
 (0)