11const semver = require ( 'semver' )
22const fs = require ( 'fs/promises' )
33const { glob } = require ( 'glob' )
4- const normalizePackageBin = require ( 'npm-normalize-package-bin' )
54const legacyFixer = require ( 'normalize-package-data/lib/fixer.js' )
65const legacyMakeWarning = require ( 'normalize-package-data/lib/make_warning.js' )
76const path = require ( 'path' )
87const log = require ( 'proc-log' )
98const git = require ( '@npmcli/git' )
109const hostedGitInfo = require ( 'hosted-git-info' )
1110
11+ // used to be npm-normalize-package-bin
12+ function normalizePackageBin ( pkg , changes ) {
13+ if ( pkg . bin ) {
14+ if ( typeof pkg . bin === 'string' && pkg . name ) {
15+ changes ?. push ( '"bin" was converted to an object' )
16+ pkg . bin = { [ pkg . name ] : pkg . bin }
17+ } else if ( Array . isArray ( pkg . bin ) ) {
18+ changes ?. push ( '"bin" was converted to an object' )
19+ pkg . bin = pkg . bin . reduce ( ( acc , k ) => {
20+ acc [ path . basename ( k ) ] = k
21+ return acc
22+ } , { } )
23+ }
24+ if ( typeof pkg . bin === 'object' ) {
25+ for ( const binKey in pkg . bin ) {
26+ if ( typeof pkg . bin [ binKey ] !== 'string' ) {
27+ delete pkg . bin [ binKey ]
28+ changes ?. push ( `removed invalid "bin[${ binKey } ]"` )
29+ continue
30+ }
31+ const base = path . join ( '/' , path . basename ( binKey . replace ( / \\ | : / g, '/' ) ) ) . slice ( 1 )
32+ if ( ! base ) {
33+ delete pkg . bin [ binKey ]
34+ changes ?. push ( `removed invalid "bin[${ binKey } ]"` )
35+ continue
36+ }
37+
38+ const binTarget = path . join ( '/' , pkg . bin [ binKey ] . replace ( / \\ / g, '/' ) )
39+ . replace ( / \\ / g, '/' ) . slice ( 1 )
40+
41+ if ( ! binTarget ) {
42+ delete pkg . bin [ binKey ]
43+ changes ?. push ( `removed invalid "bin[${ binKey } ]"` )
44+ continue
45+ }
46+
47+ if ( base !== binKey ) {
48+ delete pkg . bin [ binKey ]
49+ changes ?. push ( `"bin[${ binKey } ]" was renamed to "bin[${ base } ]"` )
50+ }
51+ if ( binTarget !== pkg . bin [ binKey ] ) {
52+ changes ?. push ( `"bin[${ base } ]" script name was cleaned` )
53+ }
54+ pkg . bin [ base ] = binTarget
55+ }
56+
57+ if ( Object . keys ( pkg . bin ) . length === 0 ) {
58+ changes ?. push ( 'empty "bin" was removed' )
59+ delete pkg . bin
60+ }
61+
62+ return pkg
63+ }
64+ }
65+ delete pkg . bin
66+ }
67+
1268function isCorrectlyEncodedName ( spec ) {
1369 return ! spec . match ( / [ / @ \s + % : ] / ) &&
1470 spec === encodeURIComponent ( spec )
@@ -257,7 +313,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
257313 }
258314
259315 if ( steps . includes ( 'bin' ) || steps . includes ( 'binDir' ) || steps . includes ( 'binRefs' ) ) {
260- normalizePackageBin ( data )
316+ normalizePackageBin ( data , changes )
261317 }
262318
263319 // expand "directories.bin"
@@ -272,7 +328,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
272328 return acc
273329 } , { } )
274330 // *sigh*
275- normalizePackageBin ( data )
331+ normalizePackageBin ( data , changes )
276332 }
277333
278334 // populate "gitHead" attribute
0 commit comments