@@ -3,10 +3,13 @@ const { glob } = require('glob')
33const normalizePackageBin = require ( 'npm-normalize-package-bin' )
44const normalizePackageData = require ( 'normalize-package-data' )
55const path = require ( 'path' )
6+ const log = require ( 'proc-log' )
7+ const git = require ( '@npmcli/git' )
68
7- const normalize = async ( pkg , { strict, steps } ) => {
9+ const normalize = async ( pkg , { strict, steps, root } ) => {
810 const data = pkg . content
911 const scripts = data . scripts || { }
12+ const pkgId = `${ data . name ?? '' } @${ data . version ?? '' } `
1013
1114 // remove attributes that start with "_"
1215 if ( steps . includes ( '_attributes' ) ) {
@@ -20,7 +23,7 @@ const normalize = async (pkg, { strict, steps }) => {
2023 // build the "_id" attribute
2124 if ( steps . includes ( '_id' ) ) {
2225 if ( data . name && data . version ) {
23- data . _id = ` ${ data . name } @ ${ data . version } `
26+ data . _id = pkgId
2427 }
2528 }
2629
@@ -34,7 +37,9 @@ const normalize = async (pkg, { strict, steps }) => {
3437 // expand "bundleDependencies: true or translate from object"
3538 if ( steps . includes ( 'bundleDependencies' ) ) {
3639 const bd = data . bundleDependencies
37- if ( bd === true ) {
40+ if ( bd === false && ! steps . includes ( 'bundleDependenciesDeleteFalse' ) ) {
41+ data . bundleDependencies = [ ]
42+ } else if ( bd === true ) {
3843 data . bundleDependencies = Object . keys ( data . dependencies || { } )
3944 } else if ( bd && typeof bd === 'object' ) {
4045 if ( ! Array . isArray ( bd ) ) {
@@ -158,7 +163,7 @@ const normalize = async (pkg, { strict, steps }) => {
158163 }
159164
160165 // expand "directories.bin"
161- if ( steps . includes ( 'binDir' ) && data . directories ?. bin ) {
166+ if ( steps . includes ( 'binDir' ) && data . directories ?. bin && ! data . bin ) {
162167 const binsDir = path . resolve ( pkg . path , path . join ( '.' , path . join ( '/' , data . directories . bin ) ) )
163168 const bins = await glob ( '**' , { cwd : binsDir } )
164169 data . bin = bins . reduce ( ( acc , binFile ) => {
@@ -174,25 +179,28 @@ const normalize = async (pkg, { strict, steps }) => {
174179
175180 // populate "gitHead" attribute
176181 if ( steps . includes ( 'gitHead' ) && ! data . gitHead ) {
182+ const gitRoot = await git . find ( { cwd : pkg . path , root } )
177183 let head
178- try {
179- head = await fs . readFile ( path . resolve ( pkg . path , '.git/HEAD' ) , 'utf8' )
180- } catch ( err ) {
184+ if ( gitRoot ) {
185+ try {
186+ head = await fs . readFile ( path . resolve ( gitRoot , '.git/HEAD' ) , 'utf8' )
187+ } catch ( err ) {
181188 // do nothing
189+ }
182190 }
183191 let headData
184192 if ( head ) {
185193 if ( head . startsWith ( 'ref: ' ) ) {
186194 const headRef = head . replace ( / ^ r e f : / , '' ) . trim ( )
187- const headFile = path . resolve ( pkg . path , '.git' , headRef )
195+ const headFile = path . resolve ( gitRoot , '.git' , headRef )
188196 try {
189197 headData = await fs . readFile ( headFile , 'utf8' )
190198 headData = headData . replace ( / ^ r e f : / , '' ) . trim ( )
191199 } catch ( err ) {
192200 // do nothing
193201 }
194202 if ( ! headData ) {
195- const packFile = path . resolve ( pkg . path , '.git/packed-refs' )
203+ const packFile = path . resolve ( gitRoot , '.git/packed-refs' )
196204 try {
197205 let refs = await fs . readFile ( packFile , 'utf8' )
198206 if ( refs ) {
@@ -271,11 +279,11 @@ const normalize = async (pkg, { strict, steps }) => {
271279 // in normalize-package-data if it had access to the file path.
272280 if ( steps . includes ( 'binRefs' ) && data . bin instanceof Object ) {
273281 for ( const key in data . bin ) {
274- const binPath = path . resolve ( pkg . path , data . bin [ key ] )
275282 try {
276- await fs . access ( binPath )
283+ await fs . access ( path . resolve ( pkg . path , data . bin [ key ] ) )
277284 } catch {
278- delete data . bin [ key ]
285+ log . warn ( 'package-json' , pkgId , `No bin file found at ${ data . bin [ key ] } ` )
286+ // XXX: should a future breaking change delete bin entries that cannot be accessed?
279287 }
280288 }
281289 }
0 commit comments