@@ -14,6 +14,8 @@ const cp = require('child_process');
14
14
15
15
const cleanup = ( ) => {
16
16
console . log ( 'Cleaning up.' ) ;
17
+ // Reset changes made to package.json files.
18
+ cp . execSync ( `git checkout -- packages/*/package.json` ) ;
17
19
// Uncomment when snapshot testing is enabled by default:
18
20
// rm ./template/src/__snapshots__/App.test.js.snap
19
21
} ;
@@ -25,7 +27,8 @@ const handleExit = () => {
25
27
} ;
26
28
27
29
const handleError = e => {
28
- console . error ( 'ERROR! An error was encountered while executing\n' , e ) ;
30
+ console . error ( 'ERROR! An error was encountered while executing' ) ;
31
+ console . error ( e ) ;
29
32
cleanup ( ) ;
30
33
console . log ( 'Exiting with error.' ) ;
31
34
process . exit ( 1 ) ;
@@ -34,69 +37,81 @@ const handleError = e => {
34
37
process . on ( 'SIGINT' , handleExit ) ;
35
38
process . on ( 'uncaughtException' , handleError ) ;
36
39
37
- // ******************************************************************************
38
- // Pack react- scripts so we can verify they work.
39
- // ******************************************************************************
40
+ console . log ( ) ;
41
+ console . log ( '-------------------------------------------------------' ) ;
42
+ console . log ( 'Assuming you have already run `yarn` to update the deps.' ) ;
43
+ console . log ( 'If not, remember to do this before testing!' ) ;
44
+ console . log ( '-------------------------------------------------------' ) ;
45
+ console . log ( ) ;
40
46
41
- const rootDir = path . join ( __dirname , '..' ) ;
42
- const reactScriptsDir = path . join ( rootDir , 'packages' , 'react-scripts' ) ;
43
- const packageJsonPath = path . join ( reactScriptsDir , 'package.json' ) ;
44
- const packageJsonOrigPath = path . join ( reactScriptsDir , 'package.json.orig' ) ;
45
-
46
- // Install all our packages
47
- const lernaPath = path . join ( rootDir , 'node_modules' , '.bin' , 'lerna' ) ;
48
- cp . execSync ( `${ lernaPath } bootstrap` , {
49
- cwd : rootDir ,
50
- stdio : 'inherit' ,
51
- } ) ;
47
+ // Temporarily overwrite package.json of all packages in monorepo
48
+ // to point to each other using absolute file:/ URLs.
52
49
53
- // Save package.json because we're going to touch it
54
- fs . writeFileSync ( packageJsonOrigPath , fs . readFileSync ( packageJsonPath ) ) ;
50
+ const gitStatus = cp . execSync ( `git status --porcelain` ) . toString ( ) ;
55
51
56
- // Replace own dependencies (those in the`packages` dir) with the local paths
57
- // of those packages
58
- const replaceOwnDepsPath = path . join ( __dirname , 'replace-own-deps.js' ) ;
59
- cp . execSync ( `node ${ replaceOwnDepsPath } ` , { stdio : 'inherit' } ) ;
52
+ if ( gitStatus . trim ( ) !== '' ) {
53
+ console . log ( 'Please commit your changes before running this script!' ) ;
54
+ console . log ( 'Exiting because `git status` is not empty:' ) ;
55
+ console . log ( ) ;
56
+ console . log ( gitStatus ) ;
57
+ console . log ( ) ;
58
+ process . exit ( 1 ) ;
59
+ }
60
+
61
+ const rootDir = path . join ( __dirname , '..' ) ;
62
+ const packagesDir = path . join ( rootDir , 'packages' ) ;
63
+ const packagePathsByName = { } ;
64
+ fs . readdirSync ( packagesDir ) . forEach ( name => {
65
+ const packageDir = path . join ( packagesDir , name ) ;
66
+ const packageJson = path . join ( packageDir , 'package.json' ) ;
67
+ if ( fs . existsSync ( packageJson ) ) {
68
+ packagePathsByName [ name ] = packageDir ;
69
+ }
70
+ } ) ;
71
+ Object . keys ( packagePathsByName ) . forEach ( name => {
72
+ const packageJson = path . join ( packagePathsByName [ name ] , 'package.json' ) ;
73
+ const json = JSON . parse ( fs . readFileSync ( packageJson , 'utf8' ) ) ;
74
+ Object . keys ( packagePathsByName ) . forEach ( otherName => {
75
+ if ( json . dependencies && json . dependencies [ otherName ] ) {
76
+ json . dependencies [ otherName ] = 'file:' + packagePathsByName [ otherName ] ;
77
+ }
78
+ if ( json . devDependencies && json . devDependencies [ otherName ] ) {
79
+ json . devDependencies [ otherName ] = 'file:' + packagePathsByName [ otherName ] ;
80
+ }
81
+ if ( json . peerDependencies && json . peerDependencies [ otherName ] ) {
82
+ json . peerDependencies [ otherName ] =
83
+ 'file:' + packagePathsByName [ otherName ] ;
84
+ }
85
+ if ( json . optionalDependencies && json . optionalDependencies [ otherName ] ) {
86
+ json . optionalDependencies [ otherName ] =
87
+ 'file:' + packagePathsByName [ otherName ] ;
88
+ }
89
+ } ) ;
90
+
91
+ fs . writeFileSync ( packageJson , JSON . stringify ( json , null , 2 ) , 'utf8' ) ;
92
+ console . log (
93
+ 'Replaced local dependencies in packages/' + name + '/package.json'
94
+ ) ;
95
+ } ) ;
96
+ console . log ( 'Replaced all local dependencies for testing.' ) ;
97
+ console . log ( 'Do not edit any package.json while this task is running.' ) ;
60
98
61
- // Finally, pack react-scripts
99
+ // Finally, pack react-scripts.
62
100
// Don't redirect stdio as we want to capture the output that will be returned
63
101
// from execSync(). In this case it will be the .tgz filename.
64
102
const scriptsFileName = cp
65
- . execSync ( `npm pack` , { cwd : reactScriptsDir } )
103
+ . execSync ( `npm pack` , { cwd : path . join ( packagesDir , 'react-scripts' ) } )
66
104
. toString ( )
67
105
. trim ( ) ;
68
- const scriptsPath = path . join (
69
- rootDir ,
70
- 'packages' ,
71
- 'react-scripts' ,
72
- scriptsFileName
73
- ) ;
74
-
75
- // Restore package.json
76
- fs . unlinkSync ( packageJsonPath ) ;
77
- fs . writeFileSync ( packageJsonPath , fs . readFileSync ( packageJsonOrigPath ) ) ;
78
- fs . unlinkSync ( packageJsonOrigPath ) ;
106
+ const scriptsPath = path . join ( packagesDir , 'react-scripts' , scriptsFileName ) ;
79
107
80
- // ******************************************************************************
81
108
// Now that we have packed them, call the global CLI.
82
- // ******************************************************************************
83
-
84
- // If Yarn is installed, clean its cache because it may have cached react-scripts
85
- try {
86
- cp . execSync ( 'yarn cache clean' ) ;
87
- } catch ( e ) {
88
- // We can safely ignore this as the user doesn't have yarn installed
89
- }
109
+ cp . execSync ( 'yarn cache clean' ) ;
90
110
91
111
const args = process . argv . slice ( 2 ) ;
92
112
93
113
// Now run the CRA command
94
- const craScriptPath = path . join (
95
- rootDir ,
96
- 'packages' ,
97
- 'create-react-app' ,
98
- 'index.js'
99
- ) ;
114
+ const craScriptPath = path . join ( packagesDir , 'create-react-app' , 'index.js' ) ;
100
115
cp . execSync (
101
116
`node ${ craScriptPath } --scripts-version="${ scriptsPath } " ${ args . join ( ' ' ) } ` ,
102
117
{
0 commit comments