forked from romanschejbal/gassetic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
261 lines (236 loc) · 7.36 KB
/
index.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
concat = require "gulp-concat"
clean = require 'gulp-clean'
frep = require "gulp-frep"
fs = require "fs"
gulp = require "gulp"
gutil = require "gulp-util"
livereload = require "gulp-livereload"
path = require "path"
tap = require 'gulp-tap'
q = require 'q'
module.exports = class Gassetic
constructor: (@config, @env, @modules, @log = true) ->
@validateConfig()
###
self explanatory
###
validateConfig: () ->
if !@getMimetypes()?
throw 'missing mimetypes in config'
if !@getDefaultTypes()?
throw 'missing default task in config'
for key of @getMimetypes()
if !@getMimetypes()[key][@env]?
throw 'missing environment ' + @env + ' in ' + key + ' mimetype'
if !@getMimetypes()[key][@env].tasks?
throw 'missing task list for ' + @env + ' environment in ' + key + ' mimetype (it can be empty array but must be defined)'
for task in @getMimetypes()[key][@env].tasks
if !task.name
throw 'invalid task "' + task.toString() + '" for ' + key + ' in ' + @env + ' environment, the structure must be like is {name: coffee, args: { bare: true }}'
if !@getMimetypes()[key].files?
throw 'missing file list for ' + key + ' mimetype'
if !@getMimetypes()[key][@env].outputFolder?
throw 'missing outputFolder path in ' + key + ' ' + @env
src = @getSourceFilesForType key
what = Object.prototype.toString
if what.call(src) != '[object Object]'
throw 'wrong file list for ' + key + ' mimetype'
for file of src
if what.call(file) != '[object String]'
throw 'invalid file "' + file + '" for ' + key + ' in ' + @env + ' environment'
###
@return {Object} mimetypes
###
getMimetypes: () ->
@config.mimetypes
###
@return {Array} default tasks
###
getDefaultTypes: () ->
@config.default
###
###
getSourceFilesForType: (type) ->
@getMimetypes()[type].files
clean: () ->
result = q.defer()
files = []
for type of @getMimetypes()
@getDestinationPathsForType type
.map (f) ->
files.push f
gulp.src(files, read: false).pipe(clean(force: true))
.on 'end', ->
result.resolve true
result.promise
getDestinationPathsForType: (type) ->
paths = []
for key, value of @getMimetypes()[type].files
paths.push path.join @getMimetypes()[type][@env].outputFolder, key
paths
###
Builds all
###
build: () ->
@replaces = {}
@watchFiles = []
finalPromise = q.defer()
promises = []
for type in @getDefaultTypes()
promises.push @buildType type
done = q.all promises
done.then =>
@replaceInFiles @replaces
.then ->
finalPromise.resolve true
finalPromise.promise
cwd: () ->
process.cwd()
###
Builds one type defined in config
###
buildType: (type) ->
buildOne = (type) =>
@replaces[type] = {}
all = []
tasks = @getMimetypes()[type][@env].tasks
gutil.log 'Processing:', gutil.colors.magenta(type), 'with', gutil.colors.gray(
(tasks.map (t) -> t.name + '(' + (if t.args then JSON.stringify(t.args) else '') + ')').join(', ')
) if @log
for destFilename of @getSourceFilesForType type
all.push @buildFiles type, destFilename
q.all all
result = q.defer()
deps = @findDependentTypes type
if deps.length > 0
all = []
while deps.length > 0
next = deps.shift()
all.push @buildType next
q.all all
.then () =>
buildOne.call @, type
.then () ->
result.resolve true
else
buildOne.call @, type
.then () ->
result.resolve true
return result.promise
###
###
buildFiles: (type, destinationFilenameConfigKey) ->
@replaces[type][destinationFilenameConfigKey] = []
result = q.defer()
tasks = @getMimetypes()[type][@env].tasks
gutil.log ' -', gutil.colors.cyan(destinationFilenameConfigKey) if @log
sourceFiles = @getMimetypes()[type].files[destinationFilenameConfigKey]
destination = path.join @getMimetypes()[type][@env].outputFolder, destinationFilenameConfigKey
pipe = gulp.src sourceFiles
tasks.map (t) =>
if t.args?
pipe = pipe.pipe @modules[t.name] [@replaceArgs(t.args, destinationFilenameConfigKey)]...
else if t.callback?
pipe = pipe.pipe @modules[t.name] [@modules[t.callback]]...
else
pipe = pipe.pipe @modules[t.name].call @
pipe = pipe.pipe gulp.dest destination
.pipe tap (f) =>
if @getMimetypes()[type][@env].webPath
webPath = f.path.substring (path.join(@cwd(), @getMimetypes()[type][@env].outputFolder)).length + 1
webPath = path.join @getMimetypes()[type][@env].webPath, webPath
@replaces[type][destinationFilenameConfigKey].push webPath
@watchFiles.push f.path
.on 'end', ->
result.resolve true
return result.promise
replaceArgs: (args, filename) ->
string = JSON.stringify args
string = string.replace '%filename%', filename
JSON.parse string
replaceInFiles: (replacements, callback) ->
regexs = []
for type of replacements
for one of replacements[type]
scripts = '\n'
for filename in replacements[type][one]
scripts += @buildScriptString(filename) + '\n'
regexs.push
pattern: new RegExp('<!-- ' + @env + ':' + one + " -->([\\s\\S]*?)<!-- endbuild -->", "ig")
replacement: "<!-- " + @env + ":" + one + " -->" + scripts + "<!-- endbuild -->"
allfiles = []
progress = []
# find the templates first
gulp.src @config.replacementPaths, read: false
.pipe tap (file) ->
allfiles.push file.path
.on 'end', ->
# do the replace
for file in allfiles
result = q.defer()
progress.push result.promise
((file, deferred) ->
gulp.src file
.pipe frep regexs
.pipe gulp.dest path.dirname file
.on 'end', ->
deferred.resolve true
) file, result
return q.all progress
buildScriptString: (fileWebPath) ->
fileWebPath = fileWebPath.replace /\\/g, '/'
ext = path.extname fileWebPath
switch ext
when ".css"
str = "<link rel=\"stylesheet\" href=\"" + fileWebPath + "\" />"
when ".js"
str = "<script src=\"" + fileWebPath + "\"></script>"
else
str = '<!-- extension not supported -->'
str
###
Finds dependent types for type that needs to be run first
@param {string} type
@param {boolean} recursive
@return {Array} dependency types
###
findDependentTypes: (type, recursive) ->
deps = []
if @config.mimetypes[type].deps?
for d in @config.mimetypes[type].deps
deps.push d
if recursive
deps = deps.concat @findDependentTypes d
deps
watch: () ->
server = livereload()
toWatch = []
for type in @getDefaultTypes()
toWatch.push type
for d in @findDependentTypes type, true
if toWatch.indexOf(d) == -1
toWatch.push d
for type in toWatch
if @getMimetypes()[type].watch?
@watchSources @getMimetypes()[type].watch, type
else
for destinationFile of @getMimetypes()[type].files
sources = @getMimetypes()[type].files[destinationFile]
@watchSources sources, type, destinationFile
gulp.watch @watchFiles
.on 'change', (e) =>
gutil.log gutil.colors.yellow new Date() if @log
gutil.log gutil.colors.blue e.path if @log
server.changed e.path
watchSources: (sources, type, destinationFile = '*') ->
gutil.log 'Watching', gutil.colors.cyan(sources.length), gutil.colors.magenta(type), 'paths for', gutil.colors.green(destinationFile), '...' if @log
gulp.watch sources
.on 'change', (e) =>
if destinationFile != '*'
destFiles = [destinationFile]
else
destFiles = []
for f of @getMimetypes()[type].files
destFiles.push f
for f in destFiles
@buildFiles type, f