Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/alallier/reload into reno…
Browse files Browse the repository at this point in the history
…vate/open-8.x
  • Loading branch information
alallier committed Jun 9, 2021
2 parents d23de5e + eff5d01 commit 5d19232
Show file tree
Hide file tree
Showing 12 changed files with 7,303 additions and 1,530 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [12.x, 14.x, 16.x]
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
Expand Down
30 changes: 16 additions & 14 deletions bin/reload
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env node

var program = require('commander')
var supervisor = require('supervisor')
var path = require('path')
var os = require('os')
var clc = require('cli-color')
const program = require('commander')
const supervisor = require('supervisor')
const path = require('path')
const os = require('os')
const clc = require('cli-color')

program.version(require('../package.json').version)
.option('-b, --browser', 'Open in the browser automatically.')
Expand All @@ -18,21 +18,23 @@ program.version(require('../package.json').version)
.option('-v, --verbose [verbose]', 'Turning on logging on the server and client side. Defaults to false', false)
.parse(process.argv)

var runFile = path.join(os.tmpdir(), 'reload-' + Math.random().toString().slice(2))
var serverFile = path.join(__dirname, '../lib/reload-server.js')
const options = program.opts()

if (program.exts.indexOf(',')) {
program.exts = program.exts.replace(/,/g, '|') // replace comma for pipe, that's what supervisor likes
const runFile = path.join(os.tmpdir(), 'reload-' + Math.random().toString().slice(2))
const serverFile = path.join(__dirname, '../lib/reload-server.js')

if (options.exts.indexOf(',')) {
options.exts = options.exts.replace(/,/g, '|') // replace comma for pipe, that's what supervisor likes
}

// Fall back to the serving directory.
if (typeof program.watchDir === 'undefined') {
program.watchDir = program.dir
if (typeof options.watchDir === 'undefined') {
options.watchDir = options.dir
}

var args = ['-e', program.exts, '-w', program.watchDir, '-q', '--', serverFile, program.port, program.dir, !!program.browser, program.hostname, runFile, program.startPage, program.fallback, program.verbose]
const args = ['-e', options.exts, '-w', options.watchDir, '-q', '--', serverFile, options.port, options.dir, !!options.browser, options.hostname, runFile, options.startPage, options.fallback, options.verbose]
supervisor.run(args)

console.log('\nReload web server:')
console.log('listening on port ' + clc.blue.bold(program.port))
console.log('monitoring dir ' + clc.green.bold(program.dir))
console.log('listening on port ' + clc.blue.bold(options.port))
console.log('monitoring dir ' + clc.green.bold(options.dir))
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function (app, opts) {
var reload = require('./lib/reload')
const reload = require('./lib/reload')

return reload(app, opts)
}
18 changes: 9 additions & 9 deletions lib/reload-client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Reload client file. **DO NOT REMOVE THIS COMMENT**

(function refresh () {
var verboseLogging = false
var socketUrl = window.location.origin
const verboseLogging = false
let socketUrl = window.location.origin
if (!window.location.origin.match(/:[0-9]+/)) {
socketUrl = window.location.origin + ':80'
}
socketUrl = socketUrl.replace() // This is dynamically populated by the reload.js file before it is sent to the browser
var socket
let socket

if (verboseLogging) {
console.log('Reload Script Loaded')
Expand All @@ -20,10 +20,10 @@
// Explanation of the flags below:

// The first change flag is used to tell reload to wait until the socket closes at least once before we allow the page to open on a socket open event. Otherwise reload will go into a inifite loop, as the page will have a socket on open event once it loads for the first time
var firstChangeFlag = false
let firstChangeFlag = false

// The navigatedAwayFromPageFlag is set to true in the event handler onbeforeunload because we want to short-circuit reload to prevent it from causing the page to reload before the navigation occurs.
var navigatedAwayFromPageFlag
let navigatedAwayFromPageFlag

// Wait until the page loads for the first time and then call the webSocketWaiter function so that we can connect the socket for the first time
window.addEventListener('load', function () {
Expand All @@ -43,13 +43,13 @@
})

// Check to see if the server sent us reload (meaning a manually reload event was fired) and then reloads the page
var socketOnMessage = function (msg) {
const socketOnMessage = function (msg) {
if (msg.data === 'reload') {
socket.close()
}
}

var socketOnOpen = function (msg) {
const socketOnOpen = function (msg) {
if (verboseLogging) {
console.log('Socket Opened')
}
Expand All @@ -69,7 +69,7 @@
}

// Socket on close event that sets flags and calls the webSocketWaiter function
var socketOnClose = function (msg) {
const socketOnClose = function (msg) {
if (verboseLogging) {
console.log('Socket Closed - Calling webSocketWaiter')
}
Expand All @@ -81,7 +81,7 @@
websocketWaiter()
}

var socketOnError = function (msg) {
const socketOnError = function (msg) {
if (verboseLogging) {
console.log(msg)
}
Expand Down
52 changes: 26 additions & 26 deletions lib/reload-server.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
var http = require('http')
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))
const http = require('http')
const reload = require('../lib/reload')
const fs = require('fs')
const open = require('open')
const clc = require('cli-color')
const argv = require('minimist')(process.argv.slice(2))

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

var port = argv._[0]
var dir = argv._[1]
var openBrowser = (argv._[2] === 'true')
var hostname = argv._[3]
var runFile = argv._[4]
var startPage = argv._[5]
var fallback = (argv._[6] === 'true')
var verbose = (argv._[7] === 'true')
const port = argv._[0]
const dir = argv._[1]
const openBrowser = (argv._[2] === 'true')
const hostname = argv._[3]
const runFile = argv._[4]
const startPage = argv._[5]
const fallback = (argv._[6] === 'true')
const verbose = (argv._[7] === 'true')

var reloadOpts = {
const reloadOpts = {
verbose: verbose,
noExpress: true
}

var time
var reloadReturned
let time
let reloadReturned

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

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

var fileEnding = pathname.split('.')[1]
var noFileEnding = fileEnding === undefined
const fileEnding = pathname.split('.')[1]
const noFileEnding = fileEnding === undefined

if (fileEnding === 'html' || pathname === '/' || pathname === '' || noFileEnding) { // Server side inject reload code to html files
if (pathname === '/' || pathname === '') {
Expand Down
2 changes: 1 addition & 1 deletion lib/reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ws = require('ws')
module.exports = function reload (app, opts, server) {
opts = opts || {}

var argumentCache = arguments
const argumentCache = arguments

return new Promise(function (resolve, reject) {
// Parameters variables
Expand Down
Loading

0 comments on commit 5d19232

Please sign in to comment.