Skip to content

Commit b3c96bd

Browse files
CyanChangesshigma
andcommitted
feat(market): support yarn json output (#294)
Co-authored-by: Shigma <[email protected]>
1 parent 498e486 commit b3c96bd

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

plugins/market/src/node/installer.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ export interface Dependency {
3232
latest?: string
3333
}
3434

35+
export interface YarnLog {
36+
type: 'warning' | 'info' | 'error' | string
37+
name: number | null
38+
displayName: string
39+
indent?: string
40+
data: string
41+
}
42+
43+
const levelMap = {
44+
'info': 'info',
45+
'warning': 'debug',
46+
'error': 'warn',
47+
}
48+
3549
export interface LocalPackage extends PackageJson {
3650
private?: boolean
3751
$workspace?: boolean
@@ -172,22 +186,40 @@ class Installer extends Service {
172186
}
173187

174188
async exec(command: string, args: string[]) {
189+
const useJson = command === 'yarn'
175190
return new Promise<number>((resolve) => {
191+
if (useJson) args.push('--json')
176192
const child = spawn(command, args, { cwd: this.cwd })
177193
child.on('exit', (code) => resolve(code))
178194
child.on('error', () => resolve(-1))
195+
196+
let stderr = ''
179197
child.stderr.on('data', (data) => {
180-
data = data.toString().trim()
181-
if (!data) return
182-
for (const line of data.split('\n')) {
198+
data = stderr + data.toString()
199+
const lines = data.split('\n')
200+
stderr = lines.pop()!
201+
for (const line of lines) {
183202
logger.warn(line)
184203
}
185204
})
205+
206+
let stdout = ''
186207
child.stdout.on('data', (data) => {
187-
data = data.toString().trim()
188-
if (!data) return
189-
for (const line of data.split('\n')) {
190-
logger.info(line)
208+
data = stdout + data.toString()
209+
const lines = data.split('\n')
210+
stdout = lines.pop()!
211+
for (const line of lines) {
212+
if (!useJson) {
213+
logger.info(line)
214+
continue
215+
}
216+
try {
217+
const { type, data } = JSON.parse(line) as YarnLog
218+
logger[levelMap[type] ?? 'info'](data)
219+
} catch (error) {
220+
logger.warn(line)
221+
logger.warn(error)
222+
}
191223
}
192224
})
193225
})

0 commit comments

Comments
 (0)