11
11
const path = require ( 'path' ) ;
12
12
const fs = require ( 'fs' ) ;
13
13
const url = require ( 'url' ) ;
14
+ const findPkg = require ( 'find-pkg' ) ;
15
+ const globby = require ( 'globby' ) ;
14
16
15
17
// Make sure any symlinks in the project folder are resolved:
16
18
// https://github.com/facebook/create-react-app/issues/637
@@ -92,6 +94,8 @@ module.exports = {
92
94
servedPath : getServedPath ( resolveApp ( 'package.json' ) ) ,
93
95
} ;
94
96
97
+ let checkForMonorepo = true ;
98
+
95
99
// @remove -on-eject-begin
96
100
const resolveOwn = relativePath => path . resolve ( __dirname , '..' , relativePath ) ;
97
101
@@ -119,17 +123,13 @@ module.exports = {
119
123
ownTypeDeclarations : resolveOwn ( 'lib/react-app.d.ts' ) ,
120
124
} ;
121
125
122
- const ownPackageJson = require ( '../package.json' ) ;
123
- const reactScriptsPath = resolveApp ( `node_modules/${ ownPackageJson . name } ` ) ;
124
- const reactScriptsLinked =
125
- fs . existsSync ( reactScriptsPath ) &&
126
- fs . lstatSync ( reactScriptsPath ) . isSymbolicLink ( ) ;
127
-
128
- // config before publish: we're in ./packages/react-scripts/config/
129
- if (
130
- ! reactScriptsLinked &&
131
- __dirname . indexOf ( path . join ( 'packages' , 'react-scripts' , 'config' ) ) !== - 1
132
- ) {
126
+ // detect if template should be used, ie. when cwd is react-scripts itself
127
+ const useTemplate =
128
+ appDirectory === fs . realpathSync ( path . join ( __dirname , '..' ) ) ;
129
+
130
+ checkForMonorepo = ! useTemplate ;
131
+
132
+ if ( useTemplate ) {
133
133
module . exports = {
134
134
dotenv : resolveOwn ( 'template/.env' ) ,
135
135
appPath : resolveApp ( '.' ) ,
@@ -156,3 +156,40 @@ if (
156
156
// @remove -on-eject-end
157
157
158
158
module . exports . moduleFileExtensions = moduleFileExtensions ;
159
+
160
+ module . exports . srcPaths = [ module . exports . appSrc ] ;
161
+
162
+ const findPkgs = ( rootPath , globPatterns ) => {
163
+ const globOpts = {
164
+ cwd : rootPath ,
165
+ strict : true ,
166
+ absolute : true ,
167
+ } ;
168
+ return globPatterns
169
+ . reduce (
170
+ ( pkgs , pattern ) =>
171
+ pkgs . concat ( globby . sync ( path . join ( pattern , 'package.json' ) , globOpts ) ) ,
172
+ [ ]
173
+ )
174
+ . map ( f => path . dirname ( path . normalize ( f ) ) ) ;
175
+ } ;
176
+
177
+ const getMonorepoPkgPaths = ( ) => {
178
+ const monoPkgPath = findPkg . sync ( path . resolve ( appDirectory , '..' ) ) ;
179
+ if ( monoPkgPath ) {
180
+ // get monorepo config from yarn workspace
181
+ const pkgPatterns = require ( monoPkgPath ) . workspaces ;
182
+ const pkgPaths = findPkgs ( path . dirname ( monoPkgPath ) , pkgPatterns ) ;
183
+ // only include monorepo pkgs if app itself is included in monorepo
184
+ if ( pkgPaths . indexOf ( appDirectory ) !== - 1 ) {
185
+ return pkgPaths . filter ( f => fs . realpathSync ( f ) !== appDirectory ) ;
186
+ }
187
+ }
188
+ return [ ] ;
189
+ } ;
190
+
191
+ if ( checkForMonorepo ) {
192
+ // if app is in a monorepo (lerna or yarn workspace), treat other packages in
193
+ // the monorepo as if they are app source
194
+ Array . prototype . push . apply ( module . exports . srcPaths , getMonorepoPkgPaths ( ) ) ;
195
+ }
0 commit comments