1
+ #! /bin/bash
2
+ # Copyright (c) 2015-present, Facebook, Inc.
3
+ #
4
+ # This source code is licensed under the MIT license found in the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ # ******************************************************************************
8
+ # This is an end-to-end test intended to run on CI.
9
+ # You can also run it locally but it's slow.
10
+ # ******************************************************************************
11
+
12
+ # Start in tasks/ even if run from root directory
13
+ cd " $( dirname " $0 " ) "
14
+
15
+ # CLI and app temporary locations
16
+ # http://unix.stackexchange.com/a/84980
17
+ temp_app_path=` mktemp -d 2> /dev/null || mktemp -d -t ' temp_app_path' `
18
+ custom_registry_url=http://localhost:4873
19
+ original_npm_registry_url=` npm get registry`
20
+ original_yarn_registry_url=` yarn config get registry`
21
+
22
+ function cleanup {
23
+ echo ' Cleaning up.'
24
+ cd " $root_path "
25
+ rm -rf " $temp_app_path "
26
+ npm set registry " $original_npm_registry_url "
27
+ yarn config set registry " $original_yarn_registry_url "
28
+ }
29
+
30
+ # Error messages are redirected to stderr
31
+ function handle_error {
32
+ echo " $( basename $0 ) : ERROR! An error was encountered executing line $1 ." 1>&2 ;
33
+ cleanup
34
+ echo ' Exiting with error.' 1>&2 ;
35
+ exit 1
36
+ }
37
+
38
+ function handle_exit {
39
+ cleanup
40
+ echo ' Exiting without error.' 1>&2 ;
41
+ exit
42
+ }
43
+
44
+ # Check for the existence of one or more files.
45
+ function exists {
46
+ for f in $* ; do
47
+ test -e " $f "
48
+ done
49
+ }
50
+
51
+ # Check for accidental dependencies in package.json
52
+ function checkDependencies {
53
+ if ! awk ' /"dependencies": {/{y=1;next}/},/{y=0; next}y' package.json | \
54
+ grep -v -q -E ' ^\s*"react(-dom|-scripts)?"' ; then
55
+ echo " Dependencies are correct"
56
+ else
57
+ echo " There are extraneous dependencies in package.json"
58
+ exit 1
59
+ fi
60
+ }
61
+
62
+ # Exit the script with a helpful error message when any error is encountered
63
+ trap ' set +x; handle_error $LINENO $BASH_COMMAND' ERR
64
+
65
+ # Cleanup before exit on any termination signal
66
+ trap ' set +x; handle_exit' SIGQUIT SIGTERM SIGINT SIGKILL SIGHUP
67
+
68
+ # Echo every command being executed
69
+ set -x
70
+
71
+ # Go to root
72
+ cd ..
73
+ root_path=$PWD
74
+
75
+ if hash npm 2> /dev/null
76
+ then
77
+ npm i -g npm@latest
78
+ npm cache clean || npm cache verify
79
+ fi
80
+
81
+ # Bootstrap monorepo
82
+ yarn
83
+
84
+ # ******************************************************************************
85
+ # First, publish the monorepo.
86
+ # ******************************************************************************
87
+
88
+ # Start local registry
89
+ tmp_registry_log=` mktemp`
90
+ nohup npx verdaccio@2.7.2 & > $tmp_registry_log &
91
+ # Wait for `verdaccio` to boot
92
+ grep -q ' http address' <( tail -f $tmp_registry_log )
93
+
94
+ # Set registry to local registry
95
+ npm set registry " $custom_registry_url "
96
+ yarn config set registry " $custom_registry_url "
97
+
98
+ # Login so we can publish packages
99
+ npx npm-cli-login@0.0.10 -u user -p password -e user@example.com -r " $custom_registry_url " --quotes
100
+
101
+ # Publish the monorepo
102
+ git clean -df
103
+ ./tasks/publish.sh --yes --force-publish=* --skip-git --cd-version=prerelease --exact --npm-tag=latest
104
+
105
+ # ******************************************************************************
106
+ # Test --scripts-version with a version number
107
+ # ******************************************************************************
108
+
109
+ cd " $temp_app_path "
110
+ npx create-react-app --scripts-version=1.0.17 test-app-version-number
111
+ cd test-app-version-number
112
+
113
+ # Check corresponding scripts version is installed.
114
+ exists node_modules/react-scripts
115
+ grep ' "version": "1.0.17"' node_modules/react-scripts/package.json
116
+ checkDependencies
117
+
118
+ # ******************************************************************************
119
+ # Test --use-npm flag
120
+ # ******************************************************************************
121
+
122
+ cd " $temp_app_path "
123
+ npx create-react-app --use-npm --scripts-version=1.0.17 test-use-npm-flag
124
+ cd test-use-npm-flag
125
+
126
+ # Check corresponding scripts version is installed.
127
+ exists node_modules/react-scripts
128
+ [ ! -e " yarn.lock" ] && echo " yarn.lock correctly does not exist"
129
+ grep ' "version": "1.0.17"' node_modules/react-scripts/package.json
130
+ checkDependencies
131
+
132
+ # ******************************************************************************
133
+ # Test --scripts-version with a tarball url
134
+ # ******************************************************************************
135
+
136
+ cd " $temp_app_path "
137
+ npx create-react-app --scripts-version=https://registry.npmjs.org/react-scripts/-/react-scripts-1.0.17.tgz test-app-tarball-url
138
+ cd test-app-tarball-url
139
+
140
+ # Check corresponding scripts version is installed.
141
+ exists node_modules/react-scripts
142
+ grep ' "version": "1.0.17"' node_modules/react-scripts/package.json
143
+ checkDependencies
144
+
145
+ # ******************************************************************************
146
+ # Test --scripts-version with a custom fork of react-scripts
147
+ # ******************************************************************************
148
+
149
+ cd " $temp_app_path "
150
+ npx create-react-app --scripts-version=react-scripts-fork test-app-fork
151
+ cd test-app-fork
152
+
153
+ # Check corresponding scripts version is installed.
154
+ exists node_modules/react-scripts-fork
155
+
156
+ # ******************************************************************************
157
+ # Test project folder is deleted on failing package installation
158
+ # ******************************************************************************
159
+
160
+ cd " $temp_app_path "
161
+ # we will install a non-existing package to simulate a failed installataion.
162
+ npx create-react-app --scripts-version=` date +%s` test-app-should-not-exist || true
163
+ # confirm that the project folder was deleted
164
+ test ! -d test-app-should-not-exist
165
+
166
+ # ******************************************************************************
167
+ # Test project folder is not deleted when creating app over existing folder
168
+ # ******************************************************************************
169
+
170
+ cd " $temp_app_path "
171
+ mkdir test-app-should-remain
172
+ echo ' ## Hello' > ./test-app-should-remain/README.md
173
+ # we will install a non-existing package to simulate a failed installataion.
174
+ npx create-react-app --scripts-version=` date +%s` test-app-should-remain || true
175
+ # confirm the file exist
176
+ test -e test-app-should-remain/README.md
177
+ # confirm only README.md is the only file in the directory
178
+ if [ " $( ls -1 ./test-app-should-remain | wc -l | tr -d ' [:space:]' ) " != " 1" ]; then
179
+ false
180
+ fi
181
+
182
+ # ******************************************************************************
183
+ # Test --scripts-version with a scoped fork tgz of react-scripts
184
+ # ******************************************************************************
185
+
186
+ cd $temp_app_path
187
+ curl " https://registry.npmjs.org/@enoah_netzach/react-scripts/-/react-scripts-0.9.0.tgz" -o enoah-scripts-0.9.0.tgz
188
+ npx create-react-app --scripts-version=$temp_app_path /enoah-scripts-0.9.0.tgz test-app-scoped-fork-tgz
189
+ cd test-app-scoped-fork-tgz
190
+
191
+ # Check corresponding scripts version is installed.
192
+ exists node_modules/@enoah_netzach/react-scripts
193
+
194
+ # ******************************************************************************
195
+ # Test nested folder path as the project name
196
+ # ******************************************************************************
197
+
198
+ # Testing a path that exists
199
+ cd " $temp_app_path "
200
+ mkdir test-app-nested-paths-t1
201
+ cd test-app-nested-paths-t1
202
+ mkdir -p test-app-nested-paths-t1/aa/bb/cc/dd
203
+ npx create-react-app test-app-nested-paths-t1/aa/bb/cc/dd
204
+ cd test-app-nested-paths-t1/aa/bb/cc/dd
205
+ yarn start --smoke-test
206
+
207
+ # Testing a path that does not exist
208
+ cd " $temp_app_path "
209
+ npx create-react-app test-app-nested-paths-t2/aa/bb/cc/dd
210
+ cd test-app-nested-paths-t2/aa/bb/cc/dd
211
+ yarn start --smoke-test
212
+
213
+ # Testing a path that is half exists
214
+ cd " $temp_app_path "
215
+ mkdir -p test-app-nested-paths-t3/aa
216
+ npx create-react-app test-app-nested-paths-t3/aa/bb/cc/dd
217
+ cd test-app-nested-paths-t3/aa/bb/cc/dd
218
+ yarn start --smoke-test
219
+
220
+ # Cleanup
221
+ cleanup
0 commit comments