forked from nanobox-io/nanobox-guides
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.coffee
327 lines (281 loc) · 9.99 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
autoprefixer = require 'gulp-autoprefixer'
bower = require 'gulp-bower'
bump = require 'gulp-bump'
coffee = require 'gulp-coffee'
concat = require 'gulp-concat'
connect = require 'connect'
fs = require 'fs'
git = require 'gulp-git'
gulp = require 'gulp'
gulpignore = require 'gulp-ignore'
gutil = require 'gulp-util'
http = require 'http'
pug = require 'jade'
jade = require 'gulp-jade'
livereload = require 'gulp-livereload'
marked = require 'marked'
minifyCss = require 'gulp-minify-css'
minifyHtml = require 'gulp-minify-html'
mustache = require 'gulp-mustache'
open = require "gulp-open"
plumber = require 'gulp-plumber'
rename = require 'gulp-rename'
rimraf = require 'rimraf'
rimrafgulp = require 'gulp-rimraf'
sass = require 'gulp-sass'
uglify = require 'gulp-uglify'
usemin = require 'gulp-usemin'
watch = require 'gulp-watch'
wrap = require 'gulp-wrap'
shadow = require 'gulp-shadow-icons'
wait = require 'gulp-wait'
marked.setOptions
smartypants: true
pug.filters.md = marked
livereload.listen()
# new
inject = require 'gulp-inject'
foreach = require 'gulp-foreach'
rev = require 'gulp-rev'
del = require 'del'
# Paths to source files
articlePath = 'articles/**/*.md'
jadePath = 'app/pages'
templatePath = 'app/jade/**/*.jade'
cssPath = 'app/scss/**/*.scss'
cssStagePath = 'stage/stage.scss'
coffeePath = 'app/coffee/**/*.coffee'
assetPath = ['app/images/**/*', 'app/fonts/*']
miscJsPath = 'app/js/*'
yamlPath = 'articles/**/*.yml'
svgPath = ['lib/assets/core-styles/svg/compiled/*.svg','app/assets/compiled/*.svg']
htaccessPath = 'app/misc/*'
parseSVG = (cb)->
gulp.src svgPath
.pipe shadow {
cssDest:'./css/'
jsDest:'./js/'
cssRegex:[
{ pattern:/Lato-Regular/g, replace:"Lato" }
{ pattern:/font-family:'Lato-Italic';/g, replace:"font-family:'Lato'; font-style:italic;" }
]
}
.pipe gulp.dest('./server/')
.on('end', cb)
onError = (err) ->
console.log err
@emit 'end'
html = (cb, src)->
src = if !src? then "" else "#{src}/"
source = "#{jadePath}/#{src}**/*.jade"
gulp.src source
.pipe jade({jade:pug, basedir:'./' }).on('error', (err)-> console.log(err); this.emit('end') )
.on 'error', onError
.pipe gulp.dest("./server/#{src}")
.on 'end', ()->
livereload.reload()
if !src?
console.log " (Reloaded all html files)"
else
console.log " (Only reloaded files matched by : #{source})"
if cb?
cb()
jadeTemplates = (cb)->
gulp.src( templatePath )
.pipe jade(client: true)
.on 'error', onError
.pipe wrap("jadeTemplate['<%= file.relative.split('.')[0] %>'] = <%= file.contents %>;\n")
.pipe concat('jade-templates.js')
.pipe wrap("jadeTemplate = {};\n<%= file.contents %>")
.pipe gulp.dest('./server/js')
.on('end', cb)
css = (cb)->
gulp.src( cssPath )
.pipe sass({errLogToConsole: true})
.on 'error', onError
.pipe autoprefixer( browsers: ['last 1 version'],cascade: false )
.pipe gulp.dest('./server/css')
.on('end', cb)
js = (cb)->
# App
gulp.src( coffeePath )
.pipe plumber()
.pipe coffee( bare: true ).on( 'error', gutil.log ) .on( 'error', gutil.beep )
.on 'error', onError
.pipe gulp.dest('server/js')
.on 'end', ()->
livereload.reload()
cb()
miscJs = (cb)->
gulp.src miscJsPath
.pipe gulp.dest('server/js')
.on 'end', cb
copyAssets = (destination, cb) ->
gulp.src assetPath
.pipe gulp.dest(destination)
.on('end', cb)
copyImages = (cb)->
copyAssets 'server/assets', cb
copyGithubImages = ()->
gulp.src './quickstart-icons/*'
.pipe gulp.dest('rel/assets/quickstart-icons/')
copyMiscRootFiles = ()->
gulp.src htaccessPath
.pipe gulp.dest('./rel')
# gulp.task 'copy-yaml', ['minify']
# .on('end', cb)
copyFiles = (path, destination, cb) ->
gulp.src path
.pipe gulp.dest(destination)
.on('end', cb)
copyBowerLibs = (cb)->
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/')
copyYaml = (cb)->
gulp.src yamlPath
.pipe gulp.dest('server/yaml')
.on 'end', ()->
livereload.reload()
cb()
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)=> if err? then console.log(err)
bumpBowerVersion = ->
gulp.src('./bower.json')
.pipe bump( {type:'patch'} )
.pipe(gulp.dest('./'));
# minifyAndJoin = () ->
# gulp.src './server/index.html'
# .pipe usemin
# css : [ minifyCss(), 'concat'],
# html: [ minifyHtml({empty: true})],
# js : [ uglify(), rev()],
# js2 : [ uglify(), rev()]
# .pipe(gulp.dest('rel/'));
minifyAndJoin = () ->
gulp.src('./server/**/*.html').pipe foreach((stream, file) ->
stream.pipe(
usemin
css : [ minifyCss(), 'concat', rev()]
html : [ minifyHtml({empty: true})]
# js : [ uglify, rev]
js : [ rev]
path : './server'
skipMissingResources : true
# assetsDir : 'rel/'
).pipe gulp.dest('rel/')
)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
gulp.task 'minii', () -> minifyAndJoin()
# Livereload Server
server = ->
port = 8080
hostname = null # allow to connect from anywhere
base = 'server'
directory = 'server'
app = connect()
.use( connect.static(base) )
.use( connect.directory(directory) )
http.createServer(app).listen port, hostname
# Open in the browser
launch = ->
console.log '-------------------------------'
console.log 'Build Complete, visit : '
console.log "http://guides.nanobox.dev"
# gulp.src("./server/index.html") # An actual file must be specified or gulp will overlook the task.
# .pipe(open("",
# url: "http://localhost:3814/index.html",
# app: "google chrome"
# ))
prettyURLS = () ->
gulp.src(["./rel/**/*.html","!./rel/index.html"])
.pipe( rename (path)->
path.dirname = "#{path.dirname}/#{path.basename}"
path.basename = "index"
)
.pipe gulp.dest('./rel')
deleteUneededFiles = ()->
gulp.src([
"./rel/**/*.html", "!./rel/**/index.html",
"./rel/**/style.css", "!./rel/style.css",
"./rel/**/js", "!./rel/js"])
.pipe rimrafgulp()
markdownChange = (file)->
path = file.path.split("/")
path.pop()
path.shift()
path.shift()
path.shift()
# Find the deepest matching folder with jaded files to recompile
for i in [path.length..0]
testPath = path.slice(0, i).join("/")
try
# If this directory exists in ./app/pages, trigger a rebuild on all the jade files in that dir
if fs.lstatSync("app/pages/#{testPath}").isDirectory()
html null, testPath
return
catch e
compileFiles = (doWatch=false, cb) ->
count = 0
onComplete = ()=>
if ++count == ar.length then cb();
ar = [
{meth:js, glob:coffeePath, dontLiveReload:true}
{meth:css, glob:cssPath}
{meth:miscJs, glob:miscJsPath}
{meth:jadeTemplates, glob:templatePath}
{meth:html, glob:jadePath, dontLiveReload:true}
{meth:copyYaml, glob:yamlPath, dontLiveReload:true}
{meth:parseSVG, glob:svgPath}
{meth:copyImages, glob:assetPath}
]
# Create a custom watcher for markdown files
gulp.watch(articlePath).on 'change', markdownChange
createWatcher = (item, params)->
item.meth.apply(null, params)
watch item.glob, =>
stream = item.meth.apply( null, [=>])
if !item.dontLiveReload
stream.pipe( livereload() )
for item in ar
params = if item.params? then item.params else [onComplete]
if doWatch
createWatcher(item, params)
else
item.meth.apply null, params
gulp.task 'tester', ()->
gulp.watch(articlePath).on 'change', markdownChange
# ----------- GENERATOR ----------- #
gulp.task 'new-framework', ()->
[language, framework] = gutil.env.path.split "/"
gulp.src 'templates/framework/**/*.jade'
.pipe mustache({language:language, framework:framework})
.pipe gulp.dest("app/pages/#{language}/#{framework}")
.on 'end', process.exit
# ----------- MAIN ----------- #
gulp.task 'clean', (cb) -> del ['./server/**/*']
gulp.task 'bowerLibs', ['clean'], (cb) -> copyBowerLibs()
gulp.task 'compile', ['bowerLibs'], (cb) -> compileFiles(true, cb)
gulp.task 'server', ['compile'], (cb) -> server(); launch(); #process.exit()
gulp.task 'default', ['server']
# ----------- BUILD (rel) ----------- #
gulp.task 'rel:clean', () -> del ['./rel/**/*']
gulp.task 'bumpVersion', ['rel:clean'], () -> bumpBowerVersion()
gulp.task 'copyStatics', ['bowerLibs'], () -> copyAssets('rel/assets', ->)
gulp.task 'copyGithubImages', ['copyStatics'], () -> copyGithubImages()
gulp.task 'releaseCompile', ['copyGithubImages'], (cb) -> compileFiles(false, cb)
gulp.task 'minify',['releaseCompile'], () -> minifyAndJoin();
gulp.task 'copy-yaml', ['minify'], () -> copyFiles('./server/yaml/**/*', './rel/yaml/', ->)
gulp.task 'pretty',['copy-yaml'], () -> prettyURLS()
gulp.task 'cleanhtml', ['pretty'], () -> deleteUneededFiles()
gulp.task 'copy-misc-root-files',['cleanhtml'], () -> copyMiscRootFiles()
gulp.task 'rel', ['rel:clean', 'bumpVersion', 'copy-misc-root-files'], -> process.exit()