Skip to content

Commit

Permalink
Server is started before the build.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavoja committed Apr 4, 2017
1 parent e5a5a9f commit b7b14b3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webski",
"version": "2.3.0",
"version": "2.3.1",
"description": "Minimalist website builder",
"main": "index.js",
"repository": {
Expand Down
38 changes: 20 additions & 18 deletions src/webski.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Webski {
this.app = args.app || express()
this.hostname = args.hostname || 'localhost'
this.port = args.port || 8000
this.initialPort = this.port
this.builders = []
this.queue = []
this.lock = true
Expand Down Expand Up @@ -74,22 +75,22 @@ class Webski {
})
console.log(`Watching: ${chalk.gray(this.src)}`)

// Do the initial build.
this.buildAll(result => {
this.lock = false
callback && callback(result)
})

// Serve content.
let wss = this.serve()
this.serve(() => {
// Do the initial build.
this.buildAll(result => {
this.lock = false
callback && callback(result)
})
})

// Handle queue.
setInterval(() => {
this.processQueue(wss, callback)
this.processQueue(callback)
}, INTERVAL)
}

processQueue (wss, callback) {
processQueue (callback) {
if (this.lock) {
return
}
Expand All @@ -115,7 +116,7 @@ class Webski {
// Build.
this.build(files, changed => {
if (changed) {
this.reloadClient(wss)
this.reloadClient()
}

callback && callback(changed)
Expand Down Expand Up @@ -154,18 +155,19 @@ class Webski {
})
}

listen (port, callback) {
if (port - this.port > PORT_RANGE) {
listen (callback) {
if (this.port - this.initialPort > PORT_RANGE) {
return console.error('Unable to start the server.')
}

this.app
this.app
.listen(port, this.hostname, () => callback(port))
.listen(this.port, this.hostname, callback)
.on('error', err => {
if (err.message.indexOf('EADDRINUSE') !== -1) {
console.log(`Unable to start server on port ${port}.`)
this.listen(port + 1, callback)
console.log(`Unable to start server on port ${this.port}.`)
++this.port
this.listen(callback)
}
})
}
Expand Down Expand Up @@ -193,12 +195,12 @@ class Webski {
})
})

this.listen(this.port, (port) => {
let host = `${this.hostname}:${port}`
this.listen(() => {
let host = `${this.hostname}:${this.port}`
console.log(`Listening: ${chalk.blue(host)}`)
this.wss = new WebSocket.Server({
host: this.hostname,
port: port + 1
port: this.port + 1
})
callback && callback()
})
Expand Down

0 comments on commit b7b14b3

Please sign in to comment.