1
+ /* eslint-disable no-console */
1
2
const {
2
3
getInternals,
3
4
npmView
@@ -14,38 +15,115 @@ const latestsPath = path.join(
14
15
'helpers' ,
15
16
'latests.json'
16
17
)
17
- const latestsJson = require ( latestsPath )
18
+
19
+ // Get internal package names from existing getInternals helper
18
20
const internalsNames = Array . from ( new Set ( getInternals ( ) . map ( n => n . name ) ) )
19
21
. filter ( x => typeof x === 'string' && x !== 'child_process' && ! x . startsWith ( 'node:' ) )
20
22
21
- // TODO A lot of this can be optimized by using `npm outdated`.
23
+ // Initial structure with placeholder for pinned packages
24
+ const initialStructure = {
25
+ pinned : [ 'ENTER_PACKAGE_NAME_HERE' ] ,
26
+ latests : { }
27
+ }
22
28
29
+ /**
30
+ * Updates latests.json with the current latest versions from npm
31
+ */
23
32
async function fix ( ) {
33
+ console . log ( 'Starting fix operation...' )
34
+ console . log ( `Found ${ internalsNames . length } packages to process` )
35
+
36
+ let outputData = initialStructure
37
+ if ( fs . existsSync ( latestsPath ) ) {
38
+ console . log ( 'Found existing latests.json, loading it...' )
39
+ outputData = require ( latestsPath )
40
+ }
41
+
24
42
const latests = { }
43
+ let processed = 0
44
+ const total = internalsNames . length
45
+
25
46
for ( const name of internalsNames ) {
26
- const distTags = await npmView ( name + ' dist-tags' )
27
- const latest = distTags . latest
28
- latests [ name ] = latest
47
+ processed ++
48
+ process . stdout . write ( `Processing package ${ processed } /${ total } : ${ name } ...` )
49
+
50
+ try {
51
+ const distTags = await npmView ( name + ' dist-tags' )
52
+ const latest = distTags . latest
53
+ if ( latest ) {
54
+ latests [ name ] = latest
55
+ process . stdout . write ( ` found version ${ latest } \n` )
56
+ } else {
57
+ process . stdout . write ( ' WARNING: no version found\n' )
58
+ console . log ( `Warning: Could not fetch latest version for "${ name } "` )
59
+ }
60
+ } catch ( error ) {
61
+ process . stdout . write ( ' ERROR\n' )
62
+ console . error ( `Error fetching version for "${ name } ":` , error . message )
63
+ }
29
64
}
30
- latestsJson . latests = latests
31
- fs . writeFileSync ( latestsPath , JSON . stringify ( latestsJson , null , 2 ) )
65
+
66
+ outputData . latests = latests
67
+ console . log ( '\nWriting updated versions to latests.json...' )
68
+ fs . writeFileSync ( latestsPath , JSON . stringify ( outputData , null , 2 ) )
69
+ console . log ( 'Successfully updated latests.json' )
70
+ console . log ( `Processed ${ total } packages` )
32
71
}
33
72
73
+ /**
74
+ * Checks if latests.json matches current npm versions
75
+ */
34
76
async function check ( ) {
77
+ console . log ( 'Starting version check...' )
78
+
79
+ if ( ! fs . existsSync ( latestsPath ) ) {
80
+ console . log ( 'latests.json does not exist. Run with "fix" to create it.' )
81
+ process . exitCode = 1
82
+ return
83
+ }
84
+
85
+ const currentData = require ( latestsPath )
86
+ console . log ( `Found ${ internalsNames . length } packages to check` )
87
+
88
+ let processed = 0
89
+ let mismatches = 0
90
+ const total = internalsNames . length
91
+
35
92
for ( const name of internalsNames ) {
36
- const latest = latestsJson . latests [ name ]
93
+ processed ++
94
+ process . stdout . write ( `Checking package ${ processed } /${ total } : ${ name } ...` )
95
+
96
+ const latest = currentData . latests [ name ]
37
97
if ( ! latest ) {
98
+ process . stdout . write ( ' MISSING\n' )
38
99
console . log ( `No latest version found for "${ name } "` )
39
100
process . exitCode = 1
101
+ continue
40
102
}
41
- const distTags = await npmView ( name + ' dist-tags' )
42
- const npmLatest = distTags . latest
43
- if ( npmLatest !== latest ) {
44
- console . log ( `"latests.json: is not up to date for "${ name } ": expected "${ npmLatest } ", got "${ latest } "` )
45
- process . exitCode = 1
103
+
104
+ try {
105
+ const distTags = await npmView ( name + ' dist-tags' )
106
+ const npmLatest = distTags . latest
107
+ if ( npmLatest !== latest ) {
108
+ process . stdout . write ( ' MISMATCH\n' )
109
+ console . log ( `"latests.json: is not up to date for "${ name } ": expected "${ npmLatest } ", got "${ latest } "` )
110
+ process . exitCode = 1
111
+ mismatches ++
112
+ } else {
113
+ process . stdout . write ( ' OK\n' )
114
+ }
115
+ } catch ( error ) {
116
+ process . stdout . write ( ' ERROR\n' )
117
+ console . error ( `Error checking version for "${ name } ":` , error . message )
46
118
}
47
119
}
48
- }
49
120
121
+ console . log ( '\nCheck completed:' )
122
+ console . log ( `- Total packages checked: ${ total } ` )
123
+ console . log ( `- Version mismatches found: ${ mismatches } ` )
124
+ if ( mismatches > 0 ) {
125
+ console . log ( 'Run with "fix" to update versions' )
126
+ }
127
+ }
50
128
if ( process . argv . includes ( 'fix' ) ) fix ( )
51
129
else check ( )
0 commit comments