Skip to content

Commit

Permalink
Reload button (#224)
Browse files Browse the repository at this point in the history
* Button added to the debugger

* Reload endpoint

* Add reload command to CLI, updated snapshots
  • Loading branch information
krizzu authored and satya164 committed Oct 10, 2017
1 parent a92c463 commit 22a8224
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/cliEntry.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Commands:
init Generates necessary configuration files
start Starts a new webpack server
bundle Builds the app bundle for packaging
reload Sends reload request to all devices that enabled live reload
Run haul COMMAND --help for more information on specific commands
"
Expand Down
1 change: 1 addition & 0 deletions src/cliEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const COMMANDS: Array<Command> = [
require('./commands/init'),
require('./commands/start'),
require('./commands/bundle'),
require('./commands/reload'),
];

const NOT_SUPPORTED_COMMANDS = [
Expand Down
48 changes: 48 additions & 0 deletions src/commands/reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2017-present, Callstack.
* All rights reserved.
*
* @flow
*/

const clear = require('clear');
const logger = require('../logger');
const http = require('http');

/**
* Send reload request to devices
*/

async function reload() {
const requestOptions = {
hostname: 'localhost',
port: 8081,
path: '/reloadapp',
method: 'HEAD',
};

const req = http.request(requestOptions, () => {
clear();
logger.done('Sent reload request');
req.end();
});

req.on('error', e => {
clear();
const error = e.toString();
if (error.includes('connect ECONNREFUSED')) {
logger.error(`Reload request failed. Make sure Haul is up.`);
} else {
logger.error(e);
}
});

req.end();
}

module.exports = {
name: 'reload',
description: 'Sends reload request to all devices that enabled live reload',
action: reload,
options: [],
};
35 changes: 35 additions & 0 deletions src/server/assets/debugger.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
document.getElementById('devtools-info').innerHTML = 'Press <span class="shortcut">' +
refresh_shortcut +
'</span> in simulator to reload and connect.';

document.getElementById('reloadButton').addEventListener('click', function() {
fetch('http://' + window.location.host + '/reloadapp', { method: 'HEAD' });
})
};
var INITIAL_MESSAGE = '<span class="circle circle-green"></span> Waiting';
function connectToDebuggerProxy() {
Expand Down Expand Up @@ -154,6 +158,33 @@
text-align: center;
}

.features {
text-align: center;
}

#reloadButton {
background-color:#b8e356;
border-radius:6px;
border:1px solid #83c41a;
display:inline-block;
color:#ffffff;
font-family:Arial;
font-size:15px;
cursor: pointer;
font-weight:bold;
padding:6px 24px;
text-decoration:none;
text-shadow:0px 1px 0px #86ae47;
outline: none;
}
#reloadButton:hover {
background-color:#a5cc52;
}
#reloadButton:active {
position:relative;
top:1px;
}

img {
width: 100%;
}
Expand All @@ -179,6 +210,10 @@
<p>Press <kbd id='dev_tools_shortcut' class="shortcut">⌘⌥J</kbd> to open Developer Tools.</p>
<p id='devtools-info'></p>
</div>

<div class="features">
<button title="Make sure 'Live Reload' is enabled on the device" id="reloadButton">Reload App</button>
</div>
</main>
</body>

Expand Down
29 changes: 19 additions & 10 deletions src/server/middleware/liveReloadMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
*/
function liveReloadMiddleware(compiler) {
let watchers = [];
const headers = {
'Content-Type': 'application/json; charset=UTF-8',
};

function notifyAllWatchers() {
watchers.forEach(watcher => {
watcher.res.writeHead(205, headers);
watcher.res.end(JSON.stringify({ changed: true }));
});

watchers = [];
}

return (req, res, next) => {
/**
Expand All @@ -26,20 +38,17 @@ function liveReloadMiddleware(compiler) {
return;
}

if (req.path === '/reloadapp') {
notifyAllWatchers();
res.end();
return;
}

/**
* On new `build`, notify all registered watchers to reload
*/
compiler.plugin('done', () => {
const headers = {
'Content-Type': 'application/json; charset=UTF-8',
};

watchers.forEach(watcher => {
watcher.res.writeHead(205, headers);
watcher.res.end(JSON.stringify({ changed: true }));
});

watchers = [];
notifyAllWatchers();
});

next();
Expand Down

0 comments on commit 22a8224

Please sign in to comment.