@@ -32,6 +32,20 @@ export interface Dependency {
32
32
latest ?: string
33
33
}
34
34
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
+
35
49
export interface LocalPackage extends PackageJson {
36
50
private ?: boolean
37
51
$workspace ?: boolean
@@ -172,22 +186,40 @@ class Installer extends Service {
172
186
}
173
187
174
188
async exec ( command : string , args : string [ ] ) {
189
+ const useJson = command === 'yarn'
175
190
return new Promise < number > ( ( resolve ) => {
191
+ if ( useJson ) args . push ( '--json' )
176
192
const child = spawn ( command , args , { cwd : this . cwd } )
177
193
child . on ( 'exit' , ( code ) => resolve ( code ) )
178
194
child . on ( 'error' , ( ) => resolve ( - 1 ) )
195
+
196
+ let stderr = ''
179
197
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 ) {
183
202
logger . warn ( line )
184
203
}
185
204
} )
205
+
206
+ let stdout = ''
186
207
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
+ }
191
223
}
192
224
} )
193
225
} )
0 commit comments