-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gruntfile.coffee
440 lines (386 loc) · 12.4 KB
/
.gruntfile.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
fs = require 'fs'
path = require 'path'
loremIpsum = require 'lorem-ipsum'
dummyImage = require 'dummy-image'
_ = require 'lodash'
extraConfigFile = '.leavesrc'
defaults =
i18n:
localesDir: 'locales'
html:
ext: '.html'
css:
outdir: 'css'
js:
outdir: 'js'
ports:
http: 9000
livereload: 35729
dir:
tmp: 'tmp'
dist: 'dist'
app:
singlePage: false
extraConfig = defaults
if fs.existsSync extraConfigFile
_.merge extraConfig, JSON.parse fs.readFileSync(extraConfigFile, 'utf8')
extraConfig.dev = _.merge {}, _.omit(extraConfig, 'dev', 'dist'), extraConfig.dev
extraConfig.dist = _.merge {}, _.omit(extraConfig, 'dev', 'dist'), extraConfig.dist
capitalize = (s) -> s[0].toUpperCase() + s.substring(1)
generatedImages = []
dumimg = (dist, dir) ->
(options) ->
if _.isNumber(options) || _.isString(options)
[width, height, type, replace] = arguments
height = width unless height
options = {width: width, height: height, type: type, replace: replace}
return options.replace if options.replace? && dist
generated = _.find(generatedImages, options)
return generated.path if generated
generatedOptions = _.clone(options)
generatedImages.push(generatedOptions)
baseDir = path.join(__dirname, dir)
options.outputDir = path.join(baseDir, 'img', 'dummy')
fs.mkdirSync options.outputDir unless fs.existsSync options.outputDir
imgPath = dummyImage(options)
generatedOptions.path = path.relative(baseDir, imgPath)
lorem = (count, options={}) ->
if typeof count == 'number'
options.count = count
else
options = count ? {}
options.units ?= 'words'
loremIpsum options
cssFiles = [
expand: true
cwd: 'assets/css'
src: ['**/*.styl', '!**/_*.styl']
dest: path.join extraConfig.dir.tmp, extraConfig.dev.css.outdir
ext: '.css'
]
cssDevFiles = [_.extend {}, cssFiles[0], {dest: path.join(extraConfig.dir.dist, extraConfig.dev.css.outdir)}]
cssDistFiles = [_.extend {}, cssFiles[0], {dest: path.join(extraConfig.dir.dist, extraConfig.dist.css.outdir)}]
templateFiles = [
expand: true
cwd: 'views'
src: ['**/*.jade', '!**/_*.jade', '!layout.jade']
dest: extraConfig.dir.tmp
ext: extraConfig.dev.html.ext
]
templateDistFiles = [_.extend({}, templateFiles[0], {dest: extraConfig.dir.dist, ext: extraConfig.dist.html.ext})]
templateDevFiles = [_.extend({}, templateFiles[0], {dest: extraConfig.dir.dist})]
coffeeFiles = [
expand: true
cwd: 'assets/js'
src: ['**/*.coffee']
dest: path.join extraConfig.dir.tmp, extraConfig.dev.js.outdir
ext: '.js'
]
coffeeDistFiles = [_.extend({}, coffeeFiles[0], {dest: path.join(extraConfig.dir.dist, extraConfig.dist.js.outdir)})]
coffeeDevFiles = [_.extend({}, coffeeFiles[0], {dest: path.join(extraConfig.dir.dist, extraConfig.dev.js.outdir)})]
htmlFiles = [
expand: true
cwd: extraConfig.dir.tmp
src: ["**/*#{extraConfig.html.ext}", "!**/_*#{extraConfig.html.ext}", "!components/**"]
dest: extraConfig.dir.tmp
ext: extraConfig.dev.html.ext
]
htmlDistFiles = [_.extend({}, htmlFiles[0], {dest: extraConfig.dir.dist, cwd: extraConfig.dir.dist, ext: extraConfig.dist.html.ext})]
htmlDevFiles = [_.extend({}, htmlFiles[0], {dest: extraConfig.dir.dist, cwd: extraConfig.dir.dist})]
i18n = false
i18nOptions =
tmp:
options:
baseDir: extraConfig.dir.tmp
outputDir: extraConfig.dir.tmp
dev: {}
dist: {}
options:
fileFormat: 'yml'
baseDir: extraConfig.dir.dist
outputDir: extraConfig.dir.dist
exclude: ['components/']
locales: []
locale: 'en'
localesPath: extraConfig.i18n.localesDir
if fs.existsSync i18nOptions.options.localesPath
files = fs.readdirSync i18nOptions.options.localesPath
unless _.isEmpty(files)
i18n = true
i18nOptions.options.locales = _.map files, (f) -> f.substring(0, f.lastIndexOf('.'))
if extraConfig?.i18n?
_.merge i18nOptions.options, extraConfig.i18n
Array::push.apply i18nOptions.options.exclude, _.map(i18nOptions.options.locales, (l) -> l + '/')
module.exports = (grunt) ->
require('load-grunt-tasks')(grunt)
grunt.initConfig
pkg: grunt.file.readJSON 'package.json'
watch:
assets:
files: ['assets/**/*', '!assets/css/**/*.styl','!assets/js/**/*.coffee']
tasks: ['newer:copy:tmpAssets']
options:
event: ['changed']
assetsGlob:
files: ['assets/**/*', '!assets/css/**/*.styl', '!assets/js/**/*.coffee']
tasks: ['copy:tmpAssets', 'runViews:tmp:true']
options:
event: ['added', 'deleted']
coffee:
cwd: 'assets/js'
files: 'assets/js/**/*.coffee'
tasks: ['runCoffee:tmp:true:true']
options:
event: ['changed']
coffeeGlob:
cwd: 'assets/js'
files: 'assets/js/**/*.coffee'
tasks: ['runCoffee:tmp:true:true', 'runViews:tmp:true']
options:
event: ['added', 'deleted']
stylesheets:
cwd: 'assets/css'
files: 'assets/css/**/*.styl'
tasks: ['runStylus:tmp:true:true']
options:
event: ['changed']
stylesheetsGlob:
cwd: 'assets/css'
files: 'assets/css/**/*.styl'
tasks: ['runStylus:tmp:true:true', 'runViews:tmp:true']
options:
event: ['added', 'deleted']
views:
cwd: 'views'
files: ['views/**/*.jade', 'views/**/*.{html,md}']
tasks: ['runViews:tmp:true']
locales:
files: "#{i18nOptions.options.localesPath}/**/*.#{i18nOptions.options.fileFormat}"
tasks: ['runViews:tmp:true']
dummy_images:
files: ['tmp/img/dummy/*']
options:
spawn: false
livereload:
port: extraConfig.ports.livereload
coffee:
tmp:
files: coffeeFiles
dev:
files: coffeeDevFiles
dist:
files: coffeeDistFiles
stylus:
tmp:
files: cssFiles
options:
compress: false
dev:
files: cssDevFiles
options:
compress: false
dist:
files: cssDistFiles
jade:
tmp:
files: templateFiles
options:
pretty: true
dev:
files: templateDevFiles
options:
pretty: true
data:
lorem: lorem
dev: true
dumimg: dumimg(false, extraConfig.dir.dist)
dist:
files: templateDistFiles
options:
data:
lorem: lorem
dev: false
dumimg: dumimg(true, extraConfig.dir.dist)
options:
data:
lorem: lorem
dev: true
dumimg: dumimg(false, extraConfig.dir.tmp)
connect:
server:
options:
port: extraConfig.ports.http
keepalive: true
debug: true
base: extraConfig.dir.tmp
useAvailablePort: true
open: true
livereload: extraConfig.ports.livereload
middleware: (connect, options, middlewares) ->
middlewares.push (req, res, next) ->
return next() unless req.headers['accept'].indexOf('text/html') >= 0
return next() unless extraConfig.app.singlePage
file = extraConfig.app.singlePage
file = 'index.html' unless path.extname(file) == '.html'
filepath = path.join(extraConfig.dir.tmp, file)
stat = fs.statSync filepath
res.writeHead 200,
'Content-Type': 'text/html'
'Content-Length': stat.size
res.write fs.readFileSync(filepath)
res.end()
next()
middlewares
copy:
tmpAssets:
expand: true
cwd: 'assets'
src: ['**/*', '!css', '!js', '!css/**/*.styl', '!js/**/*.coffee']
dest: extraConfig.dir.tmp
tmpComponents:
expand: true
cwd: '.components'
src: ['**/*', '!**/src/**']
dest: 'tmp/components'
dist:
files: [
expand: true
cwd: '.components'
src: ['**/*', '!**/src/**']
dest: 'dist/components'
,
expand: true
cwd: 'assets'
src: ['**/*', '!css', '!js', '!css/**/*.styl', '!js/**/*.coffee', '!img/**/*.{png,jpg,gif}']
dest: extraConfig.dir.dist
]
imagemin:
dist:
files: [
expand: true
cwd: 'assets'
src: ['img/**/*.{png,jpg,gif}']
dest: extraConfig.dir.dist
]
tmp: {}
dev: {}
concurrent:
start:
tasks: ['connect', 'watch', 'brerror:server']
options:
logConcurrentOutput: true
gruntPath: path.join __dirname, 'node_modules', 'grunt-cli', 'bin', 'grunt'
newer:
options:
override: (detail, include) ->
cwdPath = grunt.config("#{detail.task}.#{detail.target}.files.0.cwd")
return include() unless cwdPath?
cwd = path.join __dirname, cwdPath
baseFile = path.join __dirname, detail.path
content = fs.readFileSync baseFile, 'utf8'
compile = needsCompile cwd, baseFile, detail.time, content
include(compile)
clean:
tmp: [extraConfig.dir.tmp]
dist: [extraConfig.dir.dist]
dev: [extraConfig.dir.dist]
glob:
tmp:
files: htmlFiles
dev:
files: htmlDevFiles
dist:
files: htmlDistFiles
options:
concat: true
minify: true
cdnify:
tmp:
files: htmlFiles
options:
useLocal: true
dev:
files: htmlDevFiles
options:
useLocal: true
dist:
files: htmlDistFiles
options:
incompatible: ['glob']
i18n: i18nOptions
needsCompile = (file, baseFile, time, content) ->
stat = fs.statSync file
if stat.isDirectory()
dirFiles = fs.readdirSync(file)
for f in dirFiles
filepath = path.join file, f
return true if needsCompile filepath, baseFile, time, content
else
return false if file == baseFile || stat.mtime > time
filename = path.basename(file)
return false if filename[0] == '.'
word = filename.substr(0, filename.lastIndexOf('.'))
return true if content.indexOf(word) > -1
return false
mapFile = (filepath) ->
filepath = filepath.replace /^(assets|views)/, extraConfig.dir.tmp
filepath = filepath.replace /\.styl$/, '.css'
filepath = filepath.replace /\.coffee$/, '.js'
filepath = filepath.replace /\.jade$/, '.html'
filepath
grunt.event.on 'watch', (event, file, task) ->
return unless event == 'deleted'
filepath = path.join __dirname, mapFile(file)
grunt.file.delete(filepath) if fs.existsSync filepath
grunt.registerTask 'makeCopy', (env) ->
if env == 'tmp'
grunt.task.run ["copy:tmpAssets", "copy:tmpComponents"]
else
grunt.task.run ["copy:dist"]
grunt.registerTask 'views', (env, brerror, useNewer) ->
prefixes = []
prefixes.push 'brerror' if brerror
prefixes.push 'newer' if useNewer
prefix = prefixes.join(':')
prefix += ':' unless _.isEmpty(prefix)
tasks = [
"#{prefix}jade:#{env}"
"cdnify:#{env}"
"glob:#{env}"
]
tasks.push "i18n:#{env}" if i18n
grunt.task.run tasks
compileTasks = [
'clean'
'makeCopy'
'imagemin'
'stylus'
'coffee'
'views'
]
suffixedTasks = ['views']
_.each compileTasks, (task) ->
capTask = capitalize(task)
grunt.registerTask "run#{capTask}", (env, newer, brerror) ->
[prefix, suffix] = ['', '']
if task in suffixedTasks
suffix += ':true' if newer
suffix += ':true' if brerror
else
prefix += 'brerror:' if brerror
prefix += 'newer:' if newer
tasks = []
tasks.push "before#{capTask}:#{env}" if grunt.task.exists("before#{capTask}")
tasks.push "#{prefix}#{task}:#{env}#{suffix}"
tasks.push "after#{capTask}:#{env}" if grunt.task.exists("after#{capTask}")
grunt.event.emit "#{task}.run", env
grunt.task.run tasks
grunt.registerTask 'compile', 'Compiles the website', (env) ->
tasks = _.map compileTasks, (t) -> "run#{capitalize(t)}:#{env}"
tasks.unshift "beforeCompile:#{env}" if grunt.task.exists("beforeCompile")
tasks.push "afterCompile:#{env}" if grunt.task.exists("afterCompile")
grunt.task.run tasks
grunt.registerTask 'default', ['compile:tmp', 'concurrent:start']
if fs.existsSync('grunt.hooks.coffee') || fs.existsSync('grunt.hooks.js')
require('./grunt.hooks')(grunt, extraConfig)
grunt.event.emit 'loaded'