1
1
const valid = require ( 'semver/functions/valid' )
2
2
const clean = require ( 'semver/functions/clean' )
3
3
const fs = require ( 'fs/promises' )
4
- const { glob } = require ( 'glob' )
5
4
const path = require ( 'path' )
6
5
const log = require ( 'proc-log' )
7
- const hostedGitInfo = require ( 'hosted-git-info' )
6
+
7
+ /**
8
+ * @type {import('hosted-git-info') }
9
+ */
10
+ let _hostedGitInfo
11
+ function lazyHostedGitInfo ( ) {
12
+ if ( ! _hostedGitInfo ) {
13
+ _hostedGitInfo = require ( 'hosted-git-info' )
14
+ }
15
+ return _hostedGitInfo
16
+ }
17
+
18
+ /**
19
+ * @type {import('glob').glob }
20
+ */
21
+ let _glob
22
+ function lazyLoadGlob ( ) {
23
+ if ( ! _glob ) {
24
+ _glob = require ( 'glob' ) . glob
25
+ }
26
+ return _glob
27
+ }
8
28
9
29
// used to be npm-normalize-package-bin
10
30
function normalizePackageBin ( pkg , changes ) {
@@ -206,7 +226,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
206
226
// add "install" attribute if any "*.gyp" files exist
207
227
if ( steps . includes ( 'gypfile' ) ) {
208
228
if ( ! scripts . install && ! scripts . preinstall && data . gypfile !== false ) {
209
- const files = await glob ( '*.gyp' , { cwd : pkg . path } )
229
+ const files = await lazyLoadGlob ( ) ( '*.gyp' , { cwd : pkg . path } )
210
230
if ( files . length ) {
211
231
scripts . install = 'node-gyp rebuild'
212
232
data . scripts = scripts
@@ -273,7 +293,11 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
273
293
// populate "readme" attribute
274
294
if ( steps . includes ( 'readme' ) && ! data . readme ) {
275
295
const mdre = / \. m ? a ? r ? k ? d ? o ? w ? n ? $ / i
276
- const files = await glob ( '{README,README.*}' , { cwd : pkg . path , nocase : true , mark : true } )
296
+ const files = await lazyLoadGlob ( ) ( '{README,README.*}' , {
297
+ cwd : pkg . path ,
298
+ nocase : true ,
299
+ mark : true ,
300
+ } )
277
301
let readmeFile
278
302
for ( const file of files ) {
279
303
// don't accept directories.
@@ -304,7 +328,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
304
328
if ( steps . includes ( 'mans' ) && ! data . man && data . directories ?. man ) {
305
329
const manDir = data . directories . man
306
330
const cwd = path . resolve ( pkg . path , manDir )
307
- const files = await glob ( '**/*.[0-9]' , { cwd } )
331
+ const files = await lazyLoadGlob ( ) ( '**/*.[0-9]' , { cwd } )
308
332
data . man = files . map ( man =>
309
333
path . relative ( pkg . path , path . join ( cwd , man ) ) . split ( path . sep ) . join ( '/' )
310
334
)
@@ -317,7 +341,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
317
341
// expand "directories.bin"
318
342
if ( steps . includes ( 'binDir' ) && data . directories ?. bin && ! data . bin ) {
319
343
const binsDir = path . resolve ( pkg . path , path . join ( '.' , path . join ( '/' , data . directories . bin ) ) )
320
- const bins = await glob ( '**' , { cwd : binsDir } )
344
+ const bins = await lazyLoadGlob ( ) ( '**' , { cwd : binsDir } )
321
345
data . bin = bins . reduce ( ( acc , binFile ) => {
322
346
if ( binFile && ! binFile . startsWith ( '.' ) ) {
323
347
const binName = path . basename ( binFile )
@@ -445,7 +469,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
445
469
}
446
470
}
447
471
if ( data . repository . url ) {
448
- const hosted = hostedGitInfo . fromUrl ( data . repository . url )
472
+ const hosted = lazyHostedGitInfo ( ) . fromUrl ( data . repository . url )
449
473
let r
450
474
if ( hosted ) {
451
475
if ( hosted . getDefaultRepresentation ( ) === 'shortcut' ) {
@@ -505,7 +529,7 @@ const normalize = async (pkg, { strict, steps, root, changes, allowLegacyCase })
505
529
changes ?. push ( `Removed invalid "${ deps } .${ d } "` )
506
530
delete data [ deps ] [ d ]
507
531
}
508
- const hosted = hostedGitInfo . fromUrl ( data [ deps ] [ d ] ) ?. toString ( )
532
+ const hosted = lazyHostedGitInfo ( ) . fromUrl ( data [ deps ] [ d ] ) ?. toString ( )
509
533
if ( hosted && hosted !== data [ deps ] [ d ] ) {
510
534
changes ?. push ( `Normalized git reference to "${ deps } .${ d } "` )
511
535
data [ deps ] [ d ] = hosted . toString ( )
0 commit comments