Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. /*--------------------------------------------------------------
  3.  # Gulp
  4.  
  5.  # Learn more about Gulp task runner here: https://scotch.io/tutorials/automate-your-tasks-easily-with-gulp-js
  6.  --------------------------------------------------------------*/
  7.  
  8. /* ---------------- Setup Project ---------------- */
  9.  
  10. /*
  11.  # Environments:
  12.  * SFTP - Run: gulp sftp
  13.  * FTP - Run: gulp ftp
  14.  * Local - Run: gulp local
  15.  */
  16.  
  17. const syncUrl = 'http://192.71.126.76/webhosting-reviews-co-uk/'; // Livereload URL
  18. const hostUrl = '192.71.126.76'; // SFTP/FTP URL
  19. const hostPort = 22; // SFTP/FTP Port
  20. const uploadFolder = '/home/www/wordpress/wp-content/themes/webhosting-reviews-bootstrap'; // SFTP/FTP upload folder
  21.  
  22. // # SFTP/FTP credentials
  23. const uploadUser = 'root'; // Login
  24. const uploadPass = '51d6411938'; // Password
  25.  
  26. /* Set browser support prefixes
  27.  See more at: https://github.com/ai/browserslist#queries  */
  28.  
  29. const autoprefixerOptions = {
  30.     browsers: [
  31.         'ie >= 9',
  32.         'ie_mob >= 10',
  33.         'last 2 versions']
  34. };
  35.  
  36. /* ---------------- Enable Plugins ---------------- */
  37.  
  38. var gulp = require('gulp'); // Core Gulp
  39.  
  40. var sass = require('gulp-sass'); // SCSS compiler
  41. var autoprefixer = require('gulp-autoprefixer'); // CSS Autoprefix from Can I Use
  42. var sourcemaps = require('gulp-sourcemaps'); // Sourcemaps
  43.  
  44. var concat = require('gulp-concat-util'); // Concatenate JS files
  45. var jsmin = require('gulp-jsmin'); // Minify JS files
  46. var rename = require('gulp-rename'); // Rename JS files
  47.  
  48. var imagemin = require('gulp-imagemin'); // Compress images
  49. var pngquant = require('imagemin-pngquant'); // Run "npm i -D imagemin-pngquant" if npm install fails to install this dependency
  50.  
  51. var sftp = require('gulp-sftp'); // SFTP
  52. var ftp = require('ftp'); // FTP (https://github.com/mscdex/node-ftp)
  53.  
  54. var watch = require('gulp-watch'); // Watcher
  55. var browserSync = require("browser-sync"); // Browser Sync
  56.  
  57. /* ---------------- Plugins Configuration ---------------- */
  58.  
  59. // Local files array for watcher
  60. var localFilesGlob = ['assets/src/scss/**/*.scss', 'assets/src/javascript/**/*.js', 'assets/dist/javascript/*.js','assets/dist/javascript/*.map', '**/*.php', 'assets/dist/css/*.css', 'assets/dist/css/*.map', 'assets/dist/img/*'];
  61.  
  62. var reload = browserSync.reload; // Manual BrowserSync reload
  63.  
  64. /* ---------------- Register Tasks ---------------- */
  65.  
  66. // # SCSS Compile
  67. // Docs: https://github.com/sass/node-sass
  68. gulp.task('sass', function () {
  69.     watch('assets/src/**/*.scss', function () {
  70.         return gulp.src('assets/src/scss/style.scss')
  71.             .pipe(sourcemaps.init())
  72.             .pipe(sass({
  73.                 errLogToConsole: true,
  74.                 style: 'compressed',
  75.                 precision: 10
  76.             }).on('error', sass.logError))
  77.             .pipe(autoprefixer(autoprefixerOptions))
  78.             .pipe(sourcemaps.write('.'))
  79.             .pipe(gulp.dest('assets/dist/css'));
  80.     })
  81. });
  82.  
  83. // # Transpile and concatenate JS files // Minify
  84. gulp.task("javascript", function () {
  85.     watch('assets/src/javascript/**/*.js', function () {
  86.         return gulp.src(['assets/src/javascript/plugins/*.js', 'assets/src/javascript/scripts.js'])
  87.             // .pipe(concat("global.js", {
  88.             //     newLine: ';\n'
  89.             // }))
  90.             .pipe(jsmin())
  91.             .pipe(rename({suffix: '.min'}))
  92.             .pipe(gulp.dest("assets/dist/javascript"));
  93.     })
  94. });
  95.  
  96. // # Image Min
  97. gulp.task('imgmin', function () {
  98.     watch('assets/src/img/*', function () {
  99.         return gulp.src('assets/src/img/*')
  100.             .pipe(imagemin({
  101.                 progressive: true,
  102.                 svgoPlugins: [{
  103.                     removeViewBox: false
  104.                 }],
  105.                 use: [pngquant()]
  106.             }))
  107.             .pipe(gulp.dest('assets/dist/img'))
  108.     })
  109. });
  110.  
  111. // # Browser Sync Server
  112. gulp.task('browser-sync', function () {
  113.     browserSync({
  114.         notify: false,
  115.         ui: false,
  116.         proxy: syncUrl
  117.     });
  118. });
  119.  
  120.  
  121. // # Watch and deploy tasks.
  122. // Watch the local copy for changes and copies the new files to the server whenever an update is detected, reloads browser after each successfull transfer
  123. // -- SFTP
  124. gulp.task('sftp-deploy-watch', function () {
  125.     gulp.watch(localFilesGlob)
  126.         .on('change', function (event) {
  127.             process.stdout.write('File "' + event.path + '" changed, uploading to server\n');
  128.  
  129.             return gulp.src([event.path], {
  130.                 base: '.',
  131.                 buffer: false
  132.             })
  133.                 .pipe(sftp({
  134.                     host: hostUrl,
  135.                     remotePath: uploadFolder,
  136.                     port: hostPort,
  137.                     callback: reload,
  138.                     user: uploadUser,
  139.                     pass: uploadPass
  140.                 }))
  141.         });
  142. });
  143.  
  144. // -- FTP
  145. gulp.task('ftp-deploy-watch', function () {
  146.     gulp.watch(localFilesGlob)
  147.         .on('change', function (event) {
  148.             process.stdout.write('File "' + event.path + '" changed, uploading to server\n');
  149.             var srcFilePath = event.path;
  150.             var destFilePath = uploadFolder + '/' + srcFilePath.replace(__dirname + '\\', '').replace(/\\/g, "/");
  151.             var c = new ftp();
  152.             // After FTP connection established:
  153.             c.on('ready', function () {
  154.                 // Try to push file to server
  155.                 c.put(srcFilePath, destFilePath, function (err) {
  156.                     // Log error
  157.                     process.stdout.write('Uploading to => '+destFilePath + '\n');
  158.                         if (err) {
  159.                             // If error matches FTP 550 code (target folder(s) doesnt exist) try recursive MKDIR
  160.                             if (err['code'] == 550) {
  161.                                 process.stdout.write('One of path dirs doesn\'t exist, attempting to mkdir..\n');
  162.                                 var getFolderStructure = destFilePath.substring(0, destFilePath.lastIndexOf("/") + 1);
  163.                                 c.mkdir(getFolderStructure, true, function (err) {
  164.                                     if (err) {
  165.                                         process.stdout.write(err);
  166.                                     }
  167.                                 });
  168.                                 // Then try to upload file again
  169.                                 c.put(srcFilePath, destFilePath, function (err) {
  170.                                     process.stdout.write('Uploading to => '+destFilePath + '\n');
  171.                                     if (err) {
  172.                                         process.stdout.write(err);
  173.                                     }
  174.                                 });
  175.                             }
  176.                         }
  177.                     }
  178.                 );
  179.                 c.end();
  180.             }).on('close', function(){
  181.                 process.stdout.write('Successfully uploaded!\n');
  182.                 reload();
  183.             } );
  184.             // Init connection
  185.             c.connect(
  186.                 {
  187.                     host: hostUrl,
  188.                     user: uploadUser,
  189.                     password: uploadPass
  190.                 }
  191.             )
  192.         })
  193. });
  194.  
  195. // -- Local
  196. gulp.task('local-watch', function () {
  197.     gulp.watch(localFilesGlob)
  198.         .on('change', function (event) {
  199.             process.stdout.write('File "' + event.path + '" changed\n');
  200.             reload();
  201.         })
  202. });
  203. // Run envirnoment (SFTP)
  204. gulp.task('sftp', ['sass', 'imgmin', 'javascript', 'sftp-deploy-watch', 'browser-sync'], function () {
  205.     process.stdout.write('\n ----------------------------------\n Ready. Waiting for changes... \n ----------------------------------\n');
  206. });
  207. // Run envirnoment (FTP)
  208. gulp.task('ftp', ['sass', 'imgmin', 'javascript', 'ftp-deploy-watch', 'browser-sync'], function () {
  209.     process.stdout.write('\n ----------------------------------\n Ready. Waiting for changes... \n ----------------------------------\n');
  210. });
  211. // Run envirnoment (Local)
  212. gulp.task('local', ['sass', 'imgmin', 'javascript', 'local-watch', 'browser-sync'], function () {
  213.     process.stdout.write('\n ----------------------------------\n Ready. Waiting for changes... \n ----------------------------------\n');
  214. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement