From 45a2ab261540a634ba6538d2b77f29f77a8919ad Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Tue, 23 Mar 2021 20:47:50 -0700 Subject: [PATCH] proxy server support update-ports restart command --- .../src/lib/dev-admin/admin-server.ts | 2 +- .../src/lib/dev-admin/redbird-proxy.ts | 39 +++++++++++++++---- .../src/lib/dev-admin/redbird-spawn.ts | 2 + 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/xarc-app-dev/src/lib/dev-admin/admin-server.ts b/packages/xarc-app-dev/src/lib/dev-admin/admin-server.ts index 6114be133..4a475f35e 100644 --- a/packages/xarc-app-dev/src/lib/dev-admin/admin-server.ts +++ b/packages/xarc-app-dev/src/lib/dev-admin/admin-server.ts @@ -798,7 +798,7 @@ ${instruction}` this._updateProxyTimer = null; this.sendMsg(PROXY_SERVER_NAME, { ...update, - name: "restart", + name: "update-ports", quiet: true }); }, 250).unref(); diff --git a/packages/xarc-app-dev/src/lib/dev-admin/redbird-proxy.ts b/packages/xarc-app-dev/src/lib/dev-admin/redbird-proxy.ts index 40434f9d9..0a844dc96 100644 --- a/packages/xarc-app-dev/src/lib/dev-admin/redbird-proxy.ts +++ b/packages/xarc-app-dev/src/lib/dev-admin/redbird-proxy.ts @@ -193,13 +193,38 @@ const registerElectrodeDevRules = ({ src: formUrl({ protocol, host, path: controlPaths.restart }), target: `http://localhost:29999/skip`, onRequest: (req, res) => { - const restartOpts = QS.parse(req._url.split("?")[1]); - - res.statusCode = 200; - res.write(`proxy restarted`); - res.end(); - - process.nextTick(() => restart(restartOpts)); + const restartOpts = { ...QS.parse(req._url.split("?")[1]) }; + + const newPorts: any = {}; + + if (restartOpts.updatePorts) { + if (restartOpts.hasOwnProperty("appPort")) { + const appPort = restartOpts.appPort as string; + if (appPort !== `${regAppPort}`) { + newPorts.appPort = appPort; + } + } + if (restartOpts.hasOwnProperty("webpackDevPort")) { + const webpackDevPort = restartOpts.webpackDevPort as string; + if (webpackDevPort !== `${regWebpackDevPort}`) { + newPorts.webpackDevPort = webpackDevPort; + } + } + res.statusCode = 200; + if (_.isEmpty(newPorts)) { + res.write(`update-ports - proxy ports same: app ${regAppPort}, wds ${regWebpackDevPort}`); + } else { + res.write(`update-ports - proxy proxy ports updated`); + process.nextTick(() => restart({ ...restartOpts, newPorts })); + } + res.end(); + } else { + res.statusCode = 200; + res.write(`proxy restarted`); + res.end(); + + process.nextTick(() => restart({ ...restartOpts, newPorts })); + } return false; } diff --git a/packages/xarc-app-dev/src/lib/dev-admin/redbird-spawn.ts b/packages/xarc-app-dev/src/lib/dev-admin/redbird-spawn.ts index 3cc8c418b..7bcefb005 100644 --- a/packages/xarc-app-dev/src/lib/dev-admin/redbird-spawn.ts +++ b/packages/xarc-app-dev/src/lib/dev-admin/redbird-spawn.ts @@ -69,6 +69,8 @@ const handleRestart = type => { process.on("message", data => { if (data.name === "restart") { restart(_.omit(data, "name")); + } else if (data.name === "update-ports") { + restart({ ..._.omit(data, "name"), updatePorts: true }); } }); };