-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.coffee
276 lines (215 loc) · 6.29 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
require 'coffee-script/register'
autoprefixer = require 'gulp-autoprefixer'
argv = require('yargs').argv
browserify = require 'browserify'
bower = require 'bower'
childProcess = require 'child_process'
del = require 'del'
concat = require 'gulp-concat'
fs = require 'fs'
glob = require 'glob'
gulp = require 'gulp'
gutil = require 'gulp-util'
imagemin = require 'gulp-imagemin'
livereload = require 'gulp-livereload'
karma = require('karma').server
karmaConf = require './config/karma.conf'
minifyCss = require 'gulp-minify-css'
ngAnnotate = require 'gulp-ng-annotate'
preprocess = require 'gulp-preprocess'
rename = require 'gulp-rename'
runSequence = require 'run-sequence'
sass = require 'gulp-sass'
sh = require 'shelljs'
streamify = require 'gulp-streamify'
source = require 'vinyl-source-stream'
uglify = require 'gulp-uglify'
watchify = require 'watchify'
protractor = require('gulp-protractor').protractor
prompt = require 'prompt'
buildDir = './www'
appDir = './app'
dataDir = './data'
testDir = './tests'
vendorDir = './app/vendor'
resourcesScriptsDir = './resources/scripts'
scripts = (watch) ->
bundler = browserify
cache: {}
packageCache: {}
entries: ["#{appDir}/bootstrap.coffee"]
extensions: ['.coffee']
if watch
bundler = watchify bundler
# Check enviroment
env = argv.e or 'staging'
bundle = ->
bundleStream = bundler.bundle()
# use vinyl-source-stream to make the stream gulp compatible
# specifiy the desired output filename here
.pipe source('bundle.js')
# wrap plugins to support streams
# i.e. .pipe streamify(plugin())
.pipe streamify(preprocess({context: {BUILD_ENV: env}}))
.pipe gulp.dest("#{buildDir}/app")
bundleStream
if watch
bundler.on 'update', bundle
bundle()
gulp.task 'scripts', ->
scripts false
gulp.task 'styles', ->
gulp.src "#{appDir}/main.scss"
.pipe sass(errLogToConsole: true)
.pipe autoprefixer()
.pipe rename(extname: '.css')
.pipe gulp.dest("#{buildDir}/app")
#.pipe minifyCss(keepSpecialComments: 0)
#.pipe rename(extname: '.min.css')
#.pipe gulp.dest("#{buildDir}/app")
gulp.task 'data', ->
gulp.src "#{dataDir}/**/*", {base: "#{dataDir}"}
.pipe gulp.dest(buildDir)
gulp.task 'templates', ->
# Get the enviroment.
env = argv.e or 'staging'
# NOTE: When we build the webview, we can give the ionic templates/partials an
# .app.html extension, and the web partials a .web.html extension, then rename
# them to .html. If we decide to use gulp-template-cache, we can us the
# transformUrl option.
gulp.src [
"#{appDir}/**/*.html"
"!#{appDir}/index.html"
], {base: "#{appDir}"}
.pipe gulp.dest("#{buildDir}/app")
gulp.src "#{appDir}/index.html"
.pipe preprocess({context: {BUILD_ENV: env}})
.pipe gulp.dest(buildDir)
gulp.task 'vendor', ->
gulp.src "#{vendorDir}/**/*", {base: "#{appDir}"}
.pipe gulp.dest("#{buildDir}/app")
###
# Minify the scripts to be included in the app bundle.
####
gulp.task 'resources-scripts', ->
files = [
'meteor.js'
'ionic.js'
'ionic-angular.js'
'angular-chart.js'
'intlTelInput.js'
'jQuery-1.11.3.js'
]
for fileName in files
gulp.src "#{resourcesScriptsDir}/#{fileName}"
.pipe uglify()
.pipe rename(extname: '.min.js')
.pipe gulp.dest(resourcesScriptsDir)
gulp.task 'minify-js', ->
gulp.src "#{buildDir}/app/bundle.js"
.pipe ngAnnotate()
.pipe uglify()
.pipe gulp.dest("#{buildDir}/app")
gulp.task 'minify-css', ->
gulp.src "#{buildDir}/app/main.css"
.pipe minifyCss()
.pipe gulp.dest("#{buildDir}/app")
gulp.task 'minify-images', ->
gulp.src "#{buildDir}/images/**/*"
.pipe imagemin()
.pipe gulp.dest("#{buildDir}/images")
gulp.task 'minify', [
'minify-js'
'minify-css'
'minify-images'
]
gulp.task 'unit', ->
# Delete old test file
if fs.existsSync "#{testDir}/test-bundle.js"
fs.unlinkSync "#{testDir}/test-bundle.js"
# Watch all test files for changes, and re-browserify.
glob "#{appDir}/**/*.spec.coffee", null, (err, files) ->
bundler = browserify
cache: {}
packageCache: {}
entries: files
extensions: ['.coffee']
bundler = watchify bundler
bundle = ->
bundler.bundle()
.pipe source('test-bundle.js')
.pipe gulp.dest(testDir)
bundler.on 'update', bundle
bundle()
# run the unit tests using karma
karma.start karmaConf
gulp.task 'webdriver-update', (done) ->
childProcess.spawn 'webdriver-manager', ['update'], stdio: 'inherit'
.once 'close', done
gulp.task 'e2e', ['webdriver-update'], ->
gulp.src "#{appDir}/**/*.scenario.coffee"
.pipe protractor(configFile: './config/protractor.conf.coffee')
.on 'error', (error) ->
throw error
gulp.task 'clean', ->
del 'www'
gulp.task 'build', [
# 'clean'
'scripts'
'styles'
'templates'
'data'
'vendor'
]
gulp.task 'watch', [
'styles'
'templates'
], ->
port = argv.p or '3000'
scripts true
gulp.watch "#{appDir}/**/*.scss", ['styles']
gulp.watch "#{dataDir}/**/*", ['data']
gulp.watch "#{vendorDir}/**/*", ['vendor']
gulp.watch "#{appDir}/**/*.html", ['templates']
childProcess.spawn 'serve', ['www', '--no-logs', '--port', port], stdio: 'inherit'
livereload.listen()
gulp.watch "#{buildDir}/**/*"
.on 'change', livereload.changed
gulp.task 'ionic-upload', (done) ->
# Prompt for upload note
prompt.message = 'Enter an upload note!'.green
prompt.start()
env = argv.e or 'unknown env'
prompt.get [{name: env, required: true}], (err, result) ->
if err then return console.log 'Error with prompt'
# Set Ionic deploy tag
if argv.e is 'prod'
deployTag = 'production'
else if argv.e is 'staging'
deployTag = 'dev'
else
deployTag = 'dev'
cmdArgs = ['upload', '--note', "#{env} - #{result[env]}", "--deploy=#{deployTag}"]
childProcess.spawn 'ionic', cmdArgs, stdio: 'inherit'
.once 'close', done
gulp.task 'deploy-staging', (done) ->
argv.e = 'staging' # set env to staging
runSequence(
'build',
'minify',
'ionic-upload',
done
)
gulp.task 'deploy-prod', (done) ->
argv.e = 'prod' # set env to prod
runSequence(
'build',
'minify',
'ionic-upload',
done
)
gulp.task 'default', ['watch']
# TODO:
# gulp run android
# gulp build android
# gulp build ios