@@ -25,6 +25,13 @@ const writeFileAtomic = require('write-file-atomic')
25
25
const unixFormatPath = require ( './utils/unix-format-path.js' )
26
26
const isRegistry = require ( './utils/is-registry.js' )
27
27
28
+ const { chown } = require ( 'fs' )
29
+ const inferOwner = require ( 'infer-owner' )
30
+ const selfOwner = {
31
+ uid : process . getuid && process . getuid ( ) ,
32
+ gid : process . getgid && process . getgid ( )
33
+ }
34
+
28
35
const PKGLOCK = 'package-lock.json'
29
36
const SHRINKWRAP = 'npm-shrinkwrap.json'
30
37
const PKGLOCK_VERSION = npm . lockfileVersion
@@ -217,13 +224,19 @@ function save (dir, pkginfo, opts, cb) {
217
224
log . verbose ( 'shrinkwrap' , `skipping write for ${ path . basename ( info . path ) } because there were no changes.` )
218
225
cb ( null , pkginfo )
219
226
} else {
220
- writeFileAtomic ( info . path , swdata , ( err ) => {
221
- if ( err ) return cb ( err )
222
- if ( opts . silent ) return cb ( null , pkginfo )
223
- if ( ! shrinkwrap && ! lockfile ) {
224
- log . notice ( '' , `created a lockfile as ${ path . basename ( info . path ) } . You should commit this file.` )
225
- }
226
- cb ( null , pkginfo )
227
+ inferOwner ( info . path ) . then ( owner => {
228
+ writeFileAtomic ( info . path , swdata , ( err ) => {
229
+ if ( err ) return cb ( err )
230
+ if ( opts . silent ) return cb ( null , pkginfo )
231
+ if ( ! shrinkwrap && ! lockfile ) {
232
+ log . notice ( '' , `created a lockfile as ${ path . basename ( info . path ) } . You should commit this file.` )
233
+ }
234
+ if ( selfOwner . uid === 0 && ( selfOwner . uid !== owner . uid || selfOwner . gid !== owner . gid ) ) {
235
+ chown ( info . path , owner . uid , owner . gid , er => cb ( er , pkginfo ) )
236
+ } else {
237
+ cb ( null , pkginfo )
238
+ }
239
+ } )
227
240
} )
228
241
}
229
242
}
0 commit comments