Skip to content

Commit

Permalink
feat(market): support yarn json output (#294)
Browse files Browse the repository at this point in the history
Co-authored-by: Shigma <[email protected]>
  • Loading branch information
CyanChanges and shigma committed Jan 31, 2024
1 parent 498e486 commit b3c96bd
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions plugins/market/src/node/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ export interface Dependency {
latest?: string
}

export interface YarnLog {
type: 'warning' | 'info' | 'error' | string
name: number | null
displayName: string
indent?: string
data: string
}

const levelMap = {
'info': 'info',
'warning': 'debug',
'error': 'warn',
}

export interface LocalPackage extends PackageJson {
private?: boolean
$workspace?: boolean
Expand Down Expand Up @@ -172,22 +186,40 @@ class Installer extends Service {
}

async exec(command: string, args: string[]) {
const useJson = command === 'yarn'
return new Promise<number>((resolve) => {
if (useJson) args.push('--json')
const child = spawn(command, args, { cwd: this.cwd })
child.on('exit', (code) => resolve(code))
child.on('error', () => resolve(-1))

let stderr = ''
child.stderr.on('data', (data) => {
data = data.toString().trim()
if (!data) return
for (const line of data.split('\n')) {
data = stderr + data.toString()
const lines = data.split('\n')
stderr = lines.pop()!
for (const line of lines) {
logger.warn(line)
}
})

let stdout = ''
child.stdout.on('data', (data) => {
data = data.toString().trim()
if (!data) return
for (const line of data.split('\n')) {
logger.info(line)
data = stdout + data.toString()
const lines = data.split('\n')
stdout = lines.pop()!
for (const line of lines) {
if (!useJson) {
logger.info(line)
continue
}
try {
const { type, data } = JSON.parse(line) as YarnLog
logger[levelMap[type] ?? 'info'](data)
} catch (error) {
logger.warn(line)
logger.warn(error)
}
}
})
})
Expand Down

0 comments on commit b3c96bd

Please sign in to comment.