5
5
} = require ( './helpers/versioning' )
6
6
const path = require ( 'path' )
7
7
const fs = require ( 'fs' )
8
+ const semver = require ( 'semver' )
8
9
9
10
const latestsPath = path . join (
10
11
__dirname ,
@@ -20,15 +21,58 @@ const latestsPath = path.join(
20
21
const internalsNames = Array . from ( new Set ( getInternals ( ) . map ( n => n . name ) ) )
21
22
. filter ( x => typeof x === 'string' && x !== 'child_process' && ! x . startsWith ( 'node:' ) )
22
23
23
- // Initial structure with placeholder for pinned packages
24
+ // Initial structure with placeholder for configuration
24
25
const initialStructure = {
25
26
pinned : [ 'ENTER_PACKAGE_NAME_HERE' ] ,
27
+ onlyUseLatestTag : [ 'ENTER_PACKAGE_NAME_HERE' ] ,
26
28
latests : { }
27
29
}
28
30
29
31
/**
30
- * Updates latests.json with the current latest versions from npm
31
- */
32
+ * Gets the highest version that's compatible with our instrumentation
33
+ * This handles cases where a package has newer versions that might break compatibility
34
+ */
35
+ async function getHighestCompatibleVersion ( name , config = { } ) {
36
+ try {
37
+ // Get all distribution tags (including 'latest')
38
+ const distTags = await npmView ( name + ' dist-tags' )
39
+
40
+ // Get the latest tagged version
41
+ const latestTagged = distTags . latest
42
+
43
+ if ( ! latestTagged ) {
44
+ console . log ( `Warning: Could not fetch latest version for "${ name } "` )
45
+ return null
46
+ }
47
+
48
+ // If package is in the onlyUseLatestTag list, always use the 'latest' tag
49
+ if ( config . onlyUseLatestTag && config . onlyUseLatestTag . includes ( name ) ) {
50
+ return latestTagged
51
+ }
52
+
53
+ // Get all available versions
54
+ const allVersions = await npmView ( name + ' versions' )
55
+
56
+ // Find the highest non-prerelease version available
57
+ const stableVersions = allVersions . filter ( v => ! semver . prerelease ( v ) )
58
+ const highestStableVersion = stableVersions . sort ( semver . compare ) . pop ( )
59
+
60
+ // Use the highest stable version if it's greater than the latest tag
61
+ if ( highestStableVersion && semver . gt ( highestStableVersion , latestTagged ) ) {
62
+ process . stdout . write ( ` found version ${ highestStableVersion } (higher than 'latest' tag ${ latestTagged } )` )
63
+ return highestStableVersion
64
+ }
65
+
66
+ return latestTagged
67
+ } catch ( error ) {
68
+ console . error ( `Error fetching version for "${ name } ":` , error . message )
69
+ return null
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Updates latests.json with the current latest versions from npm
75
+ */
32
76
async function fix ( ) {
33
77
console . log ( 'Starting fix operation...' )
34
78
console . log ( `Found ${ internalsNames . length } packages to process` )
@@ -48,11 +92,10 @@ async function fix () {
48
92
process . stdout . write ( `Processing package ${ processed } /${ total } : ${ name } ...` )
49
93
50
94
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` )
95
+ const latestVersion = await getHighestCompatibleVersion ( name , outputData )
96
+ if ( latestVersion ) {
97
+ latests [ name ] = latestVersion
98
+ process . stdout . write ( ` found version ${ latestVersion } \n` )
56
99
} else {
57
100
process . stdout . write ( ' WARNING: no version found\n' )
58
101
console . log ( `Warning: Could not fetch latest version for "${ name } "` )
@@ -71,8 +114,8 @@ async function fix () {
71
114
}
72
115
73
116
/**
74
- * Checks if latests.json matches current npm versions
75
- */
117
+ * Checks if latests.json matches current npm versions
118
+ */
76
119
async function check ( ) {
77
120
console . log ( 'Starting version check...' )
78
121
@@ -102,11 +145,16 @@ async function check () {
102
145
}
103
146
104
147
try {
105
- const distTags = await npmView ( name + ' dist-tags' )
106
- const npmLatest = distTags . latest
107
- if ( npmLatest !== latest ) {
148
+ const latestVersion = await getHighestCompatibleVersion ( name , currentData )
149
+ if ( ! latestVersion ) {
150
+ process . stdout . write ( ' ERROR\n' )
151
+ console . error ( `Error fetching latest version for "${ name } "` )
152
+ continue
153
+ }
154
+
155
+ if ( latestVersion !== latest ) {
108
156
process . stdout . write ( ' MISMATCH\n' )
109
- console . log ( `"latests.json: is not up to date for "${ name } ": expected "${ npmLatest } ", got "${ latest } "` )
157
+ console . log ( `"latests.json: is not up to date for "${ name } ": expected "${ latestVersion } ", got "${ latest } "` )
110
158
process . exitCode = 1
111
159
mismatches ++
112
160
} else {
0 commit comments