@@ -2,7 +2,6 @@ import bodyParser from "body-parser";
2
2
import cors from "cors" ;
3
3
import { Embark , Plugins } from "embark" ;
4
4
import express , { NextFunction , Request , Response } from "express" ;
5
- import proxy from "express-http-proxy" ;
6
5
import expressWs from "express-ws" ;
7
6
import findUp from "find-up" ;
8
7
import helmet from "helmet" ;
@@ -87,15 +86,12 @@ export default class Server {
87
86
} ) ;
88
87
}
89
88
90
- private makePage ( reloadSeconds : number , body : string ) {
89
+ private makePage ( body : string ) {
91
90
return ( `
92
91
<!doctype html>
93
92
<html lang="en">
94
93
<head>
95
94
<meta charset="utf-8" />
96
- ${ this . isInsideMonorepo ? `
97
- <meta http-equiv="refresh" content="${ reloadSeconds } ">
98
- ` : "" }
99
95
<title>Embark API Server</title>
100
96
<style type="text/css">
101
97
code {
@@ -112,47 +108,76 @@ export default class Server {
112
108
</head>
113
109
<body>
114
110
${ body }
115
- ${ this . isInsideMonorepo ? `
116
- <p>this page will automatically reload
117
- in <span id="timer">${ reloadSeconds } </span> seconds</p>
118
- <script>
119
- let timeLeft = ${ reloadSeconds } ;
120
- const span = document.querySelector("#timer");
121
- setInterval(() => {
122
- if (timeLeft >= 1) { timeLeft -= 1; }
123
- span.innerText = \`\${timeLeft}\`;
124
- }, 1000);
125
- </script>
126
- ` : "" }
127
111
</body>
128
112
</html>
129
113
` . trim ( ) . split ( "\n" ) . map ( ( str ) => str . trim ( ) ) . filter ( ( str ) => str ) . join ( "\n" ) ) ;
130
114
}
131
115
132
116
private makePage404 ( reloadSeconds : number , envReport : string , inside : string , notice : string ) {
133
- return this . makePage ( reloadSeconds , `
117
+ return this . makePage ( `
134
118
${ envReport }
135
119
<p>missing build for package <code>embark-ui</code> ${ inside } </p>
136
120
${ notice }
121
+ ${ this . isInsideMonorepo ? `
122
+ <p>this page will automatically reload
123
+ in <span id="timer">${ reloadSeconds } </span> seconds</p>
124
+ <script>
125
+ let timeLeft = ${ reloadSeconds } ;
126
+ const span = document.querySelector("#timer");
127
+ const timer = window.setInterval(() => {
128
+ if (timeLeft >= 1) {
129
+ timeLeft -= 1;
130
+ span.innerText = \`\${timeLeft}\`;
131
+ }
132
+ if (!timeLeft) {
133
+ window.clearInterval(timer);
134
+ window.location.reload(true);
135
+ }
136
+ }, 1000);
137
+ </script>
138
+ ` : "" }
137
139
` ) ;
138
140
}
139
141
140
- private makePageEConnError ( reloadSeconds : number , waitingFor : string ) {
141
- return this . makePage ( reloadSeconds , `
142
- <p><code>lib/modules/api/server</code> inside the monorepo at
143
- <code>${ path . join ( this . monorepoRootDir , "packages/embark" ) } </code> is
144
- waiting for the Create React App development server of package
145
- <code>embark-ui</code> to ${ waitingFor } at
146
- <code>localhost:55555</code></p>
147
- ${ waitingFor === "become available" ? `
148
- <p>please run either:</p>
149
- <p><code>cd ${ this . monorepoRootDir } && yarn start</code><br />
150
- or<br />
151
- <code>cd ${ path . join ( this . monorepoRootDir , "packages/embark-ui" ) }
152
- && yarn start</code></p>
142
+ private makePage503 ( redirectSeconds : number ) {
143
+ return this . makePage ( `
144
+ <p><code>lib/modules/api/server</code> is inside the monorepo at
145
+ <code>${ path . join ( this . monorepoRootDir , "packages/embark" ) } </code></p>
146
+ <p>to access <code>embark-ui</code> in development use port
147
+ <code>3000</code></p>
148
+ <p>if you haven't already, please run either:</p>
149
+ <p><code>cd ${ this . monorepoRootDir } && yarn start</code><br />
150
+ or<br />
151
+ <code>cd ${ path . join ( this . monorepoRootDir , "packages/embark-ui" ) } &&
152
+ yarn start</code></p>
153
153
<p>to instead use a static build from the monorepo, restart embark with:
154
154
<code>EMBARK_UI_STATIC=t embark run</code></p>
155
- ` : "" }
155
+ <p>this page will automatically redirect to <a id="redirect" href="">
156
+ <span></span></a> in <span id="timer">${ redirectSeconds } </span>
157
+ seconds</p>
158
+ <script>
159
+ window.embarkApiRedirect = window.location.href.replace(
160
+ "http://localhost:55555",
161
+ "http://localhost:3000"
162
+ );
163
+ document.querySelector("#redirect").href = window.embarkApiRedirect;
164
+ document.querySelector("#redirect span").innerText =
165
+ window.embarkApiRedirect;
166
+ </script>
167
+ <script>
168
+ let timeLeft = ${ redirectSeconds } ;
169
+ const span = document.querySelector("#timer");
170
+ const timer = window.setInterval(() => {
171
+ if (timeLeft >= 1) {
172
+ timeLeft -= 1;
173
+ span.innerText = \`\${timeLeft}\`;
174
+ }
175
+ if (!timeLeft) {
176
+ window.clearInterval(timer);
177
+ window.location.href = window.embarkApiRedirect;
178
+ }
179
+ }, 1000);
180
+ </script>
156
181
` ) ;
157
182
}
158
183
@@ -195,7 +220,7 @@ export default class Server {
195
220
if ( ! this . isInsideMonorepo || process . env . EMBARK_UI_STATIC ) {
196
221
if ( existsSync ( path . join ( this . embarkUiBuildDir , "index.html" ) ) ) {
197
222
instance . app . use ( "/" , express . static ( this . embarkUiBuildDir ) ) ;
198
- instance . app . get ( "/*" , ( _req , res ) => {
223
+ instance . app . get ( / ^ \/ (? ! e m b a r k - a p i ) . * $ / , ( _req , res ) => {
199
224
res . sendFile ( path . join ( this . embarkUiBuildDir , "index.html" ) ) ;
200
225
} ) ;
201
226
} else {
@@ -204,7 +229,9 @@ export default class Server {
204
229
in <code>${ path . dirname ( this . embarkUiBuildDir ) } </code>
205
230
` ;
206
231
let notice = `
207
- <p>this distribution of <code>embark-ui</code> appears to be broken</p>
232
+ <p>this distribution of <code>embark-ui</code> appears to be broken,
233
+ please <a href="https://github.com/embark-framework/embark/issues">
234
+ file an issue</a></p>
208
235
` ;
209
236
if ( this . isInsideMonorepo ) {
210
237
envReport = `
@@ -223,40 +250,24 @@ export default class Server {
223
250
&& yarn build</code></p>
224
251
<p>restart <code>embark run</code> after building
225
252
<code>embark-ui</code></p>
226
- <p>to instead use a live development build from the monorepo, unset
227
- the environment variable <code>EMBARK_UI_STATIC</code> and restart
228
- embark</p>
253
+ <p>to instead use a live development build from the monorepo: unset
254
+ the environment variable <code>EMBARK_UI_STATIC</code>, restart
255
+ embark, and visit
256
+ <a href="http://localhost:3000">http://localhost:3000</a></p>
229
257
` ;
230
258
}
231
- const page404 = this . makePage404 ( 3 , envReport , inside , notice ) ;
259
+ const page404 = this . makePage404 ( 10 , envReport , inside , notice ) ;
232
260
const missingBuildHandler = ( _req : Request , res : Response ) => {
233
261
res . status ( 404 ) . send ( page404 ) ;
234
262
} ;
235
- instance . app . get ( "/" , missingBuildHandler ) ;
236
- instance . app . get ( "/*" , missingBuildHandler ) ;
263
+ instance . app . get ( / ^ \/ (? ! e m b a r k - a p i ) .* $ / , missingBuildHandler ) ;
237
264
}
238
265
} else {
239
- const page503 = this . makePageEConnError ( 3 , "become available" ) ;
240
- const page504 = this . makePageEConnError ( 3 , "become responsive" ) ;
241
- instance . app . use ( "/" , proxy ( "http://localhost:3000" , {
242
- // @ts -ignore
243
- proxyErrorHandler : ( err , res , next ) => {
244
- switch ( err && err . code ) {
245
- case "ECONNREFUSED" : {
246
- return res . status ( 503 ) . send ( page503 ) ;
247
- }
248
- case "ECONNRESET" : {
249
- if ( err . message === "socket hang up" ) {
250
- return res . status ( 504 ) . send ( page504 ) ;
251
- }
252
- }
253
- default : {
254
- next ( err ) ;
255
- }
256
- }
257
- } ,
258
- timeout : 1000 ,
259
- } ) ) ;
266
+ const page503 = this . makePage503 ( 10 ) ;
267
+ const unavailableBuildHandler = ( _req : Request , res : Response ) => {
268
+ res . status ( 503 ) . send ( page503 ) ;
269
+ } ;
270
+ instance . app . get ( / ^ \/ (? ! e m b a r k - a p i ) .* $ / , unavailableBuildHandler ) ;
260
271
}
261
272
262
273
return instance ;
0 commit comments