This repository has been archived by the owner on Sep 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
252 lines (214 loc) · 7.23 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
###
Modules
###
_ = require('lodash')
chalk = require('chalk')
gulp = require('gulp')
clean = require('gulp-rimraf')
cjsx = require('gulp-cjsx')
filter = require('gulp-filter')
iif = require('gulp-if')
imagemin = require('gulp-imagemin')
jade = require('gulp-jade')
plumber = require('gulp-plumber')
rename = require('gulp-rename')
sass = require('gulp-ruby-sass')
nodemon = require('gulp-nodemon')
uglify = require('gulp-uglify')
sync = require('browser-sync')
{ spawn } = require('child_process')
###
Variables
###
env = require('./env.json')
data = _.extend({}, env)
###
Directory Paths
###
_src = './src'
_build = './_build'
DIR =
modules: './node_modules'
src:
root: _src
html: "#{_src}/frontend/html"
js: "#{_src}/frontend/js"
css: "#{_src}/frontend/css"
img: "#{_src}/frontend/img"
misc: "#{_src}/frontend/misc"
vendor: "#{_src}/frontend/vendor"
backend: "#{_src}/backend"
shared: "#{_src}/shared"
build:
root: _build
public: "#{_build}/public"
js: "#{_build}/public/js"
css: "#{_build}/public/css"
img: "#{_build}/public/img"
vendor:
js: "#{_build}/public/js/vendor"
css: "#{_build}/public/css/vendor"
###
Task Manager
###
_do = (src='', dest='', task='', filename='') ->
_copy = task is ''
_sass = /sass/.test task
_jade = /jade/.test task
_coffee = /coffee|cjsx/.test task
_uglify = /uglify/.test task
_imagemin = /imagemin/.test task
_rename = /rename/.test task
_dev = /dev/.test task
if _sass
sass(src, {compass: true, style: 'compressed', sourcemap: false}) # Sass
.pipe(do plumber) # Plumber
.pipe(gulp.dest(dest)) # Destination
.pipe(filter('**/*.css')) # Filter CSS
.pipe(sync.reload(stream: true)) # BrowserSync
else
gulp.src(src)
.pipe(do plumber) # Plumber
.pipe(iif(_jade, jade({data, pretty: true}))) # Jade
.pipe(iif(_coffee, cjsx(bare: true))) # Coffee/React
.pipe(iif(_uglify and not _dev, uglify(compress: drop_debugger: false))) # Uglify
.pipe(iif(_imagemin and not _dev, imagemin(progressive: true))) # Imagemin
.pipe(iif(_rename and !!filename, rename(basename: filename))) # Rename
.pipe(gulp.dest(dest)) # Destination
###
Clean
###
gulp.task 'clean', ->
gulp.src(DIR.build.root, {read: false}).pipe(clean())
gulp.task 'clean:modules', ->
gulp.src([DIR.modules, DIR.src.vendor], {read: false}).pipe(clean())
gulp.task 'clean:all', ['clean', 'clean:modules']
###
Vendor Files
###
gulp.task 'vendor', ->
_do("#{DIR.src.vendor}/requirejs/require.js", DIR.build.vendor.js, 'uglify')
_do("#{DIR.src.vendor}/zepto/zepto.js", DIR.build.vendor.js, 'uglify')
_do("#{DIR.src.vendor}/lodash/lodash.js", DIR.build.vendor.js, 'uglify')
_do("#{DIR.src.vendor}/react/react-with-addons.js", DIR.build.vendor.js, 'uglify rename', 'react')
_do("#{DIR.src.vendor}/flux/dist/Flux.js", DIR.build.vendor.js, 'uglify rename', 'flux')
_do("#{DIR.src.vendor}/eventEmitter/eventEmitter.js", DIR.build.vendor.js, 'uglify rename', 'event')
_do("#{DIR.src.vendor}/backbone/backbone.js", DIR.build.vendor.js, 'uglify')
_do("#{DIR.src.vendor}/bluebird/js/browser/bluebird.js", DIR.build.vendor.js, 'uglify rename', 'bluebird')
_do("#{DIR.src.vendor}/prism/prism.js", DIR.build.vendor.js, 'uglify')
_do("#{DIR.src.vendor}/prism/themes/prism.css", DIR.build.vendor.css)
###
Tasks
###
gulp.task 'misc', ->
_do("#{DIR.src.misc}/**/*", DIR.build.public)
gulp.task 'html', ->
_do(["#{DIR.src.html}/**/*.jade", "!#{DIR.src.html}/**/_*.jade"], DIR.build.public, 'jade')
gulp.task 'css', ->
_do(DIR.src.css, DIR.build.css, 'sass')
gulp.task 'js', ->
_do("#{DIR.src.js}/**/*.{coffee,cjsx}", DIR.build.js, 'cjsx uglify')
gulp.task 'img', ->
_do("#{DIR.src.img}/**/*", DIR.build.img, 'imagemin')
gulp.task 'js:dev', ->
_do("#{DIR.src.js}/**/*.{coffee,cjsx}", DIR.build.js, 'cjsx dev')
gulp.task 'img:dev', ->
_do("#{DIR.src.img}/**/*", DIR.build.img, 'imagemin dev')
gulp.task 'backend', ->
_do(["!#{DIR.src.backend}/**/*.coffee", "#{DIR.src.backend}/**/*"], DIR.build.root)
_do("#{DIR.src.backend}/**/*.coffee", DIR.build.root, 'cjsx uglify')
gulp.task 'shared', ->
_do("#{DIR.src.shared}/**/*.coffee", DIR.build.js, 'coffee')
_do("#{DIR.src.shared}/**/*.coffee", DIR.build.root, 'coffee')
###
Build
###
gulp.task 'build:backend', ['shared', 'backend']
gulp.task 'build:static:dev', ['shared', 'misc', 'html', 'css', 'js:dev', 'img:dev']
gulp.task 'build:static:prod', ['vendor', 'shared', 'misc', 'html', 'js', 'css', 'img']
gulp.task 'build:dev', ['build:static:dev', 'build:backend']
gulp.task 'build:prod', ['build:static:prod', 'build:backend']
###
Watch/BrowserSync
###
gulp.task 'watch', ['watch:backend', 'watch:static']
gulp.task 'watch:backend', ['nodemon'], ->
gulp.watch ["!#{DIR.src.backend}/public", "#{DIR.src.backend}/**/*"], ['backend', sync.reload]
gulp.task 'nodemon', ['build:backend'], ->
nodemon
script: "#{DIR.build.root}/server.js"
ignore: ["#{DIR.build.public[2..]}/", "#{DIR.modules[2..]}/"]
nodeArgs: ['--debug']
env:
NODE_ENV: 'development'
DEBUG: 'StahkPhotos'
gulp.task 'watch:static', ['browser-sync'], ->
gulp.watch "#{DIR.src.css}/**/*.sass", ['css']
gulp.watch "#{DIR.src.js}/**/*.{coffee,cjsx}", ['js:dev', sync.reload]
gulp.watch "#{DIR.src.html}/**/*.jade", ['html', sync.reload]
gulp.watch "#{DIR.src.shared}/**/*.coffee", ['shared', sync.reload]
gulp.task 'browser-sync', ['build:static:dev'], ->
sync
proxy: "localhost:#{env.PORT}"
port: env.BROWSERSYNC_PORT
open: false
notify: false
###
Deploy Heroku
###
write = (data) ->
process.stdout.write(chalk.white(data.toString()))
run = (cmd, cwd, cb) ->
opts = if cwd then { cwd } else {}
parts = cmd.split(/\s+/g)
p = spawn(parts[0], parts[1..], opts)
p.stdout.on('data', write)
p.stderr.on('data', write)
p.on 'exit', (code) ->
if code
err = new Error("command #{cmd} exited [code: #{code}]")
err = _.extend {}, err, { code, cmd }
cb?(err or null)
series = (cmds, cwd, cb) ->
do execNext = ->
run cmds.shift(), cwd, (err) ->
return cb(err) if err
if cmds.length then execNext() else cb(null)
heroku = (prod) ->
app = "#{env.HEROKU.toLowerCase()}#{unless prod then '-qa' else ''}"
->
CMDS = [
"rm -rf .git"
"git init"
"git add -A"
"git commit -m '.'"
"git remote add heroku [email protected]:#{app}.git"
"git push -fu heroku master"
]
series CMDS, DIR.build.root, (err) ->
if err
console.log err
console.log(chalk.red('[Error] Deploy to Heroku failed!'))
else
console.log(chalk.green('[Success] Deploy to Heroku successful!'))
deploy = (prod) ->
->
CMDS = [
"gulp clean"
"gulp build:prod"
"gulp heroku:#{if prod then 'prod' else 'qa'}"
]
series CMDS, null, (err) ->
if err
console.log(chalk.red('[Error] Deploy failed!'))
else
console.log(chalk.green('[Success] Deploy successful!'))
gulp.task 'heroku:qa', heroku(false)
gulp.task 'heroku:prod', heroku(true)
gulp.task 'deploy:qa', deploy(false)
gulp.task 'deploy:prod', deploy(true)
###
Default Tasks
###
gulp.task 'init', ['vendor', 'watch']
gulp.task 'default', ['watch']