11const os = require ( 'os' )
2+ const fs = require ( '@npmcli/fs' )
23
34const log = require ( './log-shim.js' )
45const errorMessage = require ( './error-message.js' )
@@ -18,11 +19,10 @@ process.on('exit', code => {
1819 // unfinished timer check below
1920 process . emit ( 'timeEnd' , 'npm' )
2021
21- const hasNpm = ! ! npm
22- const hasLoadedNpm = hasNpm && npm . config . loaded
22+ const hasLoadedNpm = npm ?. config . loaded
2323
2424 // Unfinished timers can be read before config load
25- if ( hasNpm ) {
25+ if ( npm ) {
2626 for ( const [ name , timer ] of npm . unfinishedTimers ) {
2727 log . verbose ( 'unfinished npm timer' , name , timer )
2828 }
@@ -111,10 +111,9 @@ const exitHandler = err => {
111111
112112 log . disableProgress ( )
113113
114- const hasNpm = ! ! npm
115- const hasLoadedNpm = hasNpm && npm . config . loaded
114+ const hasLoadedNpm = npm ?. config . loaded
116115
117- if ( ! hasNpm ) {
116+ if ( ! npm ) {
118117 err = err || new Error ( 'Exit prior to setting npm in exit handler' )
119118 // eslint-disable-next-line no-console
120119 console . error ( err . stack || err . message )
@@ -181,17 +180,33 @@ const exitHandler = err => {
181180 }
182181 }
183182
184- const msg = errorMessage ( err , npm )
185- for ( const errline of [ ...msg . summary , ...msg . detail ] ) {
183+ const { summary, detail, files = [ ] } = errorMessage ( err , npm )
184+
185+ for ( let [ file , content ] of files ) {
186+ file = `${ npm . logPath } ${ file } `
187+ content = `'Log files:\n${ npm . logFiles . join ( '\n' ) } \n\n${ content . trim ( ) } \n`
188+ try {
189+ fs . withOwnerSync (
190+ file ,
191+ ( ) => fs . writeFileSync ( file , content ) ,
192+ { owner : 'inherit' }
193+ )
194+ detail . push ( [ '' , `\n\nFor a full report see:\n${ file } ` ] )
195+ } catch ( err ) {
196+ log . warn ( '' , `Could not write error message to ${ file } due to ${ err } ` )
197+ }
198+ }
199+
200+ for ( const errline of [ ...summary , ...detail ] ) {
186201 log . error ( ...errline )
187202 }
188203
189204 if ( hasLoadedNpm && npm . config . get ( 'json' ) ) {
190205 const error = {
191206 error : {
192207 code : err . code ,
193- summary : messageText ( msg . summary ) ,
194- detail : messageText ( msg . detail ) ,
208+ summary : messageText ( summary ) ,
209+ detail : messageText ( detail ) ,
195210 } ,
196211 }
197212 npm . outputError ( JSON . stringify ( error , null , 2 ) )
0 commit comments