Skip to content
Permalink
Browse files

feat(config): add config.server.node_modules option to copy node_modu…

…les to server dist
  • Loading branch information
jyounce committed Jul 23, 2015
1 parent 5093741 commit a25a3f5db2b1a7b8afb4174b37f32ca0885ffa7c
@@ -142,6 +142,7 @@ gulp rapid-build:test:prod
# exclude.from.minFile[scripts|styles] = (array of strings) = file paths: exclude script or style files from automatically being generated in the scripts.min.js or styles.min.css file
# exclude.from.spaFile[scripts|styles] = (array of strings) = file paths: exclude script or style files from automatically being generated in the spa.html file
# test.browsers = (array of browser names) = phantomjs will run by default, optional browser names are ['chrome', 'firefox', 'ie', 'safari']
# server.node_modules = (array of module names) = node_modules you would like to copy to the server dist, example: ['q']
# =============================================================================================================================================================================================================================
```

@@ -22,7 +22,7 @@ module.exports = (rbDir, options) ->
config = require("#{config.req.config}/config-exclude") config, options
config = require("#{config.req.config}/config-templates") config
config = require("#{config.req.config}/config-temp") config
config = require("#{config.req.config}/config-node_modules") config
config = require("#{config.req.config}/config-node_modules") config, options
config = require("#{config.req.config}/config-order") config, options
config = require("#{config.req.config}/config-globs") config
config = require("#{config.req.config}/config-bower") config, options
@@ -186,14 +186,20 @@ module.exports = (config) ->

# node_modules
# ============
addNodeModulesDistAndSrc = ->
for appOrRb, v of config.node_modules
glob.node_modules[appOrRb] = {}
for loc in ['dist', 'src']
glob.node_modules[appOrRb][loc] = {}

addNodeModules = (loc) ->
for module in config.node_modules.modules
glob.node_modules[loc][module] =
path.join pathHelp.format(config.node_modules.src.modules[module]), lang.all
for appOrRb, v of config.node_modules
for module in v.modules
_path = pathHelp.format v[loc].modules[module]
glob.node_modules[appOrRb][loc][module] = path.join _path, lang.all

glob.node_modules = {}
glob.node_modules.dist = {}
glob.node_modules.src = {}
addNodeModulesDistAndSrc()
addNodeModules 'src'

# browser sync
@@ -1,26 +1,49 @@
module.exports = (config) ->
module.exports = (config, options) ->
path = require 'path'
log = require "#{config.req.helpers}/log"
test = require("#{config.req.helpers}/test")()

# modules
# =======
rbModules = ['express']
appModules = options.server.node_modules or []

# init node_modules
# =================
node_modules =
modules: ['express']
dist:
dir: path.join config.dist.rb.server.scripts.dir, 'node_modules'
modules: {}
src:
dir: path.join config.rb.dir, 'node_modules'
modules: {}
rb:
modules: rbModules
dist: dir: null, modules: {}
src: dir: null, modules: {}
app:
modules: appModules
dist: dir: null, modules: {}
src: dir: null, modules: {}

# dist and src
# ============
addDistAndSrc = ->
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'
when 'src'
dir = config[appOrRb].dir
v2.dir = path.join dir, 'node_modules'

addDistAndSrc()

# modules to copy to server dist
# ==============================
addModulesLocPaths = ->
for module in node_modules.modules
for loc in ['dist', 'src']
_path = path.join node_modules[loc].dir, module
node_modules[loc].modules[module] = _path
addModulesLocPaths = (appOrRb) ->
for appOrRb, v of node_modules
for module in node_modules[appOrRb].modules
for loc in ['dist', 'src']
_path = path.join node_modules[appOrRb][loc].dir, module
node_modules[appOrRb][loc].modules[module] = _path

addModulesLocPaths()

@@ -20,9 +20,6 @@ module.exports = (config, options) ->
options[v1][v2][v3] = {} if not isType.object options[v1][v2][v3]
options[v1][v2][v3].dir = null if not isType.string options[v1][v2][v3].dir

serverOptions = -> # app server dist entry file
options.dist.server.fileName = null if not isType.string options.dist.server.fileName

portOptions = -> # server ports
options.ports = {} if not isType.object options.ports
options.ports.server = null if not isType.number options.ports.server
@@ -93,17 +90,25 @@ module.exports = (config, options) ->
options.test = {} if not isType.object options.test
options.test.browsers = null if not isType.array options.test.browsers

serverDistOptions = -> # app server dist entry file
options.dist.server.fileName = null if not isType.string options.dist.server.fileName

serverOptions = ->
options.server = {} if not isType.object options.server
options.server.node_modules = null if not isType.array options.server.node_modules

# init
# ====
distAndSrcOptions() # must be first
serverOptions()
portOptions()
orderOptions()
angularOptions()
spaOptions()
minifyOptions()
excludeOptions()
testOptions()
serverDistOptions()
serverOptions()

# logs
# ====
@@ -1,27 +1,30 @@
module.exports = (gulp, config) ->
q = require 'q'

runTask = (src, dest) ->
copyTask = (src, dest) ->
defer = q.defer()
gulp.src src
.pipe gulp.dest dest
.on 'end', ->
defer.resolve()
defer.promise

runTasks = (modules) ->
runTask = ->
tasks = []
defer = q.defer()
modules.forEach (module) ->
tasks.push ->
runTask(
config.glob.node_modules.src[module]
config.node_modules.dist.modules[module]
)

for appOrRb, v of config.node_modules
for module in v.modules
src = config.glob.node_modules[appOrRb].src[module]
dest = v.dist.modules[module]
do (src, dest) ->
tasks.push ->
copyTask src, dest

tasks.reduce(q.when, q()).done -> defer.resolve()
defer.promise

# register task
# =============
gulp.task "#{config.rb.prefix.task}copy-server-node_modules", ->
runTasks config.node_modules.modules
runTask()
@@ -3,21 +3,27 @@ module.exports = (gulp, config, browserSync) ->
path = require 'path'
nodemon = require 'gulp-nodemon'

# globals
# =======
rbServerFile = path.join(
config.dist.rb.server.scripts.dir,
config.dist.rb.server.scripts.file
)
watchDir = config.dist.app.server.scripts.dir
ignoreDirs = [
config.node_modules.rb.dist.dir
config.node_modules.app.dist.dir
]

# register task
# =============
gulp.task "#{config.rb.prefix.task}nodemon", ->
defer = q.defer()
nodemon
script: rbServerFile
ext: 'js json'
# todo: watch isn't restarting on file deletion
watch: config.dist.app.server.scripts.dir
ignore: config.node_modules.dist.dir
ext: 'js json'
watch: watchDir # todo: watch isn't restarting on file deletion
ignore: ignoreDirs

.on 'start', ->
browserSync.emitter._events.serverRestart() if browserSync

0 comments on commit a25a3f5

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