9
9
prepareSkipList ,
10
10
SkipList ,
11
11
} from '../core/default-skip-list' ;
12
- import { expandFolders , findPackageJsonFiles } from '../utils/package-info' ;
12
+ import { findPackageJsonFiles } from '../utils/package-info' ;
13
13
import { getConfigContext } from './configuration-context' ;
14
14
15
15
let inferVersion = false ;
@@ -70,7 +70,27 @@ function readVersionMap(packagePath: string): VersionMap {
70
70
return versions ;
71
71
}
72
72
73
- function lookupVersion ( key : string , versions : VersionMap ) : string {
73
+ function lookupVersion ( key : string , projectRoot : string , workspaceRoot : string ) : string {
74
+
75
+ const packageJsonList = findPackageJsonFiles ( projectRoot , workspaceRoot ) ;
76
+
77
+ for ( const packagePath of packageJsonList ) {
78
+
79
+ const versionMap = readVersionMap ( packagePath ) ;
80
+ const version = lookupVersionInMap ( key , versionMap ) ;
81
+
82
+ if ( version ) {
83
+ return version ;
84
+ }
85
+
86
+ }
87
+
88
+ throw new Error (
89
+ `Shared Dependency ${ key } has requiredVersion:'auto'. However, this dependency is not found in your package.json`
90
+ ) ;
91
+ }
92
+
93
+ function lookupVersionInMap ( key : string , versions : VersionMap ) : string | null {
74
94
const parts = key . split ( '/' ) ;
75
95
if ( parts . length >= 2 && parts [ 0 ] . startsWith ( '@' ) ) {
76
96
key = parts [ 0 ] + '/' + parts [ 1 ] ;
@@ -83,9 +103,7 @@ function lookupVersion(key: string, versions: VersionMap): string {
83
103
}
84
104
85
105
if ( ! versions [ key ] ) {
86
- throw new Error (
87
- `Shared Dependency ${ key } has requiredVersion:'auto'. However, this dependency is not found in your package.json`
88
- ) ;
106
+ return null ;
89
107
}
90
108
return versions [ key ] ;
91
109
}
@@ -225,17 +243,16 @@ function readConfiguredSecondaries(
225
243
export function shareAll (
226
244
config : CustomSharedConfig = { } ,
227
245
skip : SkipList = DEFAULT_SKIP_LIST ,
228
- projectPath = '' ,
229
- workspacePath = '' ,
246
+ projectPath = ''
230
247
) : Config | null {
231
248
249
+ let workspacePath : string | undefined = undefined ;
250
+
232
251
if ( ! projectPath ) {
233
252
projectPath = cwd ( ) ;
234
253
}
235
254
236
- if ( ! workspacePath ) {
237
- workspacePath = getConfigContext ( ) . workspaceRoot ?? '' ;
238
- }
255
+ workspacePath = getConfigContext ( ) . workspaceRoot ?? '' ;
239
256
240
257
if ( ! workspacePath ) {
241
258
workspacePath = projectPath ;
@@ -272,14 +289,22 @@ export function setInferVersion(infer: boolean): void {
272
289
inferVersion = infer ;
273
290
}
274
291
275
- export function share ( shareObjects : Config , packageJsonPath = '' ) : Config {
276
- if ( ! packageJsonPath ) {
277
- packageJsonPath = cwd ( ) ;
292
+ export function share ( shareObjects : Config , projectPath = '' ) : Config {
293
+ if ( ! projectPath ) {
294
+ projectPath = cwd ( ) ;
278
295
}
279
296
280
- const packagePath = findPackageJson ( packageJsonPath ) ;
297
+ let workspacePath : string | undefined = undefined ;
298
+
299
+ workspacePath = getConfigContext ( ) . workspaceRoot ?? '' ;
300
+
301
+ if ( ! workspacePath ) {
302
+ workspacePath = projectPath ;
303
+ }
304
+
305
+ const packagePath = findPackageJson ( projectPath ) ;
281
306
282
- const versions = readVersionMap ( packagePath ) ;
307
+ // const versions = readVersionMap(packagePath);
283
308
const result : any = { } ;
284
309
let includeSecondaries ;
285
310
@@ -291,7 +316,8 @@ export function share(shareObjects: Config, packageJsonPath = ''): Config {
291
316
shareObject . requiredVersion === 'auto' ||
292
317
( inferVersion && typeof shareObject . requiredVersion === 'undefined' )
293
318
) {
294
- const version = lookupVersion ( key , versions ) ;
319
+ const version = lookupVersion ( key , projectPath , workspacePath ) ;
320
+
295
321
shareObject . requiredVersion = version ;
296
322
shareObject . version = version . replace ( / ^ \D * / , '' ) ;
297
323
}
0 commit comments