1+ const finder = require ( "find-package-json" ) ;
2+ const path = require ( "path" ) ;
3+ const fs = require ( "fs" ) ;
4+
15const 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+ } ) => {
812 let combinedDependencies ;
913 if ( ! packageJson ) {
1014 throw new Error (
@@ -28,17 +32,38 @@ const AutomaticVendorFederation = ({
2832 }
2933 ) ;
3034 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
3964 }
40-
4165 return shared ;
4266 } , { } ) ;
4367} ;
68+
4469module . exports = AutomaticVendorFederation ;
0 commit comments