UNPKG

16.5 kBJavaScriptView Raw
1var amCopy
2var amRewrite
3var amAWS
4var path = require('path')
5
6var manifest
7
8var exports = {
9 build : build,
10 copy : copy,
11 deploy : deploy,
12 images : images,
13 scripts : scripts,
14 styles : styles,
15 watch : watch,
16 buildTaskname : buildTaskname,
17 copyTaskname : copyTaskname,
18 deployTaskname : deployTaskname,
19 imagesTaskname : imagesTaskname,
20 scriptsTaskname : scriptsTaskname,
21 stylesTaskname : stylesTaskname,
22 watchTaskname : watchTaskname
23}
24
25module.exports = function(gulpManifest) {
26 manifest = gulpManifest
27 amCopy = require(path.dirname(__filename) + '/copy.js')(manifest)
28 amRewrite = require(path.dirname(__filename) + '/rewrite.js')(manifest)
29 amAWS = require(path.dirname(__filename) + '/aws.js')(manifest)
30 amImages = require(path.dirname(__filename) + '/images.js')(manifest)
31 return exports
32}
33
34
35
36function build(pkgIndex, destIndex) {
37
38 if(pkgIndex === null || pkgIndex === undefined) {
39 var taskList = "["
40
41 manifest.forEach(function(pkg, pkgIndex, array){
42 taskList += "'" + buildTaskname(pkgIndex) + "',"
43 })
44 taskList = taskList.slice(0, -1)
45 taskList += "]"
46
47 return "gulp.task('" + buildTaskname() + "', " + taskList + ')\n'
48 }
49
50 if(destIndex === null || destIndex === undefined) {
51 var taskList = "["
52
53 manifest[pkgIndex]["dest"].forEach(function(dest, d, array){
54 taskList += "'" + buildTaskname(pkgIndex, d) + "',"
55 })
56 taskList = taskList.slice(0, -1)
57 taskList += "]"
58
59 return "gulp.task('" + buildTaskname(pkgIndex) + "', " + taskList + ')\n'
60 }
61
62 var tasks = "["
63
64 tasks += "'" + scriptsTaskname(pkgIndex, destIndex) + "',"
65 tasks += "'" + stylesTaskname(pkgIndex, destIndex) + "',"
66 tasks += "'" + copyTaskname(pkgIndex, destIndex) + "',"
67 tasks += "'" + imagesTaskname(pkgIndex, destIndex) + "',"
68
69 tasks = tasks.slice(0, -1)
70 tasks += "]"
71 return "gulp.task('" + buildTaskname(pkgIndex, destIndex) + "', " + tasks + ')\n\n'
72}
73
74function copy(pkgIndex, destIndex) {
75
76 if(pkgIndex === null || pkgIndex === undefined) {
77 var taskList = "["
78
79 manifest.forEach(function(pkg, pkgIndex, array){
80 taskList += "'" + copyTaskname(pkgIndex) + "',"
81 })
82 taskList = taskList.slice(0, -1)
83 taskList += "]"
84
85 return "gulp.task('" + copyTaskname() + "', " + taskList + ')\n'
86 }
87
88 if(destIndex === null || destIndex === undefined) {
89 var taskList = "["
90
91 manifest[pkgIndex]["dest"].forEach(function(dest, d, array){
92 taskList += "'" + copyTaskname(pkgIndex, d) + "',"
93 })
94 taskList = taskList.slice(0, -1)
95 taskList += "]"
96
97 return "gulp.task('" + copyTaskname(pkgIndex) + "', " + taskList + ')\n'
98 }
99
100 var task = "gulp.task('" + copyTaskname(pkgIndex, destIndex) + "', function(done){\n"
101
102 task += "\t amCopy.copy(" + pkgIndex + ", " + destIndex + ", done)\n"
103 task += "})\n\n"
104
105 return task
106}
107
108
109function images(pkgIndex, destIndex) {
110
111 if(pkgIndex === null || pkgIndex === undefined) {
112 var taskList = "["
113
114 manifest.forEach(function(pkg, pkgIndex, array){
115 taskList += "'" + imagesTaskname(pkgIndex) + "',"
116 })
117 taskList = taskList.slice(0, -1)
118 taskList += "]"
119
120 return "gulp.task('" + imagesTaskname() + "', " + taskList + ')\n'
121 }
122
123 if(destIndex === null || destIndex === undefined) {
124 var taskList = "["
125
126 manifest[pkgIndex]["dest"].forEach(function(dest, d, array){
127 taskList += "'" + imagesTaskname(pkgIndex, d) + "',"
128 })
129 taskList = taskList.slice(0, -1)
130 taskList += "]"
131
132 return "gulp.task('" + imagesTaskname(pkgIndex) + "', " + taskList + ')\n'
133 }
134
135 var task = "gulp.task('" + imagesTaskname(pkgIndex, destIndex) + "', function(done){\n"
136
137 task += "\t amImages.images(" + pkgIndex + ", " + destIndex + ", done)\n"
138 task += "})\n\n"
139
140 return task
141}
142
143
144function deploy(pkgIndex, destIndex) {
145
146 if(pkgIndex === null || pkgIndex === undefined) {
147 var taskList = "["
148
149 manifest.forEach(function(pkg, pkgIndex, array){
150 taskList += "'" + deployTaskname(pkgIndex) + "',"
151 })
152 taskList = taskList.slice(0, -1)
153 taskList += "]"
154
155 return "gulp.task('" + deployTaskname() + "', " + taskList + ')\n'
156 }
157
158 if(destIndex === null || destIndex === undefined) {
159 var taskList = "["
160
161 manifest[pkgIndex]["dest"].forEach(function(dest, d, array){
162 taskList += "'" + deployTaskname(pkgIndex, d) + "',"
163 })
164 taskList = taskList.slice(0, -1)
165 taskList += "]"
166
167 return "gulp.task('" + deployTaskname(pkgIndex) + "', " + taskList + ')\n'
168 }
169
170 var task = "gulp.task('" + deployTaskname(pkgIndex, destIndex) + "', function(done){\n"
171
172 task += "\t amAWS.deploy(" + pkgIndex + ", " + destIndex + ", done)\n"
173 task += "})\n\n"
174
175 return task
176}
177
178
179
180function scripts(pkgIndex, destIndex) {
181
182 if(pkgIndex === null || pkgIndex === undefined) {
183 var taskList = "["
184
185 manifest.forEach(function(pkg, pkgIndex, array){
186 taskList += "'" + scriptsTaskname(pkgIndex) + "',"
187 })
188 taskList = taskList.slice(0, -1)
189 taskList += "]"
190
191 return "gulp.task('" + scriptsTaskname() + "', " + taskList + ')\n'
192 }
193
194 if(destIndex === null || destIndex === undefined) {
195 var taskList = "["
196
197 manifest[pkgIndex]["dest"].forEach(function(dest, d, array){
198 taskList += "'" + scriptsTaskname(pkgIndex, d) + "',"
199 })
200 taskList = taskList.slice(0, -1)
201 taskList += "]"
202
203 return "gulp.task('" + scriptsTaskname(pkgIndex) + "', " + taskList + ')\n'
204 }
205
206 //-----------------------------------------------------------------
207
208 var destDir = manifest[pkgIndex]["dest"][destIndex]["directory"].replace(/\\/g, '/')
209 var rewrites = manifest[pkgIndex]["dest"][destIndex]["rewrites"]
210 var src = manifest[pkgIndex]["src"]["directory"].replace(/\\/g, '/')
211 var base = src
212 if(base !== "") base += '/'
213
214 var jsGlob = "["
215 var es6Glob = "["
216
217 if(manifest[pkgIndex]["src"]["js"]) {
218 var js = manifest[pkgIndex]["src"]["js"]
219 if(Array.isArray(js) && js.length > 0) {
220 js.forEach(function(glob, i, a){
221 jsGlob += "'" + base + glob.replace(/\\/g, '/') + "',"
222 })
223 jsGlob = jsGlob.slice(0, -1)
224 }
225 else if (js !== "") {
226 jsGlob += "'" + base + js.replace(/\\/g, '/') + "'"
227 }
228 }
229 jsGlob += "]"
230
231 if(manifest[pkgIndex]["src"]["es6"]) {
232 var es6 = manifest[pkgIndex]["src"]["es6"]
233 if(Array.isArray(es6) && es6.length > 0) {
234 es6.forEach(function(glob, i, a){
235 es6Glob += "'" + base + glob.replace(/\\/g, '/') + "',"
236 })
237 es6Glob = es6Glob.slice(0, -1)
238 }
239 else if (es6 !== "") {
240 es6Glob += "'" + base + es6.replace(/\\/g, '/') + "'"
241 }
242 }
243 es6Glob += "]"
244
245 var minify = manifest[pkgIndex]["dest"][destIndex]["minify"]
246
247 var task = "gulp.task('" + scriptsTaskname(pkgIndex, destIndex) + "', function () {\n\n"
248
249 task += "\t var srcDir = '" + src + "'\n"
250 task += "\t var destDir = '" + destDir + "'\n"
251 task += "\t var jsSrcs = " + jsGlob + "\n"
252 task += "\t var es6Srcs = " + es6Glob + "\n"
253 task += "\n"
254
255 if(jsGlob !== "[]") {
256 task += "\n"
257 task += "\t jsSrcs.forEach(function(fileGlob, index, array){\n"
258 task += "\t\t gulp.src(fileGlob, {base : srcDir})\n"
259
260 if(rewrites) {
261 task += "\t\t .pipe(amRewrite.gulpRewrite(" + JSON.stringify(rewrites) +"))\n"
262 }
263 if(minify) {
264 task += "\t\t .pipe(uglify())\n"
265 task += "\t\t .pipe(rename(function(path){\n"
266 task += "\t\t path.extname = '.min.js'\n"
267 task += "\t\t }))\n"
268 }
269 task += "\t\t .pipe(gulp.dest(destDir))\n"
270 task += "\t })\n"
271 }
272
273 if(es6Glob !== "[]") {
274 task += "\n"
275 task += "\t es6Srcs.forEach(function(fileGlob, index, array){\n"
276 task += "\t\t glob.sync(fileGlob).forEach(function(file, index, array){\n"
277 task += "\n"
278 task += "\t\t\t var destRelative = path.relative(srcDir, file).replace(/\\\\/g, '/').replace(/\\/?[^\\/]+$/, '')\n"
279 task += "\t\t\t if(!(destRelative === '' || destDir === '')) destRelative = '/' + destRelative\n"
280 task += "\n"
281 task += "\t\t\t browserify(file).transform('babelify',{presets: ['es2015', 'react']})\n"
282 task += "\t\t\t .bundle()\n"
283 task += "\t\t\t .on('error', function (err) { console.error(err) })\n"
284 task += "\t\t\t .pipe(source(path.basename(file)))\n"
285 task += "\t\t\t .pipe(buffer())\n"
286 if(rewrites) {
287 task += "\t\t\t .pipe(amRewrite.gulpRewrite(" + JSON.stringify(rewrites) +"))\n"
288 }
289 if(minify) {
290 task += "\t\t\t .pipe(uglify())\n"
291 task += "\t\t\t .pipe(rename(function(path){\n"
292 task += "\t\t\t\t path.extname = '.min.js'\n"
293 task += "\t\t\t }))\n"
294 }
295 task += "\t\t\t .pipe(gulp.dest(destDir + destRelative))\n"
296 task += "\t\t })\n"
297 task += "\t })\n"
298 }
299 task += "})\n\n"
300 return task
301}
302
303
304
305function styles(pkgIndex, destIndex) {
306
307 if(pkgIndex === null || pkgIndex === undefined) {
308 var taskList = "["
309
310 manifest.forEach(function(pkg, pkgIndex, array){
311 taskList += "'" + stylesTaskname(pkgIndex) + "',"
312 })
313 taskList = taskList.slice(0, -1)
314 taskList += "]"
315
316 return "gulp.task('" + stylesTaskname() + "', " + taskList + ')\n'
317 }
318
319 if(destIndex === null || destIndex === undefined) {
320 var taskList = "["
321
322 manifest[pkgIndex]["dest"].forEach(function(dest, d, array){
323 taskList += "'" + stylesTaskname(pkgIndex, d) + "',"
324 })
325 taskList = taskList.slice(0, -1)
326 taskList += "]"
327
328 return "gulp.task('" + stylesTaskname(pkgIndex) + "', " + taskList + ')\n'
329 }
330
331 //---------------------------------------------------------------
332
333 var destDir = manifest[pkgIndex]["dest"][destIndex]["directory"].replace(/\\/g, '/')
334 var rewrites = manifest[pkgIndex]["dest"][destIndex]["rewrites"]
335 var src = manifest[pkgIndex]["src"]["directory"].replace(/\\/g, '/')
336 var autoprefix = manifest[pkgIndex]["dest"][destIndex]["autoprefix"]
337
338 var base = src
339 if(base!== '') base += '/'
340
341 var cssGlob = "["
342 var scssGlob = "["
343
344 if(manifest[pkgIndex]["src"]["css"]) {
345 var css = manifest[pkgIndex]["src"]["css"]
346 if(Array.isArray(css) && css.length > 0) {
347 css.forEach(function(glob, i, a){
348 cssGlob += "'" + base + glob.replace(/\\/g, '/') + "',"
349 })
350 cssGlob = cssGlob.slice(0, -1)
351 }
352 else if (css !== "") {
353 cssGlob += "'" + base + css.replace(/\\/g, '/') + "'"
354 }
355 }
356 cssGlob += "]"
357
358 if(manifest[pkgIndex]["src"]["scss"]) {
359 var scss = manifest[pkgIndex]["src"]["scss"]
360 if(Array.isArray(scss) && scss.length > 0) {
361 scss.forEach(function(glob, i, a){
362 scssGlob += "'" + base + glob.replace(/\\/g, '/') + "',"
363 })
364 scssGlob = scssGlob.slice(0, -1)
365 }
366 else if (scss !== "") {
367 scssGlob += "'" + base + scss.replace(/\\/g, '/') + "'"
368 }
369 }
370 scssGlob += "]"
371
372
373 var task = "gulp.task('" + stylesTaskname(pkgIndex, destIndex) + "', function () {\n\n"
374
375 task += "\t var srcDir = '" + src + "'\n"
376 task += "\t var destDir = '" + destDir + "'\n"
377 task += "\t var cssSrcs = " + cssGlob + "\n"
378 task += "\t var scssSrcs = " + scssGlob + "\n"
379 task += "\n"
380
381 if(cssGlob !== "[]") {
382 task += "\n"
383 task += "\t cssSrcs.forEach(function(fileGlob, index, array){\n"
384 task += "\t\t gulp.src(fileGlob, {base : srcDir})\n"
385
386 if(rewrites) {
387 task += "\t\t .pipe(amRewrite.gulpRewrite(" + JSON.stringify(rewrites) +"))\n"
388 }
389 if(autoprefix === undefined || autoprefix) {
390 task += "\t\t .pipe(autoprefixer({\n"
391 task += "\t\t\t browsers: ['last 10 versions'],\n"
392 task += "\t\t\t cascade: false\n"
393 task += "\t\t }))\n"
394 }
395 task += "\t\t .pipe(gulp.dest(destDir))\n"
396 task += "\t })\n"
397 }
398
399 if(scssGlob !== "[]") {
400 task += "\n"
401 task += "\t scssSrcs.forEach(function(fileGlob, index, array){\n"
402 task += "\t\t gulp.src(fileGlob, {base : srcDir})\n"
403 task += "\t\t .pipe(sass().on('error', sass.logError))\n"
404 if(rewrites) {
405 task += "\t\t .pipe(amRewrite.gulpRewrite(" + JSON.stringify(rewrites) +"))\n"
406 }
407 if(autoprefix === undefined || autoprefix) {
408 task += "\t\t .pipe(autoprefixer({\n"
409 task += "\t\t\t browsers: ['last 10 versions'],\n"
410 task += "\t\t\t cascade: false\n"
411 task += "\t\t }))\n"
412 }
413 task += "\t\t .pipe(amRewrite.gulpRewriteFiletype('scss', 'css'))\n"
414 task += "\t\t .pipe(gulp.dest(destDir))\n"
415 task += "\t })\n"
416 }
417
418
419 task += "})\n\n"
420 return task
421}
422
423
424function watch(pkgIndex) {
425
426 if(pkgIndex === null || pkgIndex === undefined) {
427 var taskList = "["
428
429 manifest.forEach(function(pkg, pkgIndex, array){
430 taskList += "'" + watchTaskname(pkgIndex) + "',"
431 })
432 taskList = taskList.slice(0, -1)
433 taskList += "]"
434
435 return "gulp.task('" + watchTaskname() + "', " + taskList + ')\n'
436 }
437
438
439 var task = "gulp.task('" + watchTaskname(pkgIndex) + "', function(){\n"
440
441 var src = manifest[pkgIndex]["src"]
442 var base = src["directory"]
443 if(base !== '') base += '/'
444
445 manifest.forEach(function(pkg, pkgIndex, array){
446
447 var scriptsTasks = "['" + scriptsTaskname(pkgIndex) + "']"
448 var stylesTasks = "['" + stylesTaskname(pkgIndex) + "']"
449
450 if(src.js) {
451 if(Array.isArray(src.js) && src.js.length > 0){
452 for(var i in src.js) {
453 task += "\t gulp.watch('" + base + src.js[i].replace(/\.js/, '*') + "', " + scriptsTasks + ")\n"
454 }
455 }
456 else {
457 if(src.js !== "") task += "\t gulp.watch('" + base + src.js.replace(/\.js/, '*') + "', " + scriptsTasks + ")\n"
458 }
459 }
460
461 if(src.es6) {
462 if(Array.isArray(src.es6) && src.es6.length > 0){
463 for(var i in src.es6) {
464 task += "\t gulp.watch('" + base + src.es6[i].replace(/\.js/, '*') + "', " + sciptsTasks + ")\n"
465 }
466 }
467 else {
468 if(src.es6 !== "") task += "\t gulp.watch('" + base + src.es6.replace(/\.js/, '*') + "', " + scriptsTasks + ")\n"
469 }
470 }
471
472 if(src.css) {
473 if(Array.isArray(src.css) && src.css.length > 0){
474 for(var i in src.css) {
475 task += "\t gulp.watch('" + base + src.css[i].replace(/\.css/, '*') + "', " + stylesTasks + ")\n"
476 }
477 }
478 else {
479 if(src.css !== "") task += "\t gulp.watch('" + base + src.css.replace(/\.css/, '*') + "', " + stylesTasks + ")\n"
480 }
481 }
482
483 if(src.scss) {
484 if(Array.isArray(src.scss) && src.scss.length > 0){
485 for(var i in src.scss) {
486 task += "\t gulp.watch('" + base + src.scss[i].replace(/\.scss/, '*') + "', " + stylesTasks + ")\n"
487 }
488 }
489 else {
490 if(src.scss !== "") task += "\t gulp.watch('" + base + src.scss.replace(/\.scss/, '*') + "', " + stylesTasks + ")\n"
491 }
492 }
493
494
495 })
496
497 task += "})\n\n"
498
499 return task
500
501}
502
503//--------------------------------------------------------------------
504
505
506function buildTaskname(pkgIndex, destIndex) {
507 if(pkgIndex === null || pkgIndex === undefined) return "build_all"
508 if(destIndex === null || destIndex === undefined) return "build_" + pkgIndex
509 return "build_" + pkgIndex + "_" + destIndex
510}
511
512
513function copyTaskname(pkgIndex, destIndex) {
514 if(pkgIndex === null || pkgIndex === undefined) return "copy_all"
515 if(destIndex === null || destIndex === undefined) return "copy_" + pkgIndex
516 return "copy_" + pkgIndex + "_" + destIndex
517}
518
519function deployTaskname(pkgIndex, destIndex) {
520 if(pkgIndex === null || pkgIndex === undefined) return "deploy_all"
521 if(destIndex === null || destIndex === undefined) return "deploy_" + pkgIndex
522 return "deploy_" + pkgIndex + "_" + destIndex
523}
524
525function imagesTaskname(pkgIndex, destIndex) {
526 if(pkgIndex === null || pkgIndex === undefined) return "images_all"
527 if(destIndex === null || destIndex === undefined) return "images_" + pkgIndex
528 return "images_" + pkgIndex + "_" + destIndex
529}
530
531function scriptsTaskname(pkgIndex, destIndex) {
532 if(pkgIndex === null || pkgIndex === undefined) return "scripts_all"
533 if(destIndex === null || destIndex === undefined) return "scripts_" + pkgIndex
534 return "scripts_" + pkgIndex + "_" + destIndex
535}
536
537function stylesTaskname(pkgIndex, destIndex) {
538 if(pkgIndex === null || pkgIndex === undefined) return "styles_all"
539 if(destIndex === null || destIndex === undefined) return "styles_" + pkgIndex
540 return "styles_" + pkgIndex + "_" + destIndex
541}
542
543function watchTaskname(pkgIndex, destIndex) {
544 if(pkgIndex === null || pkgIndex === undefined) return "watch_all"
545 if(destIndex === null || destIndex === undefined) return "watch_" + pkgIndex
546 return "watch_" + pkgIndex + "_" + destIndex
547}
\No newline at end of file