Skip to content
Permalink
Browse files

feat(server): expose dist start and stop server scripts, can be calle…

…d with npm scripts

start script: dist/server/rapid-build/start-server
stop script: dist/server/rapid-build/stop-server
  • Loading branch information
jyounce committed Apr 12, 2016
1 parent 1c82f3c commit c1ed95f8711c38ce8c6282f4da391218785cc578
@@ -15,6 +15,8 @@ getCommonOptions = ->
minify:
spa:
file: false
css:
splitMinFile: false
html:
options:
ignoreCustomFragments: [
@@ -6,5 +6,9 @@
"name": "Judd Younce",
"email": "jyounce@yahoo.com",
"url": "https://github.com/jyounce"
},
"scripts": {
"start": "node dist/server/rapid-build/start-server",
"stop": "node dist/server/rapid-build/stop-server"
}
}
@@ -19,7 +19,9 @@ module.exports = (config, options) ->

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

# dirs
# ====
@@ -149,15 +151,17 @@ module.exports = (config, options) ->
config[v1][v2].server.scripts.dir = config[v1][v2].server.dir

addToServerRbDist = ->
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
)
config.dist.rb.server.scripts.filePath = path.join(
config.dist.rb.server.scripts.path
config.dist.rb.server.scripts.file
)

config.dist.rb.server.scripts.startFile = file.rbServer.start
config.dist.rb.server.scripts.stopFile = file.rbServer.stop

_path = config.dist.rb.server.scripts.path
config.dist.rb.server.scripts.start = path.join _path, file.rbServer.start
config.dist.rb.server.scripts.stop = path.join _path, file.rbServer.stop

addToServerAppDist = ->
config.dist.app.server.scripts.file = # app server dist entry file
@@ -28,6 +28,9 @@ module.exports = (config) ->
generated.pkg.files.prodFilesBlueprint = join generated.pkg.files.path, 'prod-files-blueprint.json'
generated.pkg.src.dir = 'src'
generated.pkg.src.path = join generated.pkg.path, generated.pkg.src.dir
generated.pkg.src.server = {}
generated.pkg.src.server.path = join generated.pkg.src.path, 'server'
generated.pkg.src.server.info = join generated.pkg.src.server.path, 'server-info.json'

# add generated to config
# =======================
@@ -5,7 +5,7 @@ module.exports = (config, options) ->

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

# http proxy
@@ -60,6 +60,7 @@ module.exports = (gulp, config) ->
taskHelp.addTask 'copy-js:server', '/copy/copy-js', loc: 'server'
taskHelp.addTask 'copy-libs', '/copy/copy-libs'
taskHelp.addTask 'copy-server-config', '/copy/copy-server-config'
taskHelp.addTask 'copy-server-info', '/copy/copy-server-info'
taskHelp.addTask 'copy-server-node_modules', '/copy/copy-server-node_modules'
taskHelp.addTask 'copy-views', '/copy/copy-views', taskCB: true

@@ -0,0 +1,7 @@
module.exports = (server, config) ->
port = process.env.PORT or config.ports.server

# must return
# ===========
server.app.listen port, ->
console.log "Server started on port #{port}"

This file was deleted.

@@ -0,0 +1,4 @@
{
"pid": null,
"port": null
}

This file was deleted.

@@ -1,7 +1,51 @@
module.exports = (app, config) ->
port = process.env.PORT or config.ports.server
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

# set NODE_ENV (conditionally)
# ============================
process.env.NODE_ENV = 'production' if config.env.is.prod

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

# load defaults
# =============
require('./defaults/app-settings') server, config
require('./defaults/app-middleware') server, config

# load optional http proxy
# ========================
proxyFilePath = path.join dir, 'options', '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

# load default app configuration
# ==============================
require('./defaults/app-routes') server, config

# run tasks
# =========
require('./tasks/build-server-info') server

# must return server
# ==================
server = app.listen port, ->
console.log "Server started on port #{port}"
@@ -1,11 +1,13 @@
module.exports = ->
q = require 'q'
server = require('./server').server
port = server.address().port
# server-info is dynamically populated from:
# start-server > build-server-info
# ==========================================
info = require './server-info'
msg = null

defer = q.defer()
server.close ->
console.log "Server stopped on port #{port}"
defer.resolve()
try
process.kill info.pid
msg = "Server stopped on port #{info.port}"
catch e
msg = "Failed to stop the server. #{e.message}"

defer.promise
console.log msg
@@ -0,0 +1,9 @@
module.exports = (server) ->
path = require 'path'
fse = require 'fs-extra'
jsonFile = path.resolve __dirname, '..', 'server-info.json'
json =
pid: process.pid
port: server.server.address().port

fse.writeJSONSync jsonFile, json, spaces: '\t'
@@ -14,6 +14,7 @@ module.exports = (config, gulp, taskOpts={}) ->
"#{config.rb.prefix.task}coffee:server"
"#{config.rb.prefix.task}es6:server"
"#{config.rb.prefix.task}copy-server-config"
"#{config.rb.prefix.task}copy-server-info"
"#{config.rb.prefix.task}copy-server-node_modules"
"#{config.rb.prefix.task}compile-extra-less:server"
"#{config.rb.prefix.task}compile-extra-sass:server"
@@ -0,0 +1,22 @@
module.exports = (config, gulp) ->
q = require 'q'
promiseHelp = require "#{config.req.helpers}/promise"

# API
# ===
api =
runTask: ->
return promiseHelp.get() if config.exclude.default.server.files
defer = q.defer()
src = config.generated.pkg.src.server.info
dest = config.dist.rb.server.scripts.dir
gulp.src src
.pipe gulp.dest dest
.on 'end', ->
# console.log dest
defer.resolve()
defer.promise

# return
# ======
api.runTask()
@@ -1,17 +1,22 @@
module.exports = (config) ->
q = require 'q'
path = require 'path'
nodemon = require 'gulp-nodemon'

# globals
# =======
rbServerFile = config.dist.rb.server.scripts.filePath
rbServerFile = config.dist.rb.server.scripts.start
watchDir = config.dist.app.server.scripts.dir
ignoreDirs = [

serverInfo = path.join config.dist.rb.server.scripts.dir, 'server-info.json'
ignoreFiles = [ serverInfo ]
ignoreDirs = [
config.node_modules.rb.dist.dir
config.node_modules.app.dist.dir
config.dist.rb.server.test.dir
config.dist.app.server.test.dir
]
ignore = [].concat ignoreFiles, ignoreDirs

# API
# ===
@@ -22,7 +27,7 @@ module.exports = (config) ->
script: rbServerFile
ext: 'js json'
watch: watchDir # todo: watch isn't restarting on file deletion
ignore: ignoreDirs
ignore: ignore

.on 'start', ->
browserSync = require "#{config.req.tasks}/browser/browser-sync"
@@ -1,6 +1,6 @@
module.exports = (config) ->
q = require 'q'
rbServerFile = config.dist.rb.server.scripts.filePath
rbServerFile = config.dist.rb.server.scripts.start

# API
# ===
@@ -2,7 +2,7 @@ module.exports = (config) ->
q = require 'q'
path = require 'path'
promiseHelp = require "#{config.req.helpers}/promise"
stopServerFile = path.join config.dist.rb.server.scripts.path, 'stop-server.js'
stopServerFile = config.dist.rb.server.scripts.stop

# API
# ===
@@ -12,7 +12,7 @@ module.exports = (config) ->
return promiseHelp.get() if config.exclude.default.server.files
defer = q.defer()
stopServer = require stopServerFile
stopServer().done -> defer.resolve()
defer.resolve()
defer.promise

# return

0 comments on commit c1ed95f

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