11import { existsSync , mkdirSync , rmSync , writeFileSync } from 'node:fs'
22import path from 'node:path'
33
4+ import semver from 'semver'
45import { globSync as tinyGlobSync } from 'tinyglobby'
56
67import { toSortedObject } from '@socketsecurity/registry/lib/objects'
@@ -22,6 +23,8 @@ import {
2223
2324const {
2425 BABEL_RUNTIME ,
26+ CYCLONEDX_CDXGEN ,
27+ SYNP ,
2528 ROLLUP_EXTERNAL_SUFFIX ,
2629 depStatsPath,
2730 rootDistPath,
@@ -52,13 +55,20 @@ function updateDepStatsSync(depStats) {
5255 const oldDepStats = existsSync ( depStatsPath )
5356 ? readJsonSync ( depStatsPath )
5457 : undefined
58+ const oldDeps = oldDepStats ?. dependencies
5559 Object . assign ( depStats . dependencies , {
5660 // Manually add @cyclonedx /cdxgen and synp as they are not directly
5761 // referenced in the code but used through spawned processes.
58- '@cyclonedx/cdxgen' : pkgJson . dependencies [ '@cyclonedx/cdxgen' ] ,
59- synp : pkgJson . dependencies . synp ,
60- // Assign old dep stats dependencies to preserve them.
61- ...oldDepStats ?. dependencies
62+ [ CYCLONEDX_CDXGEN ] : pkgJson . dependencies [ CYCLONEDX_CDXGEN ] ,
63+ [ SYNP ] : pkgJson . dependencies [ SYNP ] ,
64+ ...Object . fromEntries (
65+ // Assign old dep stats dependencies to preserve them.
66+ Object . entries ( oldDeps ?? { } ) . filter ( ( { 0 : key , 1 : oldSpec } ) => {
67+ // Skip old deps that are replaced with higher versions.
68+ const s = depStats . dependencies [ key ]
69+ return ! s || semver . gt ( semver . coerce ( oldSpec ) , semver . coerce ( s ) )
70+ } )
71+ )
6272 } )
6373 // Remove transitives from dependencies.
6474 for ( const key of Object . keys ( oldDepStats ?. transitives ?? { } ) ) {
0 commit comments