Skip to content

Commit

Permalink
Fixed reload command line regression
Browse files Browse the repository at this point in the history
* Added serve-statics, finalhandler, and url-parse as dependencies
* Handle all types of files statically with serve-static, (other than
HTML and reload client file
* Inject reload script tag on HTML files only
* Created public/private return API so that `reloadClientCode` (among other things later on need be) could be hidden from the public return API
  • Loading branch information
alallier committed Aug 6, 2017
1 parent bc3ef5a commit 37a1c03
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 54 deletions.
61 changes: 31 additions & 30 deletions lib/reload-server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
var http = require('http')
var path = require('path')
var reload = require('../lib/reload')
var fs = require('fs')
var open = require('open')
var clc = require('cli-color')
var argv = require('minimist')(process.argv.slice(2))

var serveStatic = require('serve-static')
var finalhandler = require('finalhandler')
var URL = require('url-parse')

var port = argv._[0]
var dir = argv._[1]
var openBrowser = (argv._[2] === 'true')
Expand All @@ -23,25 +26,37 @@ var reloadOpts = {
var time
var reloadReturned

var serve = serveStatic(dir, {'index': ['index.html', 'index.htm']})

var server = http.createServer(function (req, res) {
var file
var reqUrl = req.url
var url = new URL(req.url)
var pathname = url.pathname.replace(/(\/)(.*)/, '$2') // Strip leading `/` so we can find files on file system

if (reqUrl === '/') {
file = path.join(dir, startPage)
} else {
file = path.join(dir, reqUrl) + '.html'
}
var fileEnding = pathname.split('.')[1]

appendReloadClientCode(file, function (contents) {
if (contents) {
res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'})
res.end(contents)
} else {
res.writeHead(404, {'Content-Type': 'text/plain; charset=UTF-8'})
res.end('File Not Found')
if (fileEnding === 'html' || pathname === '/' || pathname === '') { // Server side inject reload code to html files
if (pathname === '/' || pathname === '') {
pathname = startPage
}
})

fs.readFile(pathname, 'utf8', function (err, contents) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/plain'})
res.end('File Not Found')
} else {
contents += '\n\n<!-- Inserted by Reload -->\n<script src="/reload/reload.js"></script>\n<!-- End Reload -->\n'

res.setHeader('Content-Type', 'text/html')
res.end(contents)
}
})
} else if (pathname === 'reload/reload.js') { // Server reload-client.js file from injected script tag
res.setHeader('Content-Type', 'text/javascript')

res.end(reloadReturned.reloadClientCode())
} else { // Serve any other file using serve-static
serve(req, res, finalhandler(req, res))
}
})

// Reload call and configurations. Stub app as it isn't used here
Expand All @@ -60,17 +75,3 @@ server.listen(port, function () {
console.log(clc.green('Server restarted at ' + time.toTimeString().slice(0, 8)))
}
})

// Function to send reload-client code to the browser.
function appendReloadClientCode (file, next) {
fs.readFile(file, 'utf8', function (err, contents) {
if (err) {
next(null)
}
var reloadClientCode = reloadReturned.reloadClientCode()

contents += '\n\n<!-- Inserted by Reload -->\n<script>' + reloadClientCode + '</script>\n<!-- End Reload -->\n'

next(contents)
})
}
20 changes: 13 additions & 7 deletions lib/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = function reload (app, opts, server) {
var socketPortSpecified
var argumentZero = arguments[0]
var reloadJsMatch
var reloadReturn

opts = opts || {}

Expand Down Expand Up @@ -128,20 +129,25 @@ module.exports = function reload (app, opts, server) {
}
})
}
// Return an object, right now it contains only a function reload. When this function is called it calls the `sendMessage` function with the message 'reload' which reloads all connected clients.
return {

reloadReturn = {
'reload': function () {
sendMessage('reload')
},
'reloadClientCode': function () {
if (server) {
return reloadCode
}
},
'startWebSocketServer': function () {
if (webSocketServerWaitStart) {
startWebSocketServer()
}
}
}

if (server) { // Private return API only used in command line version of reload
reloadReturn.reloadClientCode = function () {
if (server) {
return reloadCode
}
}
}

return reloadReturn
}
Loading

0 comments on commit 37a1c03

Please sign in to comment.