1
1
'use strict'
2
2
3
- const fs = require ( 'node:fs/promises' )
4
3
const Module = require ( 'node:module' )
5
4
const path = require ( 'node:path' )
6
5
7
6
const pacote = require ( 'pacote' )
7
+ const semver = require ( 'semver' )
8
8
const validateNpmPackageName = require ( 'validate-npm-package-name' )
9
9
10
10
const constants = require ( '@socketsecurity/registry/lib/constants' )
11
+ const { writeJson } = require ( '@socketsecurity/registry/lib/fs' )
11
12
const { logger } = require ( '@socketsecurity/registry/lib/logger' )
12
13
const { pFilter } = require ( '@socketsecurity/registry/lib/promises' )
13
14
const { confirm } = require ( '@socketsecurity/registry/lib/prompts' )
@@ -22,6 +23,34 @@ const npmBuiltinNamesJsonPath = path.join(npmDataPath, 'builtin-names.json')
22
23
const npmLegacyNamesJsonPath = path . join ( npmDataPath , 'legacy-names.json' )
23
24
24
25
void ( async ( ) => {
26
+ // Lazily access constants.spinner.
27
+ const { spinner } = constants
28
+
29
+ spinner . start ( )
30
+
31
+ // Lazily access constants.maintainedNodeVersions.
32
+ const { next } = constants . maintainedNodeVersions
33
+ const nodeVersion = process . version . slice ( 1 )
34
+ const isGteNext = semver . gte ( nodeVersion , next )
35
+ if (
36
+ await confirm ( {
37
+ message : `Update builtin package names?${ isGteNext ? '' : ` (Requires Node >=${ next } )` } ` ,
38
+ default : true
39
+ } )
40
+ ) {
41
+ const nodeVersion = process . version . slice ( 1 )
42
+ if ( isGteNext ) {
43
+ const builtinNames = Module . builtinModules
44
+ // Node 23 introduces 'node:sea', 'node:sqlite', 'node:test', and 'node:test/reporters'
45
+ // that have no unprefixed version so we skip them.
46
+ . filter ( n => ! n . startsWith ( 'node:' ) )
47
+ . sort ( naturalCompare )
48
+ await writeJson ( npmBuiltinNamesJsonPath , builtinNames , { spaces : 2 } )
49
+ } else {
50
+ spinner . warn ( `Skipping... (Running ${ nodeVersion } )` )
51
+ }
52
+ }
53
+
25
54
if (
26
55
! ( await confirm ( {
27
56
message : 'Update npm package names data?' ,
@@ -30,16 +59,6 @@ void (async () => {
30
59
) {
31
60
return
32
61
}
33
- // Lazily access constants.spinner.
34
- const { spinner } = constants
35
-
36
- spinner . start ( )
37
-
38
- const builtinNames = Module . builtinModules
39
- // Node 23 introduces 'node:sea', 'node:sqlite', 'node:test', and 'node:test/reporters'
40
- // that have no unprefixed version so we skip them.
41
- . filter ( n => ! n . startsWith ( 'node:' ) )
42
- . sort ( naturalCompare )
43
62
const allThePackageNames = [
44
63
...new Set ( [
45
64
// Load the 43.1MB names.json file of '[email protected] '
@@ -81,17 +100,9 @@ void (async () => {
81
100
} ,
82
101
{ retries : 4 , signal : abortSignal }
83
102
)
84
- spinner . setText ( 'Writing json files...' )
85
- await Promise . all (
86
- [
87
- { json : builtinNames , path : npmBuiltinNamesJsonPath } ,
88
- { json : legacyNames , path : npmLegacyNamesJsonPath }
89
- ] . map ( d =>
90
- fs . writeFile ( d . path , `${ JSON . stringify ( d . json , null , 2 ) } \n` , 'utf8' )
91
- )
92
- )
103
+ await writeJson ( npmLegacyNamesJsonPath , legacyNames , { spaces : 2 } )
93
104
spinner . stop ( )
94
105
if ( invalidNames . size ) {
95
- logger . warn ( ` Removed missing packages:` , [ ...invalidNames ] )
106
+ logger . warn ( ' Removed missing packages:' , [ ...invalidNames ] )
96
107
}
97
108
} ) ( )
0 commit comments