Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     // general stuff
  2. var gulp = require("gulp"),                      // gulp
  3.     fs = require("fs"),                          // the file system
  4.     notify = require("gulp-notify"),             // notifications
  5.     plumber = require("gulp-plumber"),           // prevent pipe breaking
  6.     runSequence = require("run-sequence"),       // allow tasks to be ran in sequence
  7.     json = require("json-file"),                 // read/write JSON files
  8.     prompt = require("gulp-prompt")              // allow user input
  9.     argv = require("yargs").argv,                // --flags
  10.     del = require("del"),                        // delete files & folders
  11.     newer = require("gulp-newer"),               // checks if files are newer
  12.     merge = require("merge-stream"),             // merge streams
  13.     gulpif = require("gulp-if"),                 // if statements in pipes
  14.     watch = require("gulp-watch"),               // watch for file changes
  15.     sourcemaps = require("gulp-sourcemaps"),     // sourcemaps
  16.     concat = require("gulp-concat"),             // concatenater
  17.     fileinclude = require("gulp-file-include"),  // file includer, variable replacer
  18.  
  19.     // media stuff
  20.     imagemin = require("gulp-imagemin"),         // image compressor
  21.     pngquant = require("imagemin-pngquant"),     // image compressor for PNGs
  22.  
  23.     // JS stuff
  24.     jshint = require("gulp-jshint"),             // linter
  25.     uglify = require("gulp-uglify"),             // concatenater
  26.  
  27.     // CSS stuff
  28.     sass = require("gulp-sass"),                 // SCSS compiler
  29.     autoprefixer = require("gulp-autoprefixer"), // autoprefix CSS
  30.  
  31.     // HTML stuff
  32.     phpMinify = require("gulp-php-minify"),      // PHP minifier
  33.  
  34.     // FTP stuff
  35.     ftp = require("vinyl-ftp"),                  // FTP client
  36.  
  37.     ftpHost = "",                                // FTP hostname (leave blank)
  38.     ftpUser = "",                                // FTP username (leave blank)
  39.     ftpPass = "",                                // FTP password (leave blank)
  40.     ftpPath = "",                                // FTP path (leave blank)
  41.  
  42.     // browser-sync stuff
  43.     browserSync = require("browser-sync"),       // browser-sync
  44.  
  45.     bsProxy = "",                                // browser-sync proxy (leave blank)
  46.     bsPort = "",                                 // browser-sync port (leave blank)
  47.     bsOpen = "",                                 // browser-sync open (leave blank)
  48.     bsNotify = "",                               // browser-sync notify (leave blank)
  49.  
  50.     // read data from package.json
  51.     name = json.read("./package.json").get("name"),
  52.     description = json.read("./package.json").get("description"),
  53.     version = json.read("./package.json").get("version"),
  54.     repository = json.read("./package.json").get("repository"),
  55.     license = json.read("./package.json").get("license"),
  56.  
  57.     // set up environment paths
  58.     src = "./src",   // source directory
  59.     dev = "./dev",   // development directory
  60.     dist = "./dist", // production directory
  61.  
  62.     ranTasks = []; // store which tasks where ran
  63.  
  64. // Error handling
  65. var onError = function(err) {
  66.     notify.onError({
  67.         title:    "Gulp",
  68.         subtitle: "Error!",
  69.         message:  "<%= error.message %>",
  70.         sound:    "Beep"
  71.     })(err);
  72.  
  73.     this.emit("end");
  74. };
  75.  
  76. // media task, compresses images & copies media
  77. gulp.task("media", function () {
  78.     "use strict";
  79.  
  80.     // development media directory
  81.     var mediaDirectory = dev + "/assets/media";
  82.     var screenshotDirectory = dev;
  83.  
  84.     // production media directory (if --dist is passed)
  85.     if (argv.dist) {
  86.         mediaDirectory = dist + "/assets/media";
  87.         screenshotDirectory = dist;
  88.     }
  89.  
  90.     // clean directory if --clean is passed
  91.     if (argv.clean) {
  92.         del(mediaDirectory + "/**/*");
  93.         del(screenshotDirectory + "/screenshot.png");
  94.     }
  95.  
  96.     // compress images, copy media
  97.     var media = gulp.src(src + "/assets/media/**/*")
  98.         // check if source is newer than destination
  99.         .pipe(gulpif(!argv.force, newer(mediaDirectory)))
  100.         // compress images
  101.         .pipe(imagemin({
  102.             progressive: true,
  103.             svgoPlugins: [{removeViewBox: false}],
  104.             use: [pngquant()]
  105.         }))
  106.         // output to the compiled directory
  107.         .pipe(gulp.dest(mediaDirectory));
  108.  
  109.     // compress screenshot
  110.     var screenshot = gulp.src(src + "/screenshot.png")
  111.         // check if source is newer than destination
  112.         .pipe(gulpif(!argv.force, newer(screenshotDirectory)))
  113.         // compress screenshot
  114.         .pipe(imagemin({
  115.             progressive: true,
  116.             svgoPlugins: [{removeViewBox: false}],
  117.             use: [pngquant()]
  118.         }))
  119.         // output to the compiled directory
  120.         .pipe(gulp.dest(screenshotDirectory));
  121.  
  122.     // merge both steams back in to one
  123.     return merge(media, screenshot)
  124.         // reload the files
  125.         .pipe(browserSync.reload({stream: true}))
  126.         // notify that the task is complete, if not part of default or watch
  127.         .pipe(gulpif(gulp.seq.indexOf("media") > gulp.seq.indexOf("default"), notify({title: "Success!", message: "Media task complete!", onLast: true})))
  128.         // push the task to the ranTasks array
  129.         .on("data", function() {
  130.             if (ranTasks.indexOf("media") < 0) ranTasks.push("media");
  131.         });
  132. });
  133.  
  134. // scripts task, concatenates & lints JS
  135. gulp.task("scripts", function () {
  136.     "use strict";
  137.  
  138.     // development JS directory
  139.     var jsDirectory = dev + "/assets/scripts";
  140.  
  141.     // production JS directory (if --dist is passed)
  142.     if (argv.dist) jsDirectory = dist + "/assets/scripts";
  143.  
  144.     // clean directory if --clean is passed
  145.     if (argv.clean) del(jsDirectory + "/**/*");
  146.  
  147.     // lint scripts
  148.     var linted = gulp.src([src + "/assets/scripts/*.js", "!" + src + "/assets/scripts/vendor.*.js"])
  149.         // check if source is newer than destination
  150.         .pipe(gulpif(!argv.force, newer(jsDirectory)))
  151.         // lint all non-vendor scripts
  152.         .pipe(jshint())
  153.         // print lint errors
  154.         .pipe(jshint.reporter("default"));
  155.  
  156.     // concatenate scripts
  157.     var concated = gulp.src([src + "/assets/scripts/vendor.*.js", src + "/assets/scripts/jquery.*.js", src + "/assets/scripts/*.js"])
  158.         // check if source is newer than destination
  159.         .pipe(gulpif(!argv.force, newer(jsDirectory + "/all.js")))
  160.         // initialize sourcemap
  161.         .pipe(sourcemaps.init())
  162.         // concatenate to all.js
  163.         .pipe(concat("all.js"))
  164.         // uglify (if --dist is passed)
  165.         .pipe(gulpif(argv.dist, uglify()))
  166.         // write the sourcemap (if --dist isn't passed)
  167.         .pipe(gulpif(!argv.dist, sourcemaps.write()))
  168.         // output to the compiled directory
  169.         .pipe(gulp.dest(jsDirectory));
  170.  
  171.     // copy fallback scripts
  172.     var copied = gulp.src([src + "/assets/scripts/fallback/**/*"])
  173.         // check if source is newer than destination
  174.         .pipe(gulpif(!argv.force, newer(jsDirectory + "/fallback")))
  175.         // output to the compiled directory
  176.         .pipe(gulp.dest(jsDirectory + "/fallback"));
  177.  
  178.     // merge all three steams back in to one
  179.     return merge(linted, concated, copied)
  180.         // reload the files
  181.         .pipe(browserSync.reload({stream: true}))
  182.         // notify that the task is complete, if not part of default or watch
  183.         .pipe(gulpif(gulp.seq.indexOf("scripts") > gulp.seq.indexOf("default"), notify({title: "Success!", message: "Scripts task complete!", onLast: true})))
  184.         // push the task to the ranTasks array
  185.         .on("data", function() {
  186.             if (ranTasks.indexOf("scripts") < 0) ranTasks.push("scripts");
  187.         });
  188. });
  189.  
  190. // styles task, compiles & prefixes SCSS
  191. gulp.task("styles", function () {
  192.     "use strict";
  193.  
  194.     // development CSS directory
  195.     var cssDirectory = dev + "/assets/styles";
  196.  
  197.     // production CSS directory (if --dist is passed)
  198.     if (argv.dist) cssDirectory = dist + "/assets/styles";
  199.  
  200.     // clean directory if --clean is passed
  201.     if (argv.clean) del(cssDirectory + "/**/*");
  202.  
  203.     // compile all SCSS in the root styles directory
  204.     return gulp.src(src + "/assets/styles/*.scss")
  205.         // prevent breaking on error
  206.         .pipe(plumber({errorHandler: onError}))
  207.         // check if source is newer than destination
  208.         .pipe(gulpif(!argv.force, newer({dest: cssDirectory + "/modern.css", extra: [src + "/assets/styles/**/*.scss"]})))
  209.         // initialize sourcemap
  210.         .pipe(sourcemaps.init())
  211.         // compile SCSS (compress if --dist is passed)
  212.         .pipe(gulpif(argv.dist, sass({outputStyle: "compressed"}), sass()))
  213.         // prefix CSS
  214.         .pipe(autoprefixer("last 2 version", "ie 8", "ie 9"))
  215.         // write the sourcemap (if --dist isn't passed)
  216.         .pipe(gulpif(!argv.dist, sourcemaps.write()))
  217.         // output to the compiled directory
  218.         .pipe(gulp.dest(cssDirectory))
  219.         // reload the files
  220.         .pipe(browserSync.reload({stream: true}))
  221.         // notify that the task is complete, if not part of default or watch
  222.         .pipe(gulpif(gulp.seq.indexOf("styles") > gulp.seq.indexOf("default"), notify({title: "Success!", message: "Styles task complete!", onLast: true})))
  223.         // push the task to the ranTasks array
  224.         .on("data", function() {
  225.             if (ranTasks.indexOf("styles") < 0) ranTasks.push("styles");
  226.         });
  227. });
  228.  
  229. // styles task, compiles & prefixes SCSS
  230. gulp.task("html", function () {
  231.     "use strict";
  232.  
  233.     // development HTML directory
  234.     var htmlDirectory = dev;
  235.  
  236.     // production HTML directory (if --dist is passed)
  237.     if (argv.dist) htmlDirectory = dist;
  238.  
  239.     // clean directory if --clean is passed
  240.     if (argv.clean) del([htmlDirectory + "/**/*", "!" + htmlDirectory + "{/assets,/assets/**}"]);
  241.  
  242.     // import HTML files and replace their variables
  243.     return gulp.src([src + "/**/*", "!" + src + "{/assets,/assets/**,/screenshot.png}"])
  244.         // check if source is newer than destination
  245.         .pipe(gulpif(!argv.force, newer(htmlDirectory)))
  246.         // insert variables
  247.         .pipe(fileinclude({
  248.             prefix: "@@",
  249.             basepath: "@file",
  250.             context: {
  251.                 name: name,
  252.                 description: description,
  253.                 version: version,
  254.                 repository: repository,
  255.                 license: license,
  256.             }
  257.         }))
  258.         // minify the HTML
  259.         .pipe(gulpif(argv.dist, phpMinify()))
  260.         // output to the compiled directory
  261.         .pipe(gulp.dest(htmlDirectory))
  262.         // reload the files
  263.         .pipe(browserSync.reload({stream: true}))
  264.         // notify that the task is complete, if not part of default or watch
  265.         .pipe(gulpif(gulp.seq.indexOf("html") > gulp.seq.indexOf("default"), notify({title: "Success!", message: "HTML task complete!", onLast: true})))
  266.         // push the task to the ranTasks array
  267.         .on("data", function() {
  268.             if (ranTasks.indexOf("html") < 0) ranTasks.push("html");
  269.         });
  270. });
  271.  
  272. gulp.task("config", function (cb) {
  273.     "use strict";
  274.  
  275.     fs.stat("./config.json", function (err, stats) {
  276.         if (err != null) {
  277.             fs.writeFile("./config.json", "{\"ftp\": {\"dev\": {\"host\": \"\",\"user\": \"\",\"pass\": \"\",\"path\": \"\"},\"dist\": {\"host\": \"\",\"user\": \"\",\"pass\": \"\",\"path\": \"\"}},\"browsersync\": {\"proxy\": \"\",\"port\": \"\",\"open\": \"\",\"notify\": \"\"}}", function (err) {
  278.                 configureFTP(function() {
  279.                     configureBrowsersync();
  280.                 });
  281.             });
  282.         } else {
  283.             configureFTP(function() {
  284.                 configureBrowsersync();
  285.             });
  286.         }
  287.     });
  288.  
  289.     function configureFTP(cb) {
  290.         // read FTP settingss from config.json
  291.         if (!argv.dist) {
  292.             ftpHost = json.read("./config.json").get("ftp.dev.host"),
  293.             ftpUser = json.read("./config.json").get("ftp.dev.user"),
  294.             ftpPass = json.read("./config.json").get("ftp.dev.pass"),
  295.             ftpPath = json.read("./config.json").get("ftp.dev.path");
  296.         } else {
  297.             ftpHost = json.read("./config.json").get("ftp.dist.host"),
  298.             ftpUser = json.read("./config.json").get("ftp.dist.user"),
  299.             ftpPass = json.read("./config.json").get("ftp.dist.pass"),
  300.             ftpPath = json.read("./config.json").get("ftp.dist.path");
  301.         }
  302.  
  303.         if (argv.all || (gulp.seq.indexOf("config") < gulp.seq.indexOf("ftp") || argv.ftp) && (argv.config || ftpHost === "" || ftpUser === "" || ftpPass === "" || ftpPath === "")) {
  304.             // reconfigure settings in config.json if a field is empty or if --config is passed
  305.             gulp.src("./config.json")
  306.                 .pipe(prompt.prompt([{
  307.                     // prompt for the host
  308.                     type: "input",
  309.                     name: "host",
  310.                     message: "FTP hostname:",
  311.                     default: ftpHost,
  312.                 },
  313.                 {
  314.                     // prompt for the user
  315.                     type: "input",
  316.                     name: "user",
  317.                     message: "FTP username:",
  318.                     default: ftpUser,
  319.                 },
  320.                 {
  321.                     // prompt for the host
  322.                     type: "password",
  323.                     name: "pass",
  324.                     message: "FTP password:",
  325.                     default: ftpPass,
  326.                 },
  327.                 {
  328.                     // prompt for the path
  329.                     type: "input",
  330.                     name: "path",
  331.                     message: "FTP remote path:",
  332.                     default: ftpPath,
  333.                 }], function(res) {
  334.                     // open the browsersync.json
  335.                     var file = json.read("./config.json");
  336.  
  337.                     // update the ftp settings in config.json
  338.                     if (!argv.dist) {
  339.                         file.set("ftp.dev.host", res.host);
  340.                         file.set("ftp.dev.user", res.user);
  341.                         file.set("ftp.dev.pass", res.pass);
  342.                         file.set("ftp.dev.path", res.path);
  343.                     } else {
  344.                         file.set("ftp.dist.host", res.host);
  345.                         file.set("ftp.dist.user", res.user);
  346.                         file.set("ftp.dist.pass", res.pass);
  347.                         file.set("ftp.dist.path", res.path);
  348.                     }
  349.  
  350.                     // write the updated file contents
  351.                     file.writeSync();
  352.  
  353.                     // read browsersync settings from browsersync.json
  354.                     ftpHost = res.host,
  355.                     ftpUser = res.user,
  356.                     ftpPass = res.pass,
  357.                     ftpPath = res.path;
  358.  
  359.                     configureBrowsersync();
  360.                 }));
  361.         } else {
  362.             configureBrowsersync();
  363.         }
  364.     }
  365.  
  366.     function configureBrowsersync() {
  367.         // read browsersync settings from config.json
  368.         bsProxy = json.read("./config.json").get("browsersync.proxy"),
  369.         bsPort = json.read("./config.json").get("browsersync.port"),
  370.         bsOpen = json.read("./config.json").get("browsersync.open"),
  371.         bsNotify = json.read("./config.json").get("browsersync.notify");
  372.  
  373.         if (argv.all || (gulp.seq.indexOf("config") < gulp.seq.indexOf("sync") || argv.sync) && (argv.config || bsProxy === "" || bsPort === "" || bsOpen === "" || bsNotify === "")) {
  374.             // reconfigure settings in config.json if a field is empty or if --config is passed
  375.             gulp.src("./config.json")
  376.                 .pipe(prompt.prompt([{
  377.                     // prompt for the proxy
  378.                     type: "input",
  379.                     name: "proxy",
  380.                     message: "Browsersync proxy:",
  381.                     default: bsProxy,
  382.                 },
  383.                 {
  384.                     // prompt for the port
  385.                     type: "input",
  386.                     name: "port",
  387.                     message: "Browsersync port:",
  388.                     default: bsPort,
  389.                 },
  390.                 {
  391.                     // prompt for how to open
  392.                     type: "input",
  393.                     name: "open",
  394.                     message: "Browsersync open:",
  395.                     default: bsOpen,
  396.                 },
  397.                 {
  398.                     // prompt for whether to notify
  399.                     type: "input",
  400.                     name: "notify",
  401.                     message: "Browsersync notify:",
  402.                     default: bsNotify,
  403.                 }], function(res) {
  404.                     // open the browsersync.json
  405.                     var file = json.read("./config.json");
  406.  
  407.                     // update the browsersync settings in config.json
  408.                     file.set("browsersync.proxy", res.proxy);
  409.                     file.set("browsersync.port", res.port);
  410.                     file.set("browsersync.open", res.open);
  411.                     file.set("browsersync.notify", res.notify);
  412.  
  413.                     // write the updated file contents
  414.                     file.writeSync();
  415.  
  416.                     // read browsersync settings from browsersync.json
  417.                     bsProxy = res.proxy,
  418.                     bsPort = res.port,
  419.                     bsOpen = res.open,
  420.                     bsNotify = res.notify;
  421.  
  422.                     cb();
  423.                 }));
  424.         } else {
  425.             cb();
  426.         }
  427.     }
  428. });
  429.  
  430. // upload to FTP environment, depends on config
  431. gulp.task("ftp", ["config"], function(cb) {
  432.     // development FTP directory
  433.     var ftpDirectory = dev;
  434.  
  435.     // production FTP directory (if --dist is passed)
  436.     if (argv.dist) ftpDirectory = dist;
  437.  
  438.     // create the FTP connection
  439.     var conn = ftp.create({
  440.         host: ftpHost,
  441.         user: ftpUser,
  442.         pass: ftpPass,
  443.         path: ftpPath,
  444.     })
  445.  
  446.     // upload the changed files
  447.     return gulp.src(ftpDirectory + "/**/*")
  448.         // check if files are newer
  449.         .pipe(gulpif(!argv.force, conn.newer(ftpPath)))
  450.         // upload changed files
  451.         .pipe(conn.dest(ftpPath))
  452.         // reload the files
  453.         .pipe(browserSync.reload({stream: true}))
  454.         // notify that the task is complete
  455.         .pipe(notify({title: "Success!", message: "FTP task complete!", onLast: true}));
  456.  
  457.     // return
  458.     cb();;
  459. });
  460.  
  461. // set up a browserSync server, depends on config
  462. gulp.task("sync", ["config"], function(cb) {
  463.     browserSync({
  464.         proxy: bsProxy,
  465.         port: bsPort,
  466.         open: bsOpen,
  467.         notify: bsNotify,
  468.     });
  469. });
  470.  
  471. // default task, runs through everything but dist
  472. gulp.task("default", ["media", "scripts", "styles", "html"], function () {
  473.     "use strict";
  474.  
  475.     // notify that the task is complete
  476.     gulp.src("gulpfile.js")
  477.         .pipe(gulpif(ranTasks.length, notify({title: "Success!", message: "Task(s) complete! [" + ranTasks.join(", ") + "]", onLast: true})));
  478.  
  479.     // trigger FTP task if FTP flag is passed
  480.     if (argv.ftp) runSequence("ftp");
  481.  
  482.     // reset the ranTasks array
  483.     ranTasks.length = 0;
  484. });
  485.  
  486. // watch task, runs through everything but dist, triggers when a file is saved
  487. gulp.task("watch", function () {
  488.     "use strict";
  489.  
  490.     // set up a browserSync server, if --sync is passed
  491.     if (argv.sync) runSequence("sync");
  492.  
  493.     // watch for any changes
  494.     watch("./src/**/*", function () {
  495.         // run through all tasks, then ftp, if --ftp is passed
  496.         runSequence("default");
  497.     });
  498. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement