1+ let npm // set by the cli
12let cbCalled = false
23const log = require ( 'npmlog' )
3- const npm = require ( '../npm.js' )
44let itWorked = false
55const path = require ( 'path' )
6+ const writeFileAtomic = require ( 'write-file-atomic' )
7+ const mkdirp = require ( 'mkdirp-infer-owner' )
8+ const fs = require ( 'graceful-fs' )
69let wroteLogFile = false
710let exitCode = 0
811const errorMessage = require ( './error-message.js' )
912const replaceInfo = require ( './replace-info.js' )
1013
11- const cacheFile = require ( './cache-file.js' )
12-
1314let logFileName
1415const getLogFile = ( ) => {
16+ // we call this multiple times, so we need to treat it as a singleton because
17+ // the date is part of the name
1518 if ( ! logFileName )
1619 logFileName = path . resolve ( npm . config . get ( 'cache' ) , '_logs' , ( new Date ( ) ) . toISOString ( ) . replace ( / [ . : ] / g, '_' ) + '-debug.log' )
1720
1821 return logFileName
1922}
2023
21- const timings = {
22- version : npm . version ,
23- command : process . argv . slice ( 2 ) ,
24- logfile : null ,
25- }
24+ const timings = { }
2625process . on ( 'timing' , ( name , value ) => {
2726 if ( timings [ name ] )
2827 timings [ name ] += value
@@ -35,9 +34,21 @@ process.on('exit', code => {
3534 log . disableProgress ( )
3635 if ( npm . config && npm . config . loaded && npm . config . get ( 'timing' ) ) {
3736 try {
38- timings . logfile = getLogFile ( )
39- cacheFile . append ( '_timing.json' , JSON . stringify ( timings ) + '\n' )
40- } catch ( _ ) {
37+ const file = path . resolve ( npm . config . get ( 'cache' ) , '_timing.json' )
38+ const dir = path . dirname ( npm . config . get ( 'cache' ) )
39+ mkdirp . sync ( dir )
40+
41+ fs . appendFileSync ( file , JSON . stringify ( {
42+ command : process . argv . slice ( 2 ) ,
43+ logfile : getLogFile ( ) ,
44+ version : npm . version ,
45+ ...timings ,
46+ } ) + '\n' )
47+
48+ const st = fs . lstatSync ( path . dirname ( npm . config . get ( 'cache' ) ) )
49+ fs . chownSync ( dir , st . uid , st . gid )
50+ fs . chownSync ( file , st . uid , st . gid )
51+ } catch ( ex ) {
4152 // ignore
4253 }
4354 }
@@ -174,7 +185,7 @@ const errorHandler = (er) => {
174185 log . error ( k , v )
175186 }
176187
177- const msg = errorMessage ( er )
188+ const msg = errorMessage ( er , npm )
178189 for ( const errline of [ ...msg . summary , ...msg . detail ] )
179190 log . error ( ...errline )
180191
@@ -214,7 +225,15 @@ const writeLogFile = () => {
214225 logOutput += line + os . EOL
215226 } )
216227 } )
217- cacheFile . write ( getLogFile ( ) , logOutput )
228+
229+ const file = getLogFile ( )
230+ const dir = path . dirname ( file )
231+ mkdirp . sync ( dir )
232+ writeFileAtomic . sync ( file , logOutput )
233+
234+ const st = fs . lstatSync ( path . dirname ( npm . config . get ( 'cache' ) ) )
235+ fs . chownSync ( dir , st . uid , st . gid )
236+ fs . chownSync ( file , st . uid , st . gid )
218237
219238 // truncate once it's been written.
220239 log . record . length = 0
@@ -226,3 +245,6 @@ const writeLogFile = () => {
226245
227246module . exports = errorHandler
228247module . exports . exit = exit
248+ module . exports . setNpm = ( n ) => {
249+ npm = n
250+ }
0 commit comments