@@ -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,78 @@ 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=""></a>
156
+ in <span id="timer">${ redirectSeconds } </span> seconds</p>
157
+ <script>
158
+ window.embarkApiRedirect = window.location.href.replace(
159
+ \`http://\${window.location.hostname}:55555\`,
160
+ \`http://\${window.location.hostname}:3000\`
161
+ );
162
+ document.querySelector("#redirect").href = window.embarkApiRedirect;
163
+ let displayLink = window.embarkApiRedirect.slice(7);
164
+ if (displayLink.endsWith(\`\${window.location.hostname}:3000/\`)) {
165
+ displayLink = displayLink.slice(0, -1);
166
+ }
167
+ document.querySelector("#redirect").innerText = displayLink;
168
+ </script>
169
+ <script>
170
+ let timeLeft = ${ redirectSeconds } ;
171
+ const span = document.querySelector("#timer");
172
+ const timer = window.setInterval(() => {
173
+ if (timeLeft >= 1) {
174
+ timeLeft -= 1;
175
+ span.innerText = \`\${timeLeft}\`;
176
+ }
177
+ if (!timeLeft) {
178
+ window.clearInterval(timer);
179
+ window.location.href = window.embarkApiRedirect;
180
+ }
181
+ }, 1000);
182
+ </script>
156
183
` ) ;
157
184
}
158
185
@@ -195,7 +222,7 @@ export default class Server {
195
222
if ( ! this . isInsideMonorepo || process . env . EMBARK_UI_STATIC ) {
196
223
if ( existsSync ( path . join ( this . embarkUiBuildDir , "index.html" ) ) ) {
197
224
instance . app . use ( "/" , express . static ( this . embarkUiBuildDir ) ) ;
198
- instance . app . get ( "/*" , ( _req , res ) => {
225
+ instance . app . get ( / ^ \/ (? ! e m b a r k - a p i ) . * $ / , ( _req , res ) => {
199
226
res . sendFile ( path . join ( this . embarkUiBuildDir , "index.html" ) ) ;
200
227
} ) ;
201
228
} else {
@@ -204,7 +231,9 @@ export default class Server {
204
231
in <code>${ path . dirname ( this . embarkUiBuildDir ) } </code>
205
232
` ;
206
233
let notice = `
207
- <p>this distribution of <code>embark-ui</code> appears to be broken</p>
234
+ <p>this distribution of <code>embark-ui</code> appears to be broken,
235
+ please <a href="https://github.com/embark-framework/embark/issues">
236
+ file an issue</a></p>
208
237
` ;
209
238
if ( this . isInsideMonorepo ) {
210
239
envReport = `
@@ -223,40 +252,24 @@ export default class Server {
223
252
&& yarn build</code></p>
224
253
<p>restart <code>embark run</code> after building
225
254
<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>
255
+ <p>to instead use a live development build from the monorepo: unset
256
+ the environment variable <code>EMBARK_UI_STATIC</code>, restart
257
+ embark, and visit
258
+ <a href="http://localhost:3000">http://localhost:3000</a></p>
229
259
` ;
230
260
}
231
- const page404 = this . makePage404 ( 3 , envReport , inside , notice ) ;
261
+ const page404 = this . makePage404 ( 10 , envReport , inside , notice ) ;
232
262
const missingBuildHandler = ( _req : Request , res : Response ) => {
233
263
res . status ( 404 ) . send ( page404 ) ;
234
264
} ;
235
- instance . app . get ( "/" , missingBuildHandler ) ;
236
- instance . app . get ( "/*" , missingBuildHandler ) ;
265
+ instance . app . get ( / ^ \/ (? ! e m b a r k - a p i ) .* $ / , missingBuildHandler ) ;
237
266
}
238
267
} 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
- } ) ) ;
268
+ const page503 = this . makePage503 ( 10 ) ;
269
+ const unavailableBuildHandler = ( _req : Request , res : Response ) => {
270
+ res . status ( 503 ) . send ( page503 ) ;
271
+ } ;
272
+ instance . app . get ( / ^ \/ (? ! e m b a r k - a p i ) .* $ / , unavailableBuildHandler ) ;
260
273
}
261
274
262
275
return instance ;
0 commit comments