Skip to content
Permalink
Browse files

feat(servers): find an open server port and use it if a port is alrea…

…dy in use
  • Loading branch information
jyounce committed Aug 11, 2015
1 parent 1290b4e commit 5d1b34d8ce4332f5e4170f09d86343df67dc642f
@@ -9,7 +9,7 @@ module.exports = (config) ->
# messages
# ========
server.msg =
start: "Server started on #{config.ports.server}"
start: 'Server started on'
noScripts: 'No application server scripts to load.'

# add server to config
@@ -72,6 +72,7 @@ module.exports = (gulp, config) ->

# server
# ======
require("#{config.req.tasks}/server/find-open-port") gulp, config # find-open-port
require("#{config.req.tasks}/server/start-server") gulp, config # start-server
require("#{config.req.tasks}/server/spawn-server") gulp, config # spawn-server
require("#{config.req.tasks}/server/nodemon") gulp, config, bs # nodemon
@@ -32,6 +32,8 @@
"del": "~1.2.0",
"event-stream": "~3.3.1",
"express": "~4.13.3",
"find-port": "~1.0.1",
"fs-extra": "~0.23.1",
"glob-stream": "~5.0.0",
"gulp": "~3.9.0",
"gulp-angular-templatecache": "~1.7.0",
@@ -11,7 +11,7 @@ serverDirPath = path.resolve dir, '..', '..', config.dist.app.server.dirName

app.use express.static clientDirPath
app.listen port, ->
console.log config.server.msg.start
console.log "#{config.server.msg.start} #{config.ports.server}"

app.get '/', (req, res) ->
res.sendFile spa, root: clientDirPath
@@ -3,22 +3,29 @@ module.exports = (gulp, config) ->
browserSync = require 'browser-sync'
bs = browserSync.create()

bsConfig =
# helpers
# =======
getBsConfig = ->
files: config.glob.browserSync
proxy: "http://localhost:#{config.ports.server}/"
port: config.ports.reload
ui: port: config.ports.reloadUI
browser: 'google chrome'
# open: false

# events
# ======
bs.emitter.on 'serverRestart', ->
return if not bs.active
setTimeout ->
bs.reload stream:false
, 1000

# register task
# =============
gulp.task "#{config.rb.prefix.task}browser-sync", ->
defer = q.defer()
defer = q.defer()
bsConfig = getBsConfig()
bs.init bsConfig, ->
defer.resolve()
defer.promise
@@ -10,6 +10,7 @@ module.exports = (gulp, config) ->
"#{config.rb.prefix.task}clean-dist"
"#{config.rb.prefix.task}build-angular-modules"
"#{config.rb.prefix.task}build-config"
"#{config.rb.prefix.task}find-open-port" # updates config.ports if a port is in use
[
"#{config.rb.prefix.task}copy-bower_components"
"#{config.rb.prefix.task}copy-css"
@@ -0,0 +1,86 @@
module.exports = (gulp, config) ->
q = require 'q'
fse = require 'fs-extra'
findPort = require 'find-port'
promiseHelp = require "#{config.req.helpers}/promise"

# global
# ======
updateConfig = false

# helpers
# =======
isPortUsed = (server, openPorts, configPort) ->
used = openPorts.indexOf(configPort) is -1
# console.log "is #{server} port #{configPort} used: #{used}".yellow
updateConfig = true if used and updateConfig is false
used

getConfigPorts = ->
ports = []
for server, port of config.ports
ports.push port
ports

getNewPort = (openPorts) ->
configPorts = getConfigPorts()
newPort = null
for openPort in openPorts
if configPorts.indexOf(openPort) is -1
newPort = openPort
break
newPort

# tasks
# =====
setPort = (server) ->
defer = q.defer()
configPort = config.ports[server]
start = configPort
end = start + 10
range = [start..end]
findPort range, (openPorts) ->
portUsed = isPortUsed server, openPorts, configPort
return defer.resolve() unless portUsed
config.ports[server] = getNewPort openPorts
msg = "#{server} port switched to #{config.ports[server]} because #{configPort} was in use"
console.log msg.yellow
defer.resolve()
defer.promise

setPorts = -> # synchronously
defer = q.defer()
tasks = [
-> setPort 'server'
-> setPort 'reload'
-> setPort 'reloadUI'
-> setPort 'test'
]
tasks.reduce(q.when, q()).done -> defer.resolve()
defer.promise

updateConfigFile = ->
return promiseHelp.get() unless updateConfig
# console.log 'update config:', updateConfig
defer = q.defer()
format = spaces: '\t'
configFile = config.templates.config.dest.path
fse.writeJson configFile, config, format, (e) ->
defer.resolve()
defer.promise

runTasks = -> # synchronously
defer = q.defer()
tasks = [
-> setPorts()
-> updateConfigFile()
]
tasks.reduce(q.when, q()).done -> defer.resolve()
defer.promise

# register task
# =============
gulp.task "#{config.rb.prefix.task}find-open-port", ->
runTasks()


@@ -10,15 +10,6 @@ module.exports = (gulp, config) ->

# Global
# ======
karmaConfig =
autoWatch: false
basePath: config.app.dir
browsers: config.test.browsers # see config-test.coffee
frameworks: ['jasmine', 'jasmine-matchers']
port: config.ports.test
reporters: ['dots']
singleRun: true

TestResults = status: null, exitCode: null

# helpers
@@ -29,6 +20,15 @@ module.exports = (gulp, config) ->
tests = require(tests).client
tests

getKarmaConfig = ->
autoWatch: false
basePath: config.app.dir
browsers: config.test.browsers # see config-test.coffee
frameworks: ['jasmine', 'jasmine-matchers']
port: config.ports.test
reporters: ['dots']
singleRun: true

# tasks
# =====
cleanResultsFile = (src, file) ->
@@ -39,8 +39,9 @@ module.exports = (gulp, config) ->
defer.promise

runTests = ->
defer = q.defer()
tests = getTestsFile()
defer = q.defer()
tests = getTestsFile()
karmaConfig = getKarmaConfig()

if not tests.scriptsTestCount
console.log 'no test scripts to run'.yellow

0 comments on commit 5d1b34d

Please sign in to comment.
You can’t perform that action at this time.