Skip to content
Permalink
Browse files

feat(angular): add option angular.httpBackend to enable angular mocks

  • Loading branch information
jyounce committed Jun 26, 2015
1 parent 87a6348 commit 4688281fbc0ecac60cb02c6a384532b4eec4108e
@@ -110,9 +110,12 @@ gulp rapid-build:prod
# ports.server = (int) defaults to 3000
# ports.reload = (int) defaults to 3001
# order[scripts|styles][first|last] = (array of strings) = file paths
# angular.modules = (array of strings) = additional angular modules to load, already loaded are ['ngResource', 'ngRoute', 'ngSanitize']
# angular.modules = (array of strings) = additional angular modules to load, already loaded are ['ngResource', 'ngRoute', 'ngSanitize'] and 'ngMockE2E' based on angular.httpBackend options
# angular.moduleName = (string) defaults to 'app' = application module name, value for ng-app
# angular.version = (string) defaults to '1.x' = semver version required
# angular.httpBackend.dev = (boolean) defaults to false = set to true to enable httpBackend for dev and default build
# angular.httpBackend.prod = (boolean) defaults to false = set to true to enable httpBackend for prod build
# angular.httpBackend.dir = (string) defaults to 'mocks' = directory inside your client scripts directory
# angular.templateCache.dev.enable = (boolean) defaults to false = use template cache when running default and dev task
# angular.templateCache.useAbsolutePaths = (boolean) defaults to false = prefix template urls with a '/'
# angular.exclude.files = (boolean) defaults to false = set to true to exclude angular files (lib and modules)
@@ -127,7 +130,7 @@ gulp rapid-build:prod
# minify.html.templateCache = (boolean) defaults to true = for prod build, use the template cache
# minify.js.scripts = (boolean) defaults to true = for prod build, minify the js
# minify.js.mangle = (boolean) defaults to true = for prod build, mangle the names in the js
# ==========================================================================================================================================================================================================
# ========================================================================================================================================================================================================================
```

## Build Modes
@@ -14,9 +14,9 @@ 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-file-names") config
config = require("#{config.req.config}/config-angular") config, options
config = require("#{config.req.config}/config-minify") config, options
config = require("#{config.req.config}/config-dist-and-src") config, options
config = require("#{config.req.config}/config-angular") config, options
config = require("#{config.req.config}/config-spa") config, options
config = require("#{config.req.config}/config-templates") config
config = require("#{config.req.config}/config-temp") config
@@ -1,15 +1,26 @@
module.exports = (config, options) ->
path = require 'path'
log = require "#{config.req.helpers}/log"
test = require("#{config.req.helpers}/test")()

# default modules
# ===============
modules = ['ngResource', 'ngRoute', 'ngSanitize']
# order matters because of 'ngMockE2E'
# ====================================
modules = ['ngMockE2E', 'ngResource', 'ngRoute', 'ngSanitize']

# init angular
# ============
angular = {}

# httpBackend
# ===========
httpBackendDir = options.angular.httpBackend.dir or 'mocks'
httpBackendDir = path.join config.src.app.client.scripts.dir, httpBackendDir
angular.httpBackend = {}
angular.httpBackend.dev = options.angular.httpBackend.dev or false
angular.httpBackend.prod = options.angular.httpBackend.prod or false
angular.httpBackend.dir = httpBackendDir

# modules
# =======
angular.modules = options.angular.modules or []
@@ -39,10 +50,22 @@ module.exports = (config, options) ->
# =====================
angular.bowerDeps =
'angular': angular.version
'angular-mocks': angular.version
'angular-resource': angular.version
'angular-route': angular.version
'angular-sanitize': angular.version

# methods
# =======
removeRbMocksModule = -> # helper
angular.modules.splice 0, 1

angular.removeRbMocksModule = ->
if config.env.is.prod
removeRbMocksModule() if not angular.httpBackend.prod
else if not angular.httpBackend.dev
removeRbMocksModule()

# add angular to config
# =====================
config.angular = angular
@@ -2,14 +2,45 @@ module.exports = (config) ->
log = require "#{config.req.helpers}/log"
test = require("#{config.req.helpers}/test")()

# helpers
# =======
resetIsEnv = ->
for own k, v of env.is
env.is[k] = false

setIsEnv = ->
resetIsEnv()
name = config.env.name
switch name
when 'default'
env.is.default = true
env.is.defaultOrDev = true
when 'dev'
env.is.dev = true
env.is.defaultOrDev = true
when 'prod'
env.is.prod = true

# init env
# ========
env = {}
env.name = 'default'
env.set = (gulp) -> # called in "#{config.rb.prefix.task}common"
switch gulp.seq[1]

# is
# ==
env.is =
default: true
defaultOrDev: true
dev: false
prod: false

# methods
# =======
env.set = (gulp) ->
switch gulp.seq[2] # called in 'rb-update-config' which is called first in 'rb-common'
when config.rb.tasks.dev then config.env.name = 'dev'
when config.rb.tasks.prod then config.env.name = 'prod'
setIsEnv()

# add env to config
# =================
@@ -68,16 +68,33 @@ module.exports = (config) ->

# dist
# ====
addGlob 'dist', 'bower', ['all', 'css', 'js']
addGlob 'dist', 'bower', ['all', 'css', 'js'] # css and js are for concat-scripts-and-styles task
addGlob 'dist', 'images', ['all']
addGlob 'dist', 'libs', ['all', 'css', 'js']
addGlob 'dist', 'libs', ['all', 'css', 'js'] # css and js are for concat-scripts-and-styles task
addGlob 'dist', 'scripts', ['all']
addGlob 'dist', 'scripts', ['js' ], true, true
addGlob 'dist', 'styles', ['all']
addGlob 'dist', 'styles', ['css'], true, true
addGlob 'dist', 'views', ['all']
addGlob 'dist', 'views', ['html']

# methods
# =======
removeAppAngularMocksDir = ->
srcScripts = glob.src.app.client.scripts
noMocksGlob = "!#{config.angular.httpBackend.dir}#{lang.all}"
for own k, v of srcScripts
srcScripts[k] = [v, noMocksGlob]

removeRbAngularMocks = -> # helper
glob.dist.rb.client.scripts.js.splice 1, 1
removeAppAngularMocksDir()

glob.removeRbAngularMocks = ->
if config.env.is.prod
removeRbAngularMocks() if not config.angular.httpBackend.prod
else if not config.angular.httpBackend.dev
removeRbAngularMocks()

# loading order for scripts and styles
# ====================================
@@ -18,6 +18,9 @@
# angular.modules = (array) additional angular modules
# angular.version = (string) defaults to '1.x'
# angular.moduleName = (string) application module name
# angular.httpBackend.dev = (boolean) defaults to false
# angular.httpBackend.prod = (boolean) defaults to false
# angular.httpBackend.dir = (string) defaults to 'mocks'
# angular.templateCache.dev.enable = (boolean) defaults to false
# angular.templateCache.useAbsolutePaths = (boolean) defaults to false
# angular.exclude.files = (boolean) defaults to false
@@ -77,6 +80,10 @@ module.exports = (config, options) ->
options.angular.moduleName = null if not isType.string options.angular.moduleName
options.angular.exclude = {} if not isType.object options.angular.exclude
options.angular.exclude.files = null if not isType.boolean options.angular.exclude.files
options.angular.httpBackend = {} if not isType.object options.angular.httpBackend
options.angular.httpBackend.dev = null if not isType.boolean options.angular.httpBackend.dev
options.angular.httpBackend.prod = null if not isType.boolean options.angular.httpBackend.prod
options.angular.httpBackend.dir = null if not isType.string options.angular.httpBackend.dir
options.angular.templateCache = {} if not isType.object options.angular.templateCache
options.angular.templateCache.useAbsolutePaths = null if not isType.boolean options.angular.templateCache.useAbsolutePaths
options.angular.templateCache.dev = {} if not isType.object options.angular.templateCache.dev
@@ -4,13 +4,29 @@ module.exports = (config, options) ->
pathHelp = require "#{config.req.helpers}/path"
test = require("#{config.req.helpers}/test")()

# rb angular files in order
# =========================
angularFiles = [
'angular.js'
'angular-mocks.js'
'angular-resource.js'
'angular-route.js'
'angular-sanitize.js'
]

# helpers
# =======
getDirName = (appOrRb, type) ->
dir = pathHelp.format config.dist[appOrRb].client[type].dir
i = dir.lastIndexOf('/') + 1
dir.substr i

getAngularFiles = ->
_paths = []
angularFiles.forEach (file, i) ->
_paths.push "#{rb.bower}/#{file}"
_paths

# defaults
# ========
rb =
@@ -20,13 +36,8 @@ module.exports = (config, options) ->
styles: getDirName 'rb', 'styles'

rb.files =
rb: ["#{rb.scripts}/app"]
angular: [
"#{rb.bower}/angular"
"#{rb.bower}/angular-resource"
"#{rb.bower}/angular-route"
"#{rb.bower}/angular-sanitize"
]
rb: ["#{rb.scripts}/app.js"]
angular: getAngularFiles()

# init order
# =========
@@ -48,6 +59,17 @@ module.exports = (config, options) ->
files = files.concat rb.files.angular if not options.angular.exclude.files
order.rb.scripts.first = files.concat rb.files.rb

# methods
# =======
removeRbAngularMocks = -> # helper
order.rb.scripts.first.splice 1, 1

order.removeRbAngularMocks = ->
if config.env.is.prod
removeRbAngularMocks() if not config.angular.httpBackend.prod
else if not config.angular.httpBackend.dev
removeRbAngularMocks()

# process order
# =============
removeExts = ->
@@ -1,4 +1,5 @@
# API
# bowerHelper.has.bower appOrRb
# bowerHelper.has.installed.pkg 'angular', appOrRb
# bowerHelper.get.json.from.appOrRb appOrRb
# bowerHelper.get.json.from.pkg 'angular', appOrRb
@@ -8,8 +9,8 @@
# bowerHelper.get.installed.pkgs appOrRb
# bowerHelper.get.installed.pkg 'angular', appOrRb
# bowerHelper.get.missing.pkgs appOrRb
# bowerHelper.get.src.pkgs appOrRb
# =======================================
# bowerHelper.get.src appOrRb
# ================================================
module.exports = (config) ->
fs = require 'fs'
path = require 'path'
@@ -138,6 +139,26 @@ module.exports = (config) ->
fileHelp.write.json _path, bowerJson if force
force

_removeRbAngularMocks: (paths) ->
key = 'angular-mocks'
if paths.absolute.length
index = null
paths.absolute.forEach (_path, i) ->
return index = i if _path.indexOf(key) isnt -1
return if index is null
paths.absolute.splice index, 1 # remove from absolutes
for own k, v of paths.relative
if v.indexOf(key) isnt -1
delete paths.relative[k] # remove from relatives
break

removeRbAngularMocks: (loc, paths) ->
return if loc isnt 'rb'
if config.env.is.prod
@_removeRbAngularMocks paths if not config.angular.httpBackend.prod
else if not config.angular.httpBackend.dev
@_removeRbAngularMocks paths

me =
has:
bower: (loc='rb') ->
@@ -237,9 +258,13 @@ module.exports = (config) ->
name = if hasPath then pkg.name else ''
relPaths[file.path] = path.join name, file.file
absPaths.push file.path
paths:
absolute: absPaths # []
relative: relPaths # {}
paths =
paths:
absolute: absPaths # []
relative: relPaths # {}
helper.removeRbAngularMocks loc, paths.paths # conditionally
# log.json paths
paths



@@ -0,0 +1,5 @@
module.exports =
get: (defer) ->
defer = require('q').defer() if not defer
defer.resolve()
defer.promise
@@ -25,7 +25,7 @@ module.exports = (gulp, config) ->
require("#{config.req.tasks}/clean/cleanup-client") gulp, config # cleanup-client

# common
# =======
# ======
require("#{config.req.tasks}/common") gulp, config # common

# compile
@@ -34,6 +34,10 @@ module.exports = (gulp, config) ->
require("#{config.req.tasks}/compile/es6") gulp, config # es6
require("#{config.req.tasks}/compile/less") gulp, config # less

# config
# ======
require("#{config.req.tasks}/config/update-config") gulp, config # update-config

# copy
# ====
require("#{config.req.tasks}/copy/copy-bower_components") gulp, config # copy-bower_components
@@ -40,7 +40,7 @@
"gulp-if": "~1.2.5",
"gulp-jsonminify": "~1.0.0",
"gulp-less": "~3.0.3",
"gulp-minify-css": "~1.1.6",
"gulp-minify-css": "~1.2.0",
"gulp-minify-html": "~1.0.3",
"gulp-nodemon": "~2.0.3",
"gulp-plumber": "~1.0.1",
@@ -4,12 +4,12 @@ module.exports = (gulp, config) ->
# register task
# =============
gulp.task "#{config.rb.prefix.task}common", (cb) ->
config.env.set gulp
gulpSequence(
"#{config.rb.prefix.task}update-config" # must be first and only called here
"#{config.rb.prefix.task}bower"
"#{config.rb.prefix.task}clean-dist"
"#{config.rb.prefix.task}build-config"
"#{config.rb.prefix.task}build-angular-modules"
"#{config.rb.prefix.task}build-config"
[
"#{config.rb.prefix.task}copy-bower_components"
"#{config.rb.prefix.task}copy-css"
@@ -0,0 +1,27 @@
# Task is only called from the common task.
# =========================================
module.exports = (gulp, config) ->
promiseHelp = require "#{config.req.helpers}/promise"

# register task
# =============
gulp.task "#{config.rb.prefix.task}update-config", ->
# env
# ===
config.env.set gulp # must be first

# angular
# =======
config.angular.removeRbMocksModule() # conditionally

# order
# =====
config.order.removeRbAngularMocks() # conditionally

# globs
# =====
config.glob.removeRbAngularMocks() # conditionally

# return
# ======
promiseHelp.get()

0 comments on commit 4688281

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