Advertisement
davidproweb

Untitled

Jul 23rd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var gulp = require("gulp"),
  2.     gulpMinifyCSS = require("gulp-minify-css"),
  3.     gulpRename = require("gulp-rename"),
  4.     gulpWatch = require("gulp-watch"),
  5.     gulpLess = require("gulp-less"),
  6.     cliColor = require("cli-color"),
  7.     utils = require("./utils"),
  8.     us = require("underscore"),
  9.  
  10.     Q = require("q"),
  11.  
  12.     colorFilePath = cliColor.yellowBright.bold,
  13.     colorFileEvent = cliColor.blueBright,
  14.     colorStatusSucess = cliColor.greenBright,
  15.     colorStatusFail = cliColor.redBright,
  16.  
  17.     translateEvent = {
  18.         "add": "added",
  19.         "start": "started",
  20.         "change": "changed",
  21.         "unlink": "unlinked",
  22.     };
  23.  
  24. function minifyLESS(gulpWatcher) {
  25.     var deferred = Q.defer();
  26.  
  27.     gulp.src(gulpWatcher.capture)
  28.         .pipe(gulpLess())
  29.         .on("error", function(err) {
  30.             process.stdout.write(colorStatusFail("Failed") + "\n");
  31.             deferred.reject();
  32.             return false;
  33.         })
  34.         .pipe(gulpMinifyCSS())
  35.         .pipe(gulpRename(gulpWatcher.rename))
  36.         .pipe(gulp.dest("www/publics"))
  37.         .on("end", function () {
  38.             process.stdout.write(colorStatusSucess("OK LESS") + "\n");
  39.             deferred.resolve();
  40.         });
  41.  
  42.     return deferred.promise;
  43. }
  44.  
  45. function minifyCSS(gulpWatcher) {
  46.     var deferred = Q.defer();
  47.  
  48.     gulp.src(gulpWatcher.capture)
  49.         .pipe(gulpMinifyCSS())
  50.         .pipe(gulpRename(gulpWatcher.rename))
  51.         .pipe(gulp.dest("www/publics"))
  52.         .on("end", function () {
  53.             process.stdout.write(colorStatusSucess("OK CSS ") + "\n");
  54.             deferred.resolve();
  55.         });
  56.  
  57.     return deferred.promise;
  58. }
  59.  
  60. function minifyJS(gulpWatcher) {
  61.     var deferred = Q.defer();
  62.  
  63.     gulp.src(gulpWatcher.capture)
  64.         .pipe(gulpRename(gulpWatcher.rename))
  65.         .pipe(gulp.dest("www/publics"))
  66.         .on("end", function () {
  67.             process.stdout.write(colorStatusSucess("OK JS") + "\n");
  68.             deferred.resolve();
  69.         });
  70.  
  71.     return deferred.promise;
  72. }
  73.  
  74. function startWatcher(gulpWatcher) {
  75.     var promise = Q();
  76.  
  77.     // Notify.
  78.     promise.then(function () {
  79.         utils.writeTimeline("First build of " + colorFilePath(gulpWatcher.rename) + " was " + colorFileEvent("started") + ": ");
  80.     });
  81.  
  82.     // Build a LESS style.
  83.     if (gulpWatcher.type === "less") {
  84.         promise.then(minifyLESS(gulpWatcher));
  85.     }
  86.  
  87.     // Build a CSS style.
  88.     if (gulpWatcher.type === "css") {
  89.         promise.then(minifyCSS(gulpWatcher));
  90.     }
  91.  
  92.     // Build a JS.
  93.     if (gulpWatcher.type === "js") {
  94.         promise.then(minifyJS(gulpWatcher));
  95.     }
  96.  
  97.     return promise;
  98. }
  99.  
  100. gulp.task("watch", function () {
  101.     var gulpWatchers = [
  102.             // Application.
  103.             { "watch": "sources/app.style/**/*.less",
  104.               "capture": "sources/app.style/app.less",
  105.               "rename": "app.css",
  106.               "type": "less" },
  107.             { "watch": "sources/app.script/**/*.js",
  108.               "capture": "sources/app.script/app.js",
  109.               "rename": "app.js",
  110.               "type": "js" },
  111.             // Vendor.
  112.             { "watch": "sources/vendor.style/**/*.css",
  113.               "capture": "sources/vendor.style/*.css",
  114.               "rename": "vendor.css",
  115.               "type": "css" },
  116.             { "watch": "sources/vendor.script/**/*.js",
  117.               "capture": "sources/vendor.script/*.js",
  118.               "rename": "vendor.js",
  119.               "type": "js" },
  120.         ];
  121.  
  122.     // Promise.
  123.     var promise = Q();
  124.  
  125.     // Start all watchers.
  126.     process.stdout.write("\n");
  127.     us.each(gulpWatchers, function (gulpWatcher) {
  128.         promise.then(function () {
  129.             return startWatcher(gulpWatcher);
  130.         });
  131.     });
  132.  
  133.     // Returns the promise.
  134.     return promise;
  135. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement