-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.coffee
373 lines (327 loc) · 9.85 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
livereloadPort = 35729
httpServerPort = 9000
fs = require 'fs'
path = require 'path'
loremIpsum = require 'lorem-ipsum'
_ = require 'lodash'
extraConfigFile = '.leavesrc'
defaults =
i18n:
localesDir: 'locales'
html:
ext: '.html'
css:
outdir: 'css'
js:
outdir: 'js'
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)
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 'tmp', extraConfig.dev.css.outdir
ext: '.css'
]
cssDevFiles = [_.extend {}, cssFiles[0], {dest: path.join('dist', extraConfig.dev.css.outdir)}]
cssDistFiles = [_.extend {}, cssFiles[0], {dest: path.join('dist', extraConfig.dist.css.outdir)}]
templateFiles = [
expand: true
cwd: 'views'
src: ['**/*.jade', '!**/_*.jade', '!layout.jade']
dest: 'tmp'
ext: extraConfig.dev.html.ext
]
templateDistFiles = [_.extend({}, templateFiles[0], {dest: 'dist', ext: extraConfig.dist.html.ext})]
templateDevFiles = [_.extend({}, templateFiles[0], {dest: 'dist'})]
coffeeFiles = [
expand: true
cwd: 'assets/js'
src: ['**/*.coffee']
dest: path.join 'tmp', extraConfig.dev.js.outdir
ext: '.js'
]
coffeeDistFiles = [_.extend({}, coffeeFiles[0], {dest: path.join('dist', extraConfig.dist.js.outdir)})]
coffeeDevFiles = [_.extend({}, coffeeFiles[0], {dest: path.join('dist', extraConfig.dev.js.outdir)})]
htmlFiles = [
expand: true
cwd: 'tmp'
src: ["**/*#{extraConfig.html.ext}", "!**/_*#{extraConfig.html.ext}"]
dest: 'tmp'
ext: extraConfig.dev.html.ext
]
htmlDistFiles = [_.extend({}, htmlFiles[0], {dest: 'dist', cwd: 'dist', ext: extraConfig.dist.html.ext})]
htmlDevFiles = [_.extend({}, htmlFiles[0], {dest: 'dist', cwd: 'dist'})]
i18n = false
i18nOptions =
tmp:
options:
baseDir: 'tmp'
outputDir: 'tmp'
dev: {}
dist: {}
options:
fileFormat: 'yml'
baseDir: 'dist'
outputDir: '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'
tasks: ['runViews:tmp:true']
locales:
files: "#{i18nOptions.options.localesPath}/**/*.#{i18nOptions.options.fileFormat}"
tasks: ['runViews:tmp:true']
options:
livereload: livereloadPort
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
options:
use: [
require 'axis'
]
jade:
tmp:
files: templateFiles
options:
pretty: true
dev:
files: templateDevFiles
options:
pretty: true
dist:
files: templateDistFiles
options:
data:
dev: false
options:
data:
lorem: lorem
dev: true
connect:
server:
options:
port: httpServerPort
keepalive: true
debug: true
base: 'tmp'
useAvailablePort: true
open: true
livereload: true
copy:
tmpAssets:
expand: true
cwd: 'assets'
src: ['**/*', '!css', '!js', '!css/**/*.styl', '!js/**/*.coffee']
dest: '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']
dest: 'dist'
]
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: ['tmp']
dist: ['dist']
dev: ['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)/, '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'
'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.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.overrides.coffee') || fs.existsSync('grunt.overrides.js')
require('./grunt.overrides')(grunt, extraConfig)