|
1 | | -import type { ExecaReturnBase } from 'execa' |
2 | 1 | import { $ } from 'execa' |
3 | 2 | import type { Options } from '../types' |
4 | | -import { parseArgs } from '../utils' |
| 3 | +import { decodeGbk, parseArgs } from '../utils' |
5 | 4 |
|
6 | 5 | export interface PublishAppResourcePayload { |
7 | 6 | /** |
@@ -122,19 +121,48 @@ export interface PublishH5Payload { |
122 | 121 | } |
123 | 122 |
|
124 | 123 | 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> |
130 | 129 | } |
131 | 130 |
|
132 | 131 | export function createPublishContext(options: Options) { |
133 | 132 | const cli = `${options.cli}` |
134 | 133 | const publish: PublishFunc = ( |
135 | 134 | payload: PublishAppResourcePayload | PublishWgtPayload | PublishMpWeixinPayload | PublishH5Payload, |
136 | 135 | ) => { |
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 | + }) |
138 | 166 | } |
139 | 167 |
|
140 | 168 | // add alias |
|
0 commit comments