Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var gulp            = require('gulp'),
  2.     argv            = require('yargs').argv,
  3.     autoprefixer    = require('gulp-autoprefixer'),
  4.     browsersync     = require('browser-sync').create(),
  5.     buffer          = require('vinyl-buffer'),
  6.     clean           = require('gulp-clean'),
  7.     colors          = require('colors'),
  8.     concat          = require('gulp-concat'),
  9.     gutil           = require('gulp-util'),
  10.     imagemin        = require('gulp-imagemin'),
  11.     jeditor         = require("gulp-json-editor"),
  12.     merge           = require('merge-stream'),
  13.     notify          = require('gulp-notify'),
  14.     prompt          = require('gulp-prompt'),
  15.     rename          = require('gulp-rename'),
  16.     replace         = require('gulp-replace'),
  17.     sass            = require('gulp-sass'),
  18.     shell           = require('gulp-shell'),
  19.     sourcemaps      = require('gulp-sourcemaps'),
  20.     spritesmith     = require('gulp.spritesmith'),
  21.     uglify          = require('gulp-uglify'),
  22.     util            = require('util');
  23.  
  24. var theme_name = 'candeocreative';
  25.  
  26. gulp.task('help', function() {
  27.     console.log('');
  28.     console.log('Simply run ' + 'gulp'.green + ' to do most things. If you need to run individual tasks, reference the list below.');
  29.     console.log('');
  30.     console.log('Pass the -' + 'p'.cyan + ', --' + 'prod'.cyan + ', or --' + 'production'.cyan + ' args to build for production.');
  31.     console.log('');
  32.     console.log('gulp' + ' ' + 'setup'.green + '                ' + '# Run the initial config. Only available if not already ran.'.grey);
  33.     console.log('gulp' + ' ' + 'build'.green + '                ' + '# Build everything.'.grey);
  34.     console.log('gulp' + ' ' + 'clean'.green + '                ' + '# Clean up built files.'.grey);
  35.     // console.log('gulp' + ' ' + 'copy:themes'.green + '          ' + '# Copy theme files, building styles and scripts along the way.'.grey);
  36.     // console.log('gulp' + ' ' + 'only_copy:themes'.green + '     ' + '# Copy theme files without building anything.'.grey);
  37.     // console.log('gulp' + ' ' + 'copy:plugins'.green + '         ' + '# Copy plugin files, building styles and scripts along the way.'.grey);
  38.     // console.log('gulp' + ' ' + 'only_copy:plugins'.green + '    ' + '# Copy plugin files without building anything.'.grey);
  39.     console.log('gulp' + ' ' + 'styles'.green + '               ' + '# Render styles, inject to browsersync if available.'.grey);
  40.     console.log('gulp' + ' ' + 'scripts'.green + '              ' + '# Concat and uglify javascript.'.grey);
  41.     console.log('gulp' + ' ' + 'sprites'.green + '              ' + '# Build sprites (including retina), run imagemin on the results.'.grey);
  42.     console.log('gulp' + ' ' + 'watch'.green + '                ' + '# Default gulp task.'.grey);
  43.     console.log('gulp' + ' ' + 'write_version'.green + '        ' + '# Write a new cachebreaker manually'.grey);
  44.  
  45.     console.log('gulp' + ' ' + 'uninstall'.red + '            ' + '# Remove your configuration files and start over'.grey);
  46.     console.log('');
  47. });
  48.  
  49. // just to help with booleans
  50. function interpretBooleanInput(string) {
  51.     if (string == "true")
  52.         return true;
  53.     if (string == "false")
  54.         return false;
  55.     return string;
  56. }
  57.  
  58. // for true/false only...
  59. function interpretBooleanOnlyInput(string) {
  60.     if (string == "true")
  61.         return true;
  62.     return false;
  63. }
  64.  
  65. // try to include the config file
  66. try {
  67.     var config = require('./gulp.config.json');
  68. }
  69. catch (err) {
  70.     // It doesn't exist, setup mode!
  71.     gutil.log('Configuration file not found. Entering setup mode!');
  72.     gutil.log('Note: Defaults will be used if any line is left blank');
  73.     gutil.log('Note: true/false will be interpretted as booleans. Strings will be used otherwise.');
  74.  
  75.     gulp.task('default', ['setup'], function() {
  76.  
  77.     });
  78.  
  79.     // gulp.config.json
  80.     var browsersync_proxy = false;
  81.     var browsersync_reload = false;
  82.     var wpengine_name = false;
  83.  
  84.     // local.config.php
  85.     var db_name = '';
  86.     var db_user = 'root';
  87.     var db_password = '';
  88.     var db_host = 'localhost';
  89.     var force_ssl = false;
  90.  
  91.     // setup task, this looks a bit unruly but it does get the job done.
  92.     gulp.task('setup', function() {
  93.         gulp.src('gulp.config.sample.json') // Doesn't actually matter
  94.         .pipe(prompt.confirm('Continue with setup?')) // Give them the option to back out now
  95.         .pipe(prompt.prompt({
  96.             type: 'input',
  97.             name: 'val',
  98.             message: 'Browsersync Proxy & Virtual Host Domain? (Default: false)'
  99.         }, function(res){
  100.             var interpretted = interpretBooleanInput(res.val)
  101.             if (interpretted)
  102.                 browsersync_proxy = interpretted;
  103.         }))
  104.         .pipe(prompt.prompt({
  105.             type: 'input',
  106.             name: 'val',
  107.             message: 'WPEngine Site Name? (Default: false) [Used to automatically add git remotes for deployments]'
  108.         }, function(res){
  109.             var interpretted = interpretBooleanInput(res.val)
  110.             if (interpretted)
  111.                 wpengine_name = interpretted;
  112.         }))
  113.         .pipe(prompt.prompt({
  114.             type: 'input',
  115.             name: 'val',
  116.             message: 'Reload browsersync instead of injecting styles? (Default: false; Boolean only!) [This doesn\'t actually work right now...]'
  117.         }, function(res){
  118.             var interpretted = interpretBooleanOnlyInput(res.val)
  119.             if (interpretted)
  120.                 browsersync_reload = interpretted;
  121.         }))
  122.         .pipe(prompt.prompt({
  123.             type: 'input',
  124.             name: 'val',
  125.             message: 'Database name? (Will create a database for you. NO DEFAULT.)'
  126.         }, function(res){
  127.             var interpretted = interpretBooleanInput(res.val)
  128.             if (interpretted)
  129.                 db_name = interpretted;
  130.         }))
  131.         .pipe(prompt.prompt({
  132.             type: 'input',
  133.             name: 'val',
  134.             message: 'Database user? (Default: "root")'
  135.         }, function(res){
  136.             var interpretted = interpretBooleanInput(res.val)
  137.             if (interpretted)
  138.                 db_user = interpretted;
  139.         }))
  140.         .pipe(prompt.prompt({
  141.             type: 'input',
  142.             name: 'val',
  143.             message: 'Database password? (Default: "")'
  144.         }, function(res){
  145.             var interpretted = interpretBooleanInput(res.val)
  146.             if (interpretted)
  147.                 db_password = interpretted;
  148.         }))
  149.         .pipe(prompt.prompt({
  150.             type: 'input',
  151.             name: 'val',
  152.             message: 'Database host? (Database host: "localhost")'
  153.         }, function(res){
  154.             var interpretted = interpretBooleanInput(res.val)
  155.             if (interpretted)
  156.                 db_host = interpretted;
  157.         }))
  158.         .pipe(prompt.prompt({
  159.             type: 'input',
  160.             name: 'val',
  161.             message: 'Force SSL? (Default: false)'
  162.         }, function(res){
  163.             var interpretted = interpretBooleanInput(res.val)
  164.             if (interpretted)
  165.                 force_ssl = interpretted;
  166.  
  167.             gutil.log("Applying configurations...");
  168.  
  169.             // nesting gulp pipes seems like a good idea...
  170.             // first do the json config with jeditor
  171.             gulp.src('gulp.config.sample.json')
  172.             .pipe(jeditor({
  173.                 "browsersync_proxy":browsersync_proxy,
  174.                 "theme_name":theme_name,
  175.                 "browsersync_reload":browsersync_reload,
  176.                 "wpengine_name":wpengine_name
  177.             }))
  178.             .pipe(rename('gulp.config.json'))
  179.             .pipe(gulp.dest('./'));
  180.  
  181.             // Then just replace the bracketed stuff with the variables in local.config.php and move it
  182.             gulp.src('local.config.php.sample')
  183.             .pipe(replace('{db_name}', db_name))
  184.             .pipe(replace('{db_user}', db_user))
  185.             .pipe(replace('{db_password}', db_password))
  186.             .pipe(replace('{db_host}', db_host))
  187.             .pipe(replace('{force_ssl}', force_ssl))
  188.             .pipe(replace('{theme_name}', theme_name))
  189.             .pipe(rename('local.config.php'))
  190.             .pipe(gulp.dest('./'));
  191.  
  192.             // Create the database manually..
  193.             var shellCommand;
  194.             if (db_password == '') {
  195.                 shellCommand = util.format('mysql -u %s -e "CREATE DATABASE %s"', db_user, db_name)
  196.             } else {
  197.                 shellCommand = util.format('mysql -u %s -p %s -e "CREATE DATABASE %s"', db_user, db_password, db_name);
  198.             }
  199.             gulp.src('local.config.php.sample').pipe(shell([shellCommand]));
  200.  
  201.             // Add git remotes
  202.             var add_staging_remote = 'git remote add staging git@git.wpengine.com:staging/' + wpengine_name + '.git';
  203.             var add_production_remote = 'git remote add production git@git.wpengine.com:production/' + wpengine_name + '.git';
  204.             gulp.src('local.config.php.sample').pipe(shell([add_staging_remote]));
  205.             gulp.src('local.config.php.sample').pipe(shell([add_production_remote]));
  206.  
  207.             // Create the install files that wpengine wont let us track, either because they are not allowed or will cause issues.
  208.             gulp.src('./index.php.gulp.install')
  209.                 .pipe(rename('index.php'))
  210.                 .pipe(gulp.dest('./'));
  211.  
  212.             gulp.src('./wordpress/wpcfg.php.gulp.install')
  213.                 .pipe(rename('wp-config.php'))
  214.                 .pipe(gulp.dest('./wordpress/'));
  215.  
  216.             // Removed for now, if statement used to be...:
  217.             // if (theme_name != "base") {
  218.             if (false) {
  219.                 // Copy theme files, throw in style.css from the sample.
  220.                 gulp.src(['./src/themes/base/**/*', '!./src/themes/base/style.css'])
  221.                 .pipe(gulp.dest('./src/themes/' + theme_name));
  222.  
  223.                 gulp.src('style.sample.css')
  224.                 .pipe(replace('{theme_name}', theme_name))
  225.                 .pipe(rename('style.css'))
  226.                 .pipe(gulp.dest('./src/themes/' + theme_name));
  227.             }
  228.  
  229.             gutil.log("All set! Are you on Windows and using XAMPP? Make sure you run _win_add_host_entry.bat as well!");
  230.         }));
  231.     });
  232. }
  233.  
  234. if (config) {
  235.     // src paths
  236.     var paths = {
  237.         sprites: './src/sprites/**/*.png',
  238.         sprites2x: './src/sprites/**/*@2x.png',
  239.         scripts: './src/js/**/*.js',
  240.         styles: './src/scss/**/*.scss',
  241.         main_style: './src/scss/main.scss',
  242.         themes: './src/themes/**/*',
  243.         plugins: './src/plugins/**/*',
  244.         site_version: './wp-content/themes/' + config.theme_name + '/_candeo_version.json',
  245.         theme_php: './wp-content/themes/' + config.theme_name + '/**/*.php'
  246.     };
  247.  
  248.     var dest_paths = {
  249.         sprite_css: './src/scss',
  250.         css: './wp-content/themes/' + config.theme_name + '/css',
  251.         img: './wp-content/themes/' + config.theme_name + '/img',
  252.         js: './wp-content/themes/' + config.theme_name + '/js',
  253.         build_scss: './src/scss',
  254.         site_version: './wp-content/themes/' + config.theme_name
  255.     }
  256.  
  257.     gulp.task('default', ['watch'], function() {
  258.  
  259.     });
  260.  
  261.     gulp.task('clean', ['write_version'], function() {
  262.  
  263.     });
  264.  
  265.     /*
  266.     gulp.task('copy:themes', ['styles', 'scripts'], function() {
  267.         gulp.src(paths.themes).pipe(gulp.dest(dest_paths.themes));
  268.     });
  269.  
  270.     gulp.task('only_copy:themes', function() { // only_copy is used for watch tasks
  271.         gulp.src(paths.themes).pipe(gulp.dest(dest_paths.themes));
  272.     });
  273.  
  274.     gulp.task('copy:plugins', ['styles', 'scripts'], function() {
  275.         gulp.src(paths.plugins).pipe(gulp.dest(dest_paths.plugins));
  276.     })
  277.  
  278.     gulp.task('only_copy:plugins', function() { // only_copy is used for watch tasks
  279.         gulp.src(paths.plugins).pipe(gulp.dest(dest_paths.plugins));
  280.     })
  281.     */
  282.  
  283.     gulp.task('styles', ['clean', 'sprites'], function () {
  284.         var production = false;
  285.         if (argv.p || argv.prod || argv.production)
  286.             production = true;
  287.  
  288.         if (production) {
  289.             return gulp.src(paths.main_style)
  290.                 .pipe(sass({
  291.                     outputStyle: 'compressed'
  292.                 }))
  293.                 .on('error', notify.onError(function (error) {
  294.                     return 'Sass Error: ' + error;
  295.                 }))
  296.                 .pipe(autoprefixer())
  297.                 .pipe(gulp.dest(dest_paths.css))
  298.                 .pipe(browsersync.reload({stream: true}));
  299.         }
  300.         else {
  301.             return gulp.src(paths.main_style)
  302.                 .pipe(sourcemaps.init())
  303.                 .pipe(sass({
  304.                     outputStyle: 'expanded'
  305.                 }))
  306.                 .on('error', notify.onError(function (error) {
  307.                     return 'Sass Error: ' + error;
  308.                 }))
  309.                 .pipe(sourcemaps.write())
  310.                 .pipe(autoprefixer())
  311.                 .pipe(gulp.dest(dest_paths.css))
  312.                 .pipe(browsersync.reload({stream: true}));
  313.         }
  314.     });
  315.  
  316.     gulp.task('scripts', ['clean'], function() {
  317.         var production = false;
  318.         if (argv.p || argv.prod || argv.production)
  319.             production = true;
  320.  
  321.         if (production) {
  322.             return gulp.src(paths.scripts)
  323.                 .pipe(concat('concat.js'))
  324.                 .pipe(gulp.dest(dest_paths.js))
  325.                 .pipe(rename('uglify.js'))
  326.                 .pipe(uglify().on('error', gutil.log))
  327.                 .pipe(gulp.dest(dest_paths.js));
  328.         }
  329.         else {
  330.             return gulp.src(paths.scripts)
  331.                 .pipe(sourcemaps.init())
  332.                 .pipe(concat('concat.js'))
  333.                 .pipe(gulp.dest(dest_paths.js))
  334.                 .pipe(rename('uglify.js'))
  335.                 .pipe(uglify().on('error', gutil.log))
  336.                 .pipe(sourcemaps.write())
  337.                 .pipe(gulp.dest(dest_paths.js));
  338.         }
  339.     });
  340.  
  341.     gulp.task('sprites', function() {
  342.         var spriteData;
  343.  
  344.         spriteData = gulp.src(paths.sprites).pipe(spritesmith({
  345.             imgName: '../img/sprite.png',
  346.             cssName: '_sprite.scss',
  347.             retinaSrcFilter: [paths.sprites2x],
  348.             retinaImgName: '../img/sprite@2x.png'
  349.         }))
  350.         .on('error', gutil.log);
  351.  
  352.         // Pipe image stream through image optimizer and onto disk
  353.         var imgStream = spriteData.img
  354.             // DEV: We must buffer our stream into a Buffer for `imagemin`
  355.             .pipe(buffer())
  356.             .pipe(imagemin())
  357.             .pipe(gulp.dest(dest_paths.img));
  358.  
  359.         // Pipe CSS stream through CSS optimizer and onto disk
  360.         var cssStream = spriteData.css
  361.             .pipe(gulp.dest(dest_paths.sprite_css));
  362.  
  363.         // Return a merged stream to handle both `end` events
  364.         return merge(imgStream, cssStream);
  365.     });
  366.  
  367.     gulp.task('build', ['styles', 'scripts'], function() {
  368.  
  369.     });
  370.  
  371.     // Watch - run build first, then just update what is needed.
  372.     gulp.task('watch', ['build'], function () {
  373.         if (config.browsersync_proxy) {
  374.             browsersync.init({
  375.                 proxy: config.browsersync_proxy,
  376.                 xip: true,
  377.                 reloadDelay: 0,
  378.                 open: "external",
  379.             });
  380.         }
  381.  
  382.         gulp.watch(paths.styles, ['styles']).on('change', browsersync.reload);
  383.         gulp.watch(paths.scripts, ['scripts']).on('change', browsersync.reload);
  384.         gulp.watch('src/sprites/**/*.png', ['sprites']).on('change', browsersync.reload);
  385.         gulp.watch(paths.theme_php).on('change', browsersync.reload);
  386.         if (config.browsersync_proxy && config.browsersync_reload) {
  387.             // Also reload browsersync on theme/plugin changes.
  388.             gulp.watch(paths.main_style).on('change', browsersync.reload);
  389.             gulp.watch(paths.sprites).on('change', browsersync.reload);
  390.         }
  391.     });
  392.  
  393.     // Write version for cachebreaker automation. Just a random hexstring.
  394.     gulp.task('write_version', function() {
  395.         function randomstring(length, chars) {
  396.             var result = '';
  397.             for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
  398.             return result;
  399.         }
  400.         var rstring = randomstring(32, '0123456789abcdef');
  401.  
  402.         return gulp.src(paths.site_version)
  403.             .pipe(jeditor({
  404.                 "v":rstring,
  405.             }))
  406.             .pipe(gulp.dest(dest_paths.site_version));
  407.     });
  408.  
  409.     gulp.task('uninstall', ['clean'], function() {
  410.         return gulp.src(['./gulp.config.json', './local.config.php', './index.php', './wordpress/wp-config.php'])
  411.             .pipe(prompt.confirm('Are you sure? This cannot be undone'))
  412.             .pipe(clean());
  413.     });
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement