diff --git a/scripts/start-dev-server.js b/scripts/start-dev-server.js index b9040cffb..24cd004d3 100644 --- a/scripts/start-dev-server.js +++ b/scripts/start-dev-server.js @@ -27,7 +27,9 @@ const convertAnsi = new Convert(); const DEFAULT_PORT = process.env.PORT || 3000; const STATIC_DIR = path.join(__dirname, '..', 'static'); -function launchElectron({bundleURL, electronURL}) { +let shutdownElectron = undefined; + +function launchElectron({devServerURL, bundleURL, electronURL}) { const args = [ path.join(STATIC_DIR, 'index.js'), '--remote-debugging-port=9222', @@ -41,17 +43,27 @@ function launchElectron({bundleURL, electronURL}) { SONAR_ROOT: process.cwd(), BUNDLE_URL: bundleURL, ELECTRON_URL: electronURL, + DEV_SERVER_URL: devServerURL, }, stdio: 'inherit', }); - proc.on('close', () => { + const electronCloseListener = () => { process.exit(); - }); + }; - process.on('exit', () => { + const processExitListener = () => { proc.kill(); - }); + }; + + proc.on('close', electronCloseListener); + process.on('exit', processExitListener); + + return () => { + proc.off('close', electronCloseListener); + process.off('exit', processExitListener); + proc.kill(); + }; } function startMetroServer(app) { @@ -105,6 +117,18 @@ function startAssetServer(port) { next(); }); + app.post('/_restartElectron', (req, res) => { + if (shutdownElectron) { + shutdownElectron(); + } + shutdownElectron = launchElectron({ + devServerURL: `http://localhost:${port}`, + bundleURL: `http://localhost:${port}/src/init.bundle`, + electronURL: `http://localhost:${port}/index.dev.html`, + }); + res.end(); + }); + app.get('/', (req, res) => { fs.readFile(path.join(STATIC_DIR, 'index.dev.html'), (err, content) => { res.end(content); @@ -221,7 +245,8 @@ function outputScreen(socket) { const socket = await addWebsocket(server); await startMetroServer(app); outputScreen(socket); - launchElectron({ + shutdownElectron = launchElectron({ + devServerURL: `http://localhost:${port}`, bundleURL: `http://localhost:${port}/src/init.bundle`, electronURL: `http://localhost:${port}/index.dev.html`, }); diff --git a/src/utils/restartFlipper.tsx b/src/utils/restartFlipper.tsx index 3d533a9c9..4b40a3651 100644 --- a/src/utils/restartFlipper.tsx +++ b/src/utils/restartFlipper.tsx @@ -15,10 +15,10 @@ export default function restart() { remote.app.relaunch(); remote.app.exit(); } else { - // Relaunching the process doesn't work in dev mode - // because it just launches an empty electron shell. - // Instead, approximate it by doing a refresh. - // Should be roughly equivalent but there may be some differences. - remote.getCurrentWindow().reload(); + // Relaunching the process with the standard way doesn't work in dev mode. + // So instead we're sending a signal to dev server to kill the current instance of electron and launch new. + fetch(`${remote.process.env.DEV_SERVER_URL}/_restartElectron`, { + method: 'POST', + }); } }