11
11
const path = require ( 'path' ) ;
12
12
const fs = require ( 'fs' ) ;
13
13
const url = require ( 'url' ) ;
14
- const findMonorepo = require ( 'react-dev-utils/workspaceUtils' ) . findMonorepo ;
14
+ const findPkg = require ( 'find-pkg' ) ;
15
+ const globby = require ( 'globby' ) ;
15
16
16
17
// Make sure any symlinks in the project folder are resolved:
17
18
// https://github.com/facebook/create-react-app/issues/637
@@ -57,6 +58,7 @@ module.exports = {
57
58
appIndexJs : resolveApp ( 'src/index.js' ) ,
58
59
appPackageJson : resolveApp ( 'package.json' ) ,
59
60
appSrc : resolveApp ( 'src' ) ,
61
+ yarnLockFile : resolveApp ( 'yarn.lock' ) ,
60
62
testsSetup : resolveApp ( 'src/setupTests.js' ) ,
61
63
appNodeModules : resolveApp ( 'node_modules' ) ,
62
64
publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
@@ -78,6 +80,7 @@ module.exports = {
78
80
appIndexJs : resolveApp ( 'src/index.js' ) ,
79
81
appPackageJson : resolveApp ( 'package.json' ) ,
80
82
appSrc : resolveApp ( 'src' ) ,
83
+ yarnLockFile : resolveApp ( 'yarn.lock' ) ,
81
84
testsSetup : resolveApp ( 'src/setupTests.js' ) ,
82
85
appNodeModules : resolveApp ( 'node_modules' ) ,
83
86
publicUrl : getPublicUrl ( resolveApp ( 'package.json' ) ) ,
@@ -103,6 +106,7 @@ if (useTemplate) {
103
106
appIndexJs : resolveOwn ( 'template/src/index.js' ) ,
104
107
appPackageJson : resolveOwn ( 'package.json' ) ,
105
108
appSrc : resolveOwn ( 'template/src' ) ,
109
+ yarnLockFile : resolveOwn ( 'template/yarn.lock' ) ,
106
110
testsSetup : resolveOwn ( 'template/src/setupTests.js' ) ,
107
111
appNodeModules : resolveOwn ( 'node_modules' ) ,
108
112
publicUrl : getPublicUrl ( resolveOwn ( 'package.json' ) ) ,
@@ -116,16 +120,40 @@ if (useTemplate) {
116
120
117
121
module . exports . srcPaths = [ module . exports . appSrc ] ;
118
122
119
- module . exports . useYarn = fs . existsSync (
120
- path . join ( module . exports . appPath , 'yarn.lock' )
121
- ) ;
123
+ const findPkgs = ( rootPath , globPatterns ) => {
124
+ const globOpts = {
125
+ cwd : rootPath ,
126
+ strict : true ,
127
+ absolute : true ,
128
+ } ;
129
+ return globPatterns
130
+ . reduce (
131
+ ( pkgs , pattern ) =>
132
+ pkgs . concat ( globby . sync ( path . join ( pattern , 'package.json' ) , globOpts ) ) ,
133
+ [ ]
134
+ )
135
+ . map ( f => path . dirname ( path . normalize ( f ) ) ) ;
136
+ } ;
137
+
138
+ const getMonorepoPkgPaths = ( ) => {
139
+ const monoPkgPath = findPkg . sync ( path . resolve ( appDirectory , '..' ) ) ;
140
+ if ( monoPkgPath ) {
141
+ // get monorepo config from yarn workspace
142
+ const pkgPatterns = require ( monoPkgPath ) . workspaces ;
143
+ if ( pkgPatterns == null ) {
144
+ return [ ] ;
145
+ }
146
+ const pkgPaths = findPkgs ( path . dirname ( monoPkgPath ) , pkgPatterns ) ;
147
+ // only include monorepo pkgs if app itself is included in monorepo
148
+ if ( pkgPaths . indexOf ( appDirectory ) !== - 1 ) {
149
+ return pkgPaths . filter ( f => fs . realpathSync ( f ) !== appDirectory ) ;
150
+ }
151
+ }
152
+ return [ ] ;
153
+ } ;
122
154
123
155
if ( checkForMonorepo ) {
124
156
// if app is in a monorepo (lerna or yarn workspace), treat other packages in
125
157
// the monorepo as if they are app source
126
- const mono = findMonorepo ( appDirectory ) ;
127
- if ( mono . isAppIncluded ) {
128
- Array . prototype . push . apply ( module . exports . srcPaths , mono . pkgs ) ;
129
- }
130
- module . exports . useYarn = module . exports . useYarn || mono . isYarnWs ;
158
+ Array . prototype . push . apply ( module . exports . srcPaths , getMonorepoPkgPaths ( ) ) ;
131
159
}
0 commit comments