-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.coffee
206 lines (174 loc) · 6.33 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
autoprefixer = require 'gulp-autoprefixer'
bower = require 'gulp-bower'
bump = require 'gulp-bump'
cleanCSS = require 'gulp-clean-css'
concat = require 'gulp-concat'
connect = require 'connect'
coffeeify = require 'gulp-coffeeify'
foreach = require 'gulp-foreach'
fs = require 'fs'
git = require 'gulp-git'
gulp = require 'gulp'
gutil = require 'gulp-util'
htmlMin = require 'gulp-htmlmin'
http = require 'http'
jade = require 'gulp-jade'
livereload = require 'gulp-livereload'
open = require "gulp-open"
plumber = require 'gulp-plumber'
rev = require 'gulp-rev' # Not currently using..
rimraf = require 'rimraf'
sass = require 'gulp-sass'
serveStatic = require 'serve-static'
serveIndex = require 'serve-index'
shadowIcons = require 'gulp-shadow-icons'
uglify = require 'gulp-uglify'
usemin = require 'gulp-usemin'
watch = require 'gulp-watch'
wrap = require 'gulp-wrap'
jadeTargetDir = './server/js/jade'
# Paths to source files
jadeStagePath = 'stage/index.jade'
jadePath = 'app/jade/**/*.jade'
cssPath = 'app/scss/**/*.scss'
cssStagePath = 'stage/stage.scss'
appJsPath = ['app/coffee/**/*.coffee', "#{jadeTargetDir}/**/*.js"]
stageJsPath = 'stage/**/*.coffee'
assetPath = 'app/assets/*.!(svg)'
svgPath = 'app/assets/compiled/*.svg'
mainJsFile = './app/coffee/main.coffee'
mainStageJsFile = './stage/stage.coffee'
# ------------------------------------ Source compiling
htmlStage = (cb)->
gulp.src jadeStagePath
.pipe jade()
.pipe plumber()
.pipe gulp.dest('./server/')
.on('end', cb)
html = (cb)->
gulp.src( jadePath )
.pipe jade(client: true)
.pipe plumber()
.pipe wrap( "module.exports = <%= file.contents %>" )
.pipe gulp.dest(jadeTargetDir)
.on('end', cb)
css = (cb)->
gulp.src( cssPath )
.pipe sass().on('error', sass.logError)
.pipe autoprefixer( browsers: ['last 1 version'],cascade: false )
.pipe gulp.dest('./server/css')
.on('end', cb)
cssStage = (cb)->
gulp.src( cssStagePath )
.pipe sass().on('error', sass.logError)
.pipe autoprefixer( browsers: ['last 1 version'],cascade: false )
.pipe gulp.dest('./server/stage/css')
.on('end', cb)
js = (cb)->
# App
gulp.src( mainJsFile )
.pipe plumber()
.pipe coffeeify({options: { debug: true, paths: ["#{__dirname}/node_modules", "#{__dirname}/app/coffee/", "#{__dirname}/server/js/" ] } })
.pipe gulp.dest('server/js/')
.on('end', cb)
jsStage = (cb)->
gulp.src mainStageJsFile
.pipe plumber()
.pipe coffeeify({options: { debug: true, paths: ["#{__dirname}/node_modules", "#{__dirname}/app/coffee/"] } })
.pipe gulp.dest('server/stage/js')
.on('end', cb)
parseSVG = (cb)->
gulp.src svgPath
.pipe shadowIcons {
cssDest:'./css/'
jsDest:'./js/'
cssNamespace:''
cssRegex:[]
}
.pipe gulp.dest('./server/')
.on('end', cb)
# Compile
compileFiles = (doWatch=false, cb) ->
count = 0
onComplete = ()=> if ++count == ar.length then cb()
ar = [
{meth:js, glob:appJsPath}
{meth:css, glob:cssPath}
{meth:html, glob:jadePath}
{meth:parseSVG, glob:svgPath}
{meth:jsStage, glob:stageJsPath}
{meth:cssStage, glob:cssStagePath}
{meth:htmlStage, glob:jadeStagePath}
{meth:copyAssets, glob:assetPath, params:['server/assets', onComplete]}
]
createWatcher = (item, params)->
watch item.glob, =>
item.meth.apply null, params
.pipe( livereload() )
item.meth.apply null, params
# Watch / compile all the files found in the array
for item in ar
params = if item.params? then item.params else [onComplete]
if doWatch
createWatcher(item, params)
else
item.meth.apply null, params
# ------------------------------------ Random helpers
copyAssets = (destination, cb) ->
gulp.src assetPath
.pipe gulp.dest(destination)
.on('end', cb)
copyBowerLibs = ()->
bower().pipe gulp.dest('./server/bower-libs/')
copyFilesToBuild = ->
gulp.src( './server/js/*' ).pipe gulp.dest('./rel/')
gulp.src( './server/css/main.css' ).pipe gulp.dest('./rel/')
pushViaGit = ->
# Start out by reading the version number for commit msg, then git push, etc..
fs.readFile './bower.json', 'utf8', (err, data) =>
regex = /version"\s*:\s*"(.+)"/
version = data.match(regex)[1]
gulp.src('./')
.pipe git.add()
.pipe git.commit("BUILD - #{version}")
.pipe git.push 'origin', 'master', (err)=> console.log( err)
bumpBowerVersion = ->
gulp.src('./bower.json')
.pipe bump( {type:'patch'} )
.pipe(gulp.dest('./'));
minifyAndJoin = () ->
gulp.src('./server/*.html').pipe foreach((stream, file) ->
stream.pipe(
usemin
css : [ cleanCSS( {compatibility: 'ie8'} ) , 'concat']
html: [ htmlMin( {collapseWhitespace: true})]
js : [ uglify() ]
).pipe gulp.dest('rel/')
)
# ------------------------------------ Server
server = ->
port = 6688
hostname = null # allow to connect from anywhere
base = 'server'
directory = 'server'
app = connect()
.use( serveStatic(base) )
.use( serveIndex(directory) )
http.createServer(app).listen port, hostname
livereload.listen()
console.log "SERVER LISTENING -> localhost:#{port}"
# Open in the browser
launch = -> gulp.src("").pipe open( uri: "http://localhost:6688/index.html" )
# ----------- MAIN ----------- #
gulp.task 'clean', (cb) -> rimraf './server', cb
gulp.task 'bowerLibs', ['clean'], () -> copyBowerLibs()
gulp.task 'compile', ['bowerLibs'], (cb) -> compileFiles(true, cb)
gulp.task 'server', ['compile'], (cb) -> server(); launch();
gulp.task 'default', ['server']
# ----------- BUILD (rel) ----------- #
gulp.task 'rel:clean', (cb) -> rimraf './rel', cb
gulp.task 'bumpVersion', () -> bumpBowerVersion()
gulp.task 'copyStatics', ['bowerLibs'], () -> copyAssets('rel/assets', ->)
gulp.task 'releaseCompile', ['copyStatics'], (cb) -> compileFiles(false, cb)
gulp.task 'minify',['releaseCompile'], () -> minifyAndJoin();
gulp.task 'rel', ['rel:clean', 'bumpVersion', 'minify'], -> rimraf './rel/index.html', ->