1
+ const finder = require ( "find-package-json" ) ;
2
+ const path = require ( "path" ) ;
3
+ const fs = require ( "fs" ) ;
4
+
1
5
const AutomaticVendorFederation = ( {
2
- exclude,
3
- ignoreVersion,
4
- packageJson,
5
- ignorePatchVersion = true ,
6
- shareFrom = [ "dependencies" ] ,
7
- } ) => {
6
+ exclude,
7
+ ignoreVersion,
8
+ packageJson,
9
+ ignorePatchVersion = true ,
10
+ shareFrom = [ "dependencies" ] ,
11
+ } ) => {
8
12
let combinedDependencies ;
9
13
if ( ! packageJson ) {
10
14
throw new Error (
@@ -28,17 +32,38 @@ const AutomaticVendorFederation = ({
28
32
}
29
33
) ;
30
34
return shareableDependencies . reduce ( ( shared , pkg ) => {
31
- let packageVersion = require ( pkg + "/package.json" ) . version . split ( "." ) ;
32
- if ( ignorePatchVersion ) {
33
- packageVersion . pop ( ) ;
34
- }
35
- if ( ignoreVersion && ignoreVersion . includes ( pkg ) ) {
36
- Object . assign ( shared , { [ pkg ] : pkg } ) ;
37
- } else {
38
- Object . assign ( shared , { [ `${ pkg } -${ packageVersion . join ( "." ) } ` ] : pkg } ) ;
35
+ let packageVersion ;
36
+ try {
37
+ const packageExists = fs . existsSync (
38
+ path . join ( path . dirname ( require . resolve ( pkg ) ) , "/package.json" )
39
+ ) ;
40
+ if ( packageExists ) {
41
+ console . log ( 'package exists' )
42
+ const resolvedPackage = path . join (
43
+ path . dirname ( require . resolve ( pkg ) ) ,
44
+ "/package.json"
45
+ ) ;
46
+ packageVersion = require ( resolvedPackage ) . version . split ( "." ) ;
47
+ } else {
48
+ console . log ( "searching for package" ) ;
49
+ const f = finder ( path . dirname ( require . resolve ( pkg ) ) ) ;
50
+ const jsonValue = f . next ( ) . value ;
51
+ packageVersion = require ( f . next ( ) . filename ) . version . split ( "." ) ;
52
+ }
53
+ console . log ( packageVersion ) ;
54
+ if ( ignorePatchVersion ) {
55
+ packageVersion . pop ( ) ;
56
+ }
57
+ if ( ignoreVersion && ignoreVersion . includes ( pkg ) ) {
58
+ Object . assign ( shared , { [ pkg ] : pkg } ) ;
59
+ } else {
60
+ Object . assign ( shared , { [ `${ pkg } -${ packageVersion . join ( "." ) } ` ] : pkg } ) ;
61
+ }
62
+ } catch ( e ) {
63
+ return shared
39
64
}
40
-
41
65
return shared ;
42
66
} , { } ) ;
43
67
} ;
68
+
44
69
module . exports = AutomaticVendorFederation ;
0 commit comments