Skip to content

Commit

Permalink
feat(server): add quick server startup with spawn server and use node…
Browse files Browse the repository at this point in the history
…mon for dev build
  • Loading branch information
jyounce committed Jul 28, 2015
1 parent 19ce257 commit cd289bf
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions config.coffee
Expand Up @@ -14,6 +14,7 @@ module.exports = (rbDir, options) ->
config = require("#{config.req.config}/config-rb") config, rbDir
config = require("#{config.req.config}/config-app") 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-file-names") config
config = require("#{config.req.config}/config-minify") config, options
config = require("#{config.req.config}/config-dist-and-src") config, options
Expand Down
8 changes: 6 additions & 2 deletions config/config-dist-and-src.coffee
Expand Up @@ -18,8 +18,8 @@ module.exports = (config, options) ->
views: 'views'

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

# dirs
# ====
Expand Down Expand Up @@ -140,6 +140,10 @@ module.exports = (config, options) ->
removeServerDir() # server dir is server.scripts.dir
addToServerAppDist() # add file and path
config.dist.rb.server.scripts.file = file.rbServer # rb server dist bootstrap file
config.dist.rb.server.scripts.path = path.join(
config.dist.rb.server.scripts.dir
config.dist.rb.server.scripts.file
)

# final touch-ups
# ===============
Expand Down
31 changes: 31 additions & 0 deletions config/config-server.coffee
@@ -0,0 +1,31 @@
module.exports = (config) ->
log = require "#{config.req.helpers}/log"
test = require("#{config.req.helpers}/test")()

# init server
# ===========
server = {}

# messages
# ========
server.msg =
start: "Server started on #{config.ports.server}"
noScripts: 'No application server scripts to load.'

# add server to config
# ====================
config.server = server

# logs
# ====
# log.json server, 'server ='

# tests
# =====
test.log 'true', config.server, 'add server to config'

# return
# ======
config


2 changes: 1 addition & 1 deletion init/rapid.coffee
Expand Up @@ -20,7 +20,7 @@ module.exports = (gulp, config) ->
gulp.task config.rb.tasks.dev, ["#{config.rb.prefix.task}common"], (cb) ->
gulpSequence(
"#{config.rb.prefix.task}build-spa"
"#{config.rb.prefix.task}start-server"
"#{config.rb.prefix.task}start-server:dev"
"#{config.rb.prefix.task}browser-sync"
"#{config.rb.prefix.task}watch"
cb
Expand Down
1 change: 1 addition & 0 deletions init/tasks.coffee
Expand Up @@ -72,6 +72,7 @@ module.exports = (gulp, config) ->
# server
# ======
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

# test
Expand Down
4 changes: 2 additions & 2 deletions src/server/server.coffee
Expand Up @@ -11,7 +11,7 @@ appFilePath = path.join appPath, appFile # app server dist entry script

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

app.get '/', (req, res) ->
res.sendFile spa, root:client
Expand All @@ -30,6 +30,6 @@ catch e
if e.code and
e.code.toLowerCase() is 'module_not_found' and
e.message.indexOf(appFilePath) isnt -1
console.log 'no app server scripts to load'
console.log config.server.msg.noScripts
else
console.log e # log e if there is an actual error
7 changes: 2 additions & 5 deletions tasks/server/nodemon.coffee
Expand Up @@ -5,11 +5,8 @@ module.exports = (gulp, config, browserSync) ->

# globals
# =======
rbServerFile = path.join(
config.dist.rb.server.scripts.dir,
config.dist.rb.server.scripts.file
)
watchDir = config.dist.app.server.scripts.dir
rbServerFile = config.dist.rb.server.scripts.path
watchDir = config.dist.app.server.scripts.dir
ignoreDirs = [
config.node_modules.rb.dist.dir
config.node_modules.app.dist.dir
Expand Down
15 changes: 15 additions & 0 deletions tasks/server/spawn-server.coffee
@@ -0,0 +1,15 @@
module.exports = (gulp, config) ->
q = require 'q'
spawn = require('child_process').spawn
rbServerFile = config.dist.rb.server.scripts.path

# register task
# =============
gulp.task "#{config.rb.prefix.task}spawn-server", ->
defer = q.defer()
server = spawn 'node', [ rbServerFile ]
server.stdout.on 'data', (data) ->
msg = data.toString().trim()
console.log msg.yellow
defer.resolve() if msg.indexOf(config.server.msg.start) isnt -1
defer.promise
4 changes: 3 additions & 1 deletion tasks/server/start-server.coffee
@@ -1,9 +1,11 @@
module.exports = (gulp, config) ->
# task deps
# =========
taskDeps = ["#{config.rb.prefix.task}nodemon"]
taskDeps = ["#{config.rb.prefix.task}spawn-server"]
taskDepsDev = ["#{config.rb.prefix.task}nodemon"]

# register task
# =============
gulp.task "#{config.rb.prefix.task}start-server", taskDeps
gulp.task "#{config.rb.prefix.task}start-server:dev", taskDepsDev

0 comments on commit cd289bf

Please sign in to comment.