Skip to content

Commit

Permalink
fix(test server task): stop the server when the test has ran
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

The application server routes file arguments have changed because it was limited to only app and opts. Now this file accepts one server argument that is an object containing app, express, middleware, paths and server.

Before:
module.exports = (app, opts) => {
  app.get('/api/superheroes', (req, res) =>
  ...
}

Now:
module.exports = (server) => {
  var app = server.app

  app.get('/api/superheroes', (req, res) =>
  ...
}
  • Loading branch information
jyounce committed Sep 30, 2015
1 parent 1874068 commit 9a2f9e9
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 122 deletions.
1 change: 0 additions & 1 deletion config.coffee
Expand Up @@ -15,7 +15,6 @@ module.exports = (rbDir, options) ->
config = require("#{config.req.config}/config-app") config, options
config = require("#{config.req.config}/config-build") config, options
config = require("#{config.req.config}/config-ports") config, options
config = require("#{config.req.config}/config-server") config
config = require("#{config.req.config}/config-browser") config, options
config = require("#{config.req.config}/config-minify") config, options
config = require("#{config.req.config}/config-file-names") config
Expand Down
4 changes: 2 additions & 2 deletions config/config-dist-and-src.coffee
Expand Up @@ -19,7 +19,7 @@ module.exports = (config, options) ->

file =
appServer: 'routes.js' # app server dist entry file
rbServer: 'server.js' # rb server dist bootstrap file
rbServerInit: 'init-server.js' # rb server dist bootstrap file

# dirs
# ====
Expand Down Expand Up @@ -149,7 +149,7 @@ module.exports = (config, options) ->
config[v1][v2].server.scripts.dir = config[v1][v2].server.dir

addToServerRbDist = ->
config.dist.rb.server.scripts.file = file.rbServer # rb server dist bootstrap file
config.dist.rb.server.scripts.file = file.rbServerInit # rb server dist bootstrap file
config.dist.rb.server.scripts.path = path.join(
config.app.dir
config.dist.rb.server.scripts.dir
Expand Down
18 changes: 11 additions & 7 deletions config/config-node_modules.coffee
Expand Up @@ -5,7 +5,7 @@ module.exports = (config, options) ->

# modules
# =======
rbModules = ['express', 'body-parser']
rbModules = ['body-parser', 'express', 'q']
appModules = options.server.node_modules or []

# http proxy
Expand All @@ -18,25 +18,29 @@ module.exports = (config, options) ->
rb:
modules: rbModules
dist: dir: null, modules: {}
src: dir: null, modules: {}
src: dir: null, relPath: null, modules: {}
app:
modules: appModules
dist: dir: null, modules: {}
src: dir: null, modules: {}
src: dir: null, relPath: null, modules: {}

# dist and src
# ============
addDistAndSrc = ->
nmDir = 'node_modules'
for appOrRb, v1 of node_modules
for k2, v2 of v1
continue unless k2 is 'dist' or k2 is 'src'
switch k2
when 'dist'
dir = config.dist[appOrRb].server.scripts.dir
v2.dir = path.join dir, 'node_modules'
dir = config.dist[appOrRb].server.scripts.dir
v2.dir = path.join dir, nmDir
when 'src'
dir = config[appOrRb].dir
v2.dir = path.join dir, 'node_modules'
dir = config[appOrRb].dir
v2.dir = path.join dir, nmDir
v2.relPath = nmDir
continue if appOrRb is 'app'
v2.relPath = path.join nmDir, config.rb.name, nmDir

addDistAndSrc()

Expand Down
31 changes: 0 additions & 31 deletions config/config-server.coffee

This file was deleted.

63 changes: 63 additions & 0 deletions helpers/jasmine.coffee
@@ -0,0 +1,63 @@
module.exports = (config) ->
q = require 'q'
path = require 'path'
Jasmine = require 'jasmine'
Reporter = require 'jasmine-terminal-reporter'
jasmineExpect = path.join config.node_modules.rb.src.relPath, 'jasmine-expect', 'index.js'

jasmine =
# properties
# ==========
defer: q.defer()
jasmine: null
results: status: null, total: 0, passed: 0, failed: 0, failedSpecs: []

# public
# ======
init: (files) ->
@_setJasmine()
._setConfig files
._setOnComplete()
._addReporter()
@

execute: -> # 5 seconds is the default spec timeout
@jasmine.execute()
@defer.promise

getResults: ->
@results

# private
# =======
_setJasmine: ->
@jasmine = new Jasmine()
@

_setConfig: (files) ->
@jasmine.loadConfig
spec_dir: ''
spec_files: files
helpers: [ jasmineExpect ]
@

_setOnComplete: (defer) ->
@jasmine.onComplete (passed) =>
@results.status = if passed then 'passed' else 'failed'
@defer.resolve()
@

_addReporter: ->
@jasmine.addReporter new Reporter
isVerbose: false
showColors: true
includeStackTrace: false

@jasmine.addReporter
specDone: (result) =>
@results.total++
return @results.passed++ if result.status is 'passed'
@results.failed++
@results.failedSpecs.push result.fullName
@

2 changes: 2 additions & 0 deletions init/rapid.coffee
Expand Up @@ -79,6 +79,7 @@ module.exports = (gulp, config) ->
"#{config.rb.prefix.task}common-server"
"#{config.rb.prefix.task}start-server"
"#{config.rb.prefix.task}common-test-server"
"#{config.rb.prefix.task}stop-server"
cb
) -> defer.resolve() unless task.wasCalledFrom config.rb.tasks['test']

Expand Down Expand Up @@ -111,6 +112,7 @@ module.exports = (gulp, config) ->
"#{config.rb.prefix.task}start-server"
"#{config.rb.prefix.task}common-test-server"
"#{config.rb.prefix.task}clean-server-test-dist"
"#{config.rb.prefix.task}stop-server"
cb
) -> defer.resolve() unless task.wasCalledFrom config.rb.tasks['prod:test']

Expand Down
5 changes: 3 additions & 2 deletions init/tasks.coffee
Expand Up @@ -91,9 +91,10 @@ module.exports = (gulp, config) ->
# server
# ======
require("#{config.req.tasks}/server/find-open-port") gulp, config
require("#{config.req.tasks}/server/start-server") gulp, config
require("#{config.req.tasks}/server/spawn-server") gulp, config
require("#{config.req.tasks}/server/nodemon") gulp, config, bs
require("#{config.req.tasks}/server/spawn-server") gulp, config
require("#{config.req.tasks}/server/start-server") gulp, config
require("#{config.req.tasks}/server/stop-server") gulp, config

# client test
# ===========
Expand Down
9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"body-parser": "1.14.1",
"bower": "1.5.3",
"browser-sync": "2.9.6",
"browser-sync": "2.9.7",
"coffee-script": "1.10.0",
"colors": "1.1.2",
"copy-paste": "1.1.3",
Expand All @@ -47,8 +47,7 @@
"gulp-cachebust": "0.0.6",
"gulp-coffee": "2.3.1",
"gulp-concat": "2.6.0",
"gulp-if": "1.2.5",
"gulp-jasmine": "2.1.0",
"gulp-if": "2.0.0",
"gulp-jsonminify": "1.0.0",
"gulp-less": "3.0.3",
"gulp-minify-css": "1.2.1",
Expand All @@ -64,7 +63,10 @@
"gulp-util": "3.0.6",
"gulp-watch": "4.3.5",
"http-proxy-middleware": "0.9.0",
"jasmine": "2.3.2",
"jasmine-core": "2.3.4",
"jasmine-expect": "2.0.0-beta2",
"jasmine-terminal-reporter": "1.0.0",
"karma": "0.13.10",
"karma-jasmine": "0.3.6",
"karma-jasmine-matchers": "2.0.0-beta2",
Expand All @@ -75,6 +77,7 @@
"karma-safari-launcher": "0.1.1",
"lodash": "3.10.1",
"open": "0.0.5",
"phantomjs": "1.9.18",
"postcss": "5.0.8",
"postcss-import": "7.0.0",
"q": "1.4.1",
Expand Down
15 changes: 15 additions & 0 deletions src/server/app-defaults.coffee
@@ -0,0 +1,15 @@
# default app configuration
# =========================
module.exports = (server, config) ->
app = server.app
spa = config.spa.dist.file # ex: spa.html

app.set 'x-powered-by', false # removes this http header
app.use server.express.static server.paths.client
app.use server.middleware.bodyParser.json() # parse application/json

app.get '/', (req, res) ->
msg = 'Hello Server!'
return res.send msg unless config.build.client
return res.send msg if config.exclude.spa
res.sendFile spa, root: server.paths.client
9 changes: 6 additions & 3 deletions src/server/http-proxy.coffee
@@ -1,5 +1,4 @@
module.exports = (app, config, opts) ->
return if not config.httpProxy.length
module.exports = (app, config) ->
proxyMidware = require 'http-proxy-middleware' # express middleware
proxies = []

Expand All @@ -11,4 +10,8 @@ module.exports = (app, config, opts) ->

# add middleware to express app
# =============================
app.use proxies
app.use proxies

# return middleware
# =================
proxyMidware
36 changes: 36 additions & 0 deletions src/server/init-server.coffee
@@ -0,0 +1,36 @@
dir = __dirname # all paths are relative to this file
path = require 'path'
express = require 'express'
bodyParser = require 'body-parser' # middleware for parsing the req.body
config = require path.join dir, 'config.json'
appFilePath = path.resolve dir, '..', config.dist.app.server.scripts.file
clientDirPath = path.resolve dir, '..', '..', config.dist.app.client.dirName # creates absolute path to the client folder
serverDirPath = path.resolve dir, '..', '..', config.dist.app.server.dirName

# create the server object
# ========================
server = require './server'
server.express = express
server.app = server.express()
server.middleware = { bodyParser }
server.paths = client: clientDirPath, server: serverDirPath
server.server = require('./start-server') server.app, config
require('./app-defaults') server, config

# load optional http proxy
# ========================
proxyFilePath = path.join dir, 'http-proxy.js'
require(proxyFilePath) server.app, config if config.httpProxy.length

# load optional app server dist entry script
# ==========================================
try require(appFilePath) server
catch e
if e.code and
e.code.toLowerCase() is 'module_not_found' and
e.message.indexOf(appFilePath) isnt -1
console.log 'No application server scripts to load.'
else
console.log e # log e if there is an actual error


51 changes: 3 additions & 48 deletions src/server/server.coffee
@@ -1,48 +1,3 @@
dir = __dirname # all paths are relative to this file
path = require 'path'
express = require 'express'
bodyParser = require 'body-parser'
config = require path.join dir, 'config.json'
app = express()
port = process.env.PORT or config.ports.server
spa = config.spa.dist.file # ex: spa.html
clientDirPath = path.resolve dir, '..', '..', config.dist.app.client.dirName # creates absolute path to the client folder
appFilePath = path.resolve dir, '..', config.dist.app.server.scripts.file
serverDirPath = path.resolve dir, '..', '..', config.dist.app.server.dirName
proxyFilePath = path.join dir, 'http-proxy.js'

# configure
# =========
app.use express.static clientDirPath
app.use bodyParser.json() # parse application/json

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

app.get '/', (req, res) ->
msg = 'Hello Server!'
return res.send msg unless config.build.client
return res.send msg if config.exclude.spa
res.sendFile spa, root: clientDirPath

# options to pass
# ===============
opts =
path:
client: clientDirPath
server: serverDirPath

# load optional http proxy
# ========================
require(proxyFilePath) app, config, opts if config.httpProxy.length

# load optional app server dist entry script
# ==========================================
try require(appFilePath) app, opts
catch e
if e.code and
e.code.toLowerCase() is 'module_not_found' and
e.message.indexOf(appFilePath) isnt -1
console.log config.server.msg.noScripts
else
console.log e # log e if there is an actual error
# Dynamically populated in init-server.js
# =======================================
module.exports = {}
7 changes: 7 additions & 0 deletions src/server/start-server.coffee
@@ -0,0 +1,7 @@
module.exports = (app, config) ->
port = process.env.PORT or config.ports.server

# must return server
# ==================
server = app.listen port, ->
console.log "Server started on port #{port}"
11 changes: 11 additions & 0 deletions src/server/stop-server.coffee
@@ -0,0 +1,11 @@
module.exports = ->
q = require 'q'
server = require('./server').server
port = server.address().port

defer = q.defer()
server.close ->
console.log "Server stopped on port #{port}"
defer.resolve()

defer.promise
1 change: 0 additions & 1 deletion tasks/server/find-open-port.coffee
Expand Up @@ -2,7 +2,6 @@
# =========================================
module.exports = (gulp, config) ->
q = require 'q'
fse = require 'fs-extra'
findPort = require 'find-port'
configHelp = require("#{config.req.helpers}/config") config

Expand Down
5 changes: 2 additions & 3 deletions tasks/server/nodemon.coffee
@@ -1,7 +1,6 @@
module.exports = (gulp, config, browserSync) ->
q = require 'q'
path = require 'path'
nodemon = require 'gulp-nodemon'
q = require 'q'
nodemon = require 'gulp-nodemon'

# globals
# =======
Expand Down

0 comments on commit 9a2f9e9

Please sign in to comment.