1+ import { existsSync , readFileSync } from "node:fs" ;
12import { pathToFileURL } from "node:url" ;
23import { consola } from "consola" ;
34import { resolveModulePath } from "exsolve" ;
5+ import { dirname , join } from "pathe" ;
46import { isCI , isTest } from "std-env" ;
57
68export interface DepOptions {
@@ -10,7 +12,10 @@ export interface DepOptions {
1012 dir : string ;
1113 /** Human readable reason, used in prompts and errors. */
1214 reason : string ;
13- /** Version range used when installing (ignored if it is not a simple range). */
15+ /**
16+ * Supported version range, used when installing (ignored if it is not a simple range)
17+ * and checked against the version of an already installed package.
18+ */
1419 version ?: string ;
1520 /** Install as a dev dependency. (default: `true`) */
1621 dev ?: boolean ;
@@ -29,6 +34,7 @@ export async function ensureDep(opts: DepOptions, _retry?: boolean): Promise<str
2934 } ) ;
3035
3136 if ( resolved ) {
37+ _checkVersion ( opts , resolved ) ;
3238 return resolved ;
3339 }
3440
@@ -119,3 +125,61 @@ export async function ensureLibDeps(
119125 }
120126 }
121127}
128+
129+ const _versionWarnings = new Set < string > ( ) ;
130+
131+ /** Warn once if the installed version of a dependency is outside of its supported range. */
132+ function _checkVersion ( opts : DepOptions , entry : string ) : void {
133+ if ( ! opts . version || _versionWarnings . has ( `${ opts . id } @${ opts . version } ` ) ) {
134+ return ;
135+ }
136+ const version = _installedVersion ( opts . id , entry ) ;
137+ if ( ! version || _isSupportedVersion ( version , opts . version ) ) {
138+ return ;
139+ }
140+ _versionWarnings . add ( `${ opts . id } @${ opts . version } ` ) ;
141+ consola . warn (
142+ `\`${ opts . id } @${ version } \` is installed in \`${ opts . dir } \` but ${ opts . reason } requires \`${ opts . version } \`. Please update it if you run into issues.`
143+ ) ;
144+ }
145+
146+ /** Read the version of the package `entry` belongs to (walking up to its `package.json`). */
147+ function _installedVersion ( id : string , entry : string ) : string | undefined {
148+ let dir = dirname ( entry ) ;
149+ for ( let depth = 0 ; depth < 10 ; depth ++ ) {
150+ const pkgPath = join ( dir , "package.json" ) ;
151+ if ( existsSync ( pkgPath ) ) {
152+ try {
153+ const pkg = JSON . parse ( readFileSync ( pkgPath , "utf8" ) ) ;
154+ if ( pkg . name === id ) {
155+ return typeof pkg . version === "string" ? pkg . version : undefined ;
156+ }
157+ } catch {
158+ // Ignore unreadable or invalid `package.json` files
159+ }
160+ }
161+ const parent = dirname ( dir ) ;
162+ if ( parent === dir ) {
163+ break ;
164+ }
165+ dir = parent ;
166+ }
167+ }
168+
169+ /**
170+ * Check a version against a simple caret range (`^8`, `^7 || ^8`).
171+ *
172+ * Ranges in any other format (and `0.x` versions, for which caret is minor scoped) are not
173+ * checked. Prerelease versions match their own major (`8.0.0-beta.1` satisfies `^8`).
174+ */
175+ function _isSupportedVersion ( version : string , range : string ) : boolean {
176+ const major = Number . parseInt ( version , 10 ) ;
177+ if ( ! major ) {
178+ return true ;
179+ }
180+ const ranges = range . split ( "||" ) . map ( ( r ) => r . trim ( ) ) ;
181+ if ( ! ranges . every ( ( r ) => / ^ \^ \d + ( \. \d + ) * $ / . test ( r ) ) ) {
182+ return true ;
183+ }
184+ return ranges . some ( ( r ) => Number . parseInt ( r . slice ( 1 ) , 10 ) === major ) ;
185+ }
0 commit comments