@@ -3,10 +3,13 @@ const { glob } = require('glob')
3
3
const normalizePackageBin = require ( 'npm-normalize-package-bin' )
4
4
const normalizePackageData = require ( 'normalize-package-data' )
5
5
const path = require ( 'path' )
6
+ const log = require ( 'proc-log' )
7
+ const git = require ( '@npmcli/git' )
6
8
7
- const normalize = async ( pkg , { strict, steps } ) => {
9
+ const normalize = async ( pkg , { strict, steps, root } ) => {
8
10
const data = pkg . content
9
11
const scripts = data . scripts || { }
12
+ const pkgId = `${ data . name ?? '' } @${ data . version ?? '' } `
10
13
11
14
// remove attributes that start with "_"
12
15
if ( steps . includes ( '_attributes' ) ) {
@@ -20,7 +23,7 @@ const normalize = async (pkg, { strict, steps }) => {
20
23
// build the "_id" attribute
21
24
if ( steps . includes ( '_id' ) ) {
22
25
if ( data . name && data . version ) {
23
- data . _id = ` ${ data . name } @ ${ data . version } `
26
+ data . _id = pkgId
24
27
}
25
28
}
26
29
@@ -34,7 +37,9 @@ const normalize = async (pkg, { strict, steps }) => {
34
37
// expand "bundleDependencies: true or translate from object"
35
38
if ( steps . includes ( 'bundleDependencies' ) ) {
36
39
const bd = data . bundleDependencies
37
- if ( bd === true ) {
40
+ if ( bd === false && ! steps . includes ( 'bundleDependenciesDeleteFalse' ) ) {
41
+ data . bundleDependencies = [ ]
42
+ } else if ( bd === true ) {
38
43
data . bundleDependencies = Object . keys ( data . dependencies || { } )
39
44
} else if ( bd && typeof bd === 'object' ) {
40
45
if ( ! Array . isArray ( bd ) ) {
@@ -158,7 +163,7 @@ const normalize = async (pkg, { strict, steps }) => {
158
163
}
159
164
160
165
// expand "directories.bin"
161
- if ( steps . includes ( 'binDir' ) && data . directories ?. bin ) {
166
+ if ( steps . includes ( 'binDir' ) && data . directories ?. bin && ! data . bin ) {
162
167
const binsDir = path . resolve ( pkg . path , path . join ( '.' , path . join ( '/' , data . directories . bin ) ) )
163
168
const bins = await glob ( '**' , { cwd : binsDir } )
164
169
data . bin = bins . reduce ( ( acc , binFile ) => {
@@ -174,25 +179,28 @@ const normalize = async (pkg, { strict, steps }) => {
174
179
175
180
// populate "gitHead" attribute
176
181
if ( steps . includes ( 'gitHead' ) && ! data . gitHead ) {
182
+ const gitRoot = await git . find ( { cwd : pkg . path , root } )
177
183
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 ) {
181
188
// do nothing
189
+ }
182
190
}
183
191
let headData
184
192
if ( head ) {
185
193
if ( head . startsWith ( 'ref: ' ) ) {
186
194
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 )
188
196
try {
189
197
headData = await fs . readFile ( headFile , 'utf8' )
190
198
headData = headData . replace ( / ^ r e f : / , '' ) . trim ( )
191
199
} catch ( err ) {
192
200
// do nothing
193
201
}
194
202
if ( ! headData ) {
195
- const packFile = path . resolve ( pkg . path , '.git/packed-refs' )
203
+ const packFile = path . resolve ( gitRoot , '.git/packed-refs' )
196
204
try {
197
205
let refs = await fs . readFile ( packFile , 'utf8' )
198
206
if ( refs ) {
@@ -271,11 +279,11 @@ const normalize = async (pkg, { strict, steps }) => {
271
279
// in normalize-package-data if it had access to the file path.
272
280
if ( steps . includes ( 'binRefs' ) && data . bin instanceof Object ) {
273
281
for ( const key in data . bin ) {
274
- const binPath = path . resolve ( pkg . path , data . bin [ key ] )
275
282
try {
276
- await fs . access ( binPath )
283
+ await fs . access ( path . resolve ( pkg . path , data . bin [ key ] ) )
277
284
} 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?
279
287
}
280
288
}
281
289
}
0 commit comments