@@ -2,6 +2,7 @@ export * as Npm from "./npm"
22
33import path from "path"
44import npa from "npm-package-arg"
5+ import semver from "semver"
56import { Effect , Schema , Context , Layer , Option , FileSystem } from "effect"
67import { NodeFileSystem } from "@effect/platform-node"
78import { FSUtil } from "./fs-util"
@@ -117,19 +118,20 @@ const layer = Layer.effect(
117118 // so compare the cached version against the registry before reusing it.
118119 // Returns true when the cache is stale and a reinstall is needed; any
119120 // registry or read failure conservatively keeps the cached copy.
120- const isDistTagOutdated = Effect . fn ( "Npm.isDistTagOutdated" ) ( function * ( name : string , target : string ) {
121- const cached = yield * afs . readJson ( path . join ( target , "package.json" ) ) . pipe (
122- Effect . orElseSucceed ( ( ) => undefined ) ,
123- )
124- const cachedVersion =
125- typeof cached === "object" && cached !== null && typeof ( cached as any ) . version === "string"
126- ? ( ( cached as any ) . version as string )
127- : undefined
121+ const isDistTagOutdated = Effect . fn ( "Npm.isDistTagOutdated" ) ( function * ( dir : string , name : string , target : string ) {
122+ const decodePackage = Schema . decodeUnknownOption ( Schema . Struct ( { version : Schema . String } ) )
123+ const json = yield * afs . readJson ( path . join ( target , "package.json" ) ) . pipe ( Effect . option )
124+ if ( Option . isNone ( json ) ) return true
125+ const decoded = decodePackage ( json . value )
126+ const cachedVersion = Option . isSome ( decoded ) ? decoded . value . version : undefined
128127 if ( ! cachedVersion ) return true
129128
129+ // Resolve the registry from the same npm config `reify` uses so custom
130+ // registries and enterprise mirrors are honored.
131+ const base = yield * NpmConfig . registry ( dir )
130132 const encoded = name . includes ( "/" ) ? name . replace ( "/" , "%2f" ) : name
131133 const res = yield * Effect . promise ( ( ) =>
132- fetch ( `https://registry.npmjs.org /${ encoded } /latest` , {
134+ fetch ( `${ base } /${ encoded } /latest` , {
133135 headers : { Accept : "application/vnd.npm.install-v1+json" } ,
134136 signal : AbortSignal . timeout ( 10_000 ) ,
135137 } ) . catch ( ( ) => undefined ) ,
@@ -143,6 +145,11 @@ const layer = Layer.effect(
143145 )
144146 const latestVersion = data ?. version
145147 if ( ! latestVersion ) return false
148+ // Semver-equivalent spellings ("v1.0.0", "1.0.0+build") must not trigger
149+ // pointless reinstalls.
150+ if ( semver . valid ( latestVersion ) && semver . valid ( cachedVersion ) ) {
151+ return ! semver . eq ( latestVersion , cachedVersion )
152+ }
146153 return latestVersion !== cachedVersion
147154 } )
148155
@@ -160,7 +167,7 @@ const layer = Layer.effect(
160167
161168 if ( yield * afs . existsSafe ( target ) ) {
162169 if ( parsed ?. type === "tag" ) {
163- if ( ! ( yield * isDistTagOutdated ( name , target ) ) ) return resolveEntryPoint ( name , target )
170+ if ( ! ( yield * isDistTagOutdated ( dir , name , target ) ) ) return resolveEntryPoint ( name , target )
164171 } else {
165172 return resolveEntryPoint ( name , target )
166173 }
0 commit comments