-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.coffee
348 lines (271 loc) · 6.58 KB
/
Gulpfile.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
gulp = require 'gulp'
gutil = require 'gulp-util'
run = require 'run-sequence'
semver = require 'semver-utils'
###
DEFAULT TASK
###
gulp.task 'default', (done) ->
run(
'clean'
'build'
'copy:bin'
'server'
[
'server:open'
'test:unit:continuous'
'watch'
]
done
)
gulp.task 'watch', (done) ->
server = livereload()
gulp.watch urls.coffee, ['build:src']
.on 'change', (file) -> server.changed file.path
gulp.watch [
'src/**/*.scss'
]
.on 'change', (file) ->
run(
[
'build:sass'
]
'css:testing'
-> server.changed file.path
)
gulp.watch [
'src/**/*.coffee'
'!src/**/*.jquery.coffee'
'!src/**/*.spec.coffee'
'!src/**/*.ui.coffee'
'src/**/*.html'
'!src/**/examples/*.html'
]
.on 'change', (file) ->
run(
'build:src'
-> server.changed file.path
)
gulp.watch [
'src/**/*.spec.coffee'
], ['test:unit:compile']
.on 'change', (file) -> server.changed file.path
###
# Server & Livereload
###
connect = require 'gulp-connect'
open = require 'open'
path = require 'path'
livereload = require 'gulp-livereload'
SERVER_PORT = process.env.PORT or 8000
LIVERELOAD_PORT = 35729
gulp.task 'server', ->
express = require 'express'
app = express()
app.use '/', express.static path.join __dirname, 'dist/site/app'
.use '/cui', express.static path.join __dirname, 'bin'
.use '/vendor', express.static path.join __dirname, 'vendor'
.listen SERVER_PORT
gulp.task 'server:open', ->
open "http://localhost:#{SERVER_PORT}"
###
# Build Process
###
# Utilities/commonly used
concat = require 'gulp-concat'
clean = require 'gulp-clean'
# HTML templates
htmlmin = require 'gulp-htmlmin'
templateCache = require 'gulp-angular-templatecache'
# Coffeescript/Javascript
coffee = require 'gulp-coffee'
uglify = require 'gulp-uglify'
annotate = require 'gulp-ng-annotate'
rename = require 'gulp-rename'
header = require 'gulp-header'
# SASS/CSS
sass = require 'gulp-sass'
csso = require 'gulp-csso'
###
URLS TO LOCATIONS
###
vendor = [
'vendor/angular/angular.js'
'vendor/angular-route/angular-route.js'
'vendor/angular-animate/angular-animate.js'
'vendor/angular-touch/angular-touch.js'
]
vendor_min = [
'vendor/angular/angular.min.js'
'vendor/angular-route/angular-route.min.js'
'vendor/angular-animate/angular-animate.min.js'
'vendor/angular-touch/angular-touch.min.js'
]
urls =
src:
sass: [
'src/base.scss'
'src/*/*/*.scss'
]
###
BUILD UTILITIES
###
gulp.task 'build', (done) ->
run(
'build:src:index'
'build:src'
'build:sass'
'build:vendor'
done
)
# Standalone: Task must be explicitly called.
gulp.task 'clean', ->
gulp.src [
'coverage/'
'.tmp'
'server/examples'
'bin'
'dist'
], read: no
.pipe clean()
###
HTML --> $templateCache
###
appTemplates = ->
gulp.src [
'src/**/*.html'
]
.pipe rename dirname: ''
.pipe htmlmin
collapseWhitespace: yes
removeComments: yes
collapseBooleanAttributes: yes
removeAttributeQuotes: yes
caseSensitive: yes
.pipe templateCache 'app.tmpl.js',
root: '__cui'
standalone: yes
module: 'app.templates'
###
COFFEESCRIPT (SRC)
###
appSource = ->
gulp.src [
'src/index.coffee'
'src/**/*.coffee'
'!src/**/*.jquery.coffee'
'!src/**/*.spec.coffee'
'!src/**/*.ui.coffee'
]
.pipe concat 'appsrc.js'
.pipe coffee().on 'error', gutil.log
###
appTemplates AND appSource
SRC Compilation
###
VENDOR_HEADER = ""
gulp.task 'build:src', ->
require('event-stream')
.merge appTemplates(), appSource()
.pipe concat 'app.js'
.pipe header HEADER
.pipe gulp.dest 'bin/js'
.pipe gulp.dest '.tmp/bin/js'
.pipe annotate
single_quotes: yes
remove: yes
add: yes
.pipe uglify mangle: off
.pipe header HEADER
.pipe rename suffix: '.min'
.pipe gulp.dest 'bin/js'
###
Build the index files, which register the modules for Angular.
###
fs = require 'fs'
wrench = require 'wrench'
gulp.task 'build:src:index', (done) ->
regex = /[\/\\](\w+)[\/\\]\w+\.coffee$/
arr = []
files = wrench.readdirSyncRecursive 'src'
files = files.filter (fileName) ->
regex.test fileName
files.forEach (fullPath) ->
match = regex.exec fullPath
arr.push "app.#{match[1]}.#{match[2]}" if match
out = """
# This file is generated by `gulp build:src:index`, do not edit yourself.
window.__app = #{__app}
app = angular.module 'app', #{JSON.stringify(arr, null, 2)}
angular.module('app.base', ['app.templates'])
.value('baseTemplatePath', '__app/')
"""
fs.writeFile 'src/index.coffee', out,
(err) ->
if err then throw new Error 'Unable to write index.coffee. Check your write permissions to `src/`'
done()
gulp.task 'copy:bin', ->
checkForSite ->
gulp.src 'bin/**/*'
.pipe gulp.dest 'dist/site/app'
###
SASS
###
base64 = require 'gulp-base64'
gulp.task 'build:sass', ->
gulp.src urls.src.sass
.pipe sass
includePaths: [
require('node-bourbon').includePaths
'vendor/modularized-normalize-scss/'
'src/'
]
onError: (err) ->
gutil.log gutil.colors.red err
gutil.beep()
.pipe concat 'app.css'
.pipe base64
baseDir: 'src'
extensions: ['png', 'jpg']
.pipe header HEADER
# app.css
.pipe gulp.dest 'bin/css'
# app.min.css
.pipe csso()
.pipe rename suffix: '.min'
.pipe gulp.dest 'bin/css'
###
VENDOR
###
gulp.task 'build:vendor', [
'build:vendor:full'
'build:vendor:min'
]
gulp.task 'build:vendor:full', ->
gulp.src cui_vendor
.pipe concat 'vendor.js'
.pipe header VENDOR_HEADER
.pipe gulp.dest 'bin/js'
.pipe gulp.dest '.tmp/bin/js'
gulp.task 'build:vendor:min', ->
gulp.src cui_vendor_min
.pipe uglify mangle: off
.pipe concat 'vendor.min.js'
.pipe header VENDOR_HEADER
.pipe gulp.dest 'bin/js'
###
UNIT TESTS
###
exec = require('child_process').exec
gulp.task 'test:unit', ['test:unit:compile', 'build:src'], ->
karma = require('karma').server
karma.start
configFile: path.resolve __dirname, 'karma.conf.coffee'
singleRun: yes
autoWatch: no
gulp.task 'test:unit:continuous', ['test:unit:compile'], ->
karma = require('karma').server
karma.start
configFile: path.resolve __dirname, 'karma.conf.coffee'
singleRun: no
autoWatch: yes