Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var gulp = require('gulp'),
- argv = require('yargs').argv,
- autoprefixer = require('gulp-autoprefixer'),
- browsersync = require('browser-sync').create(),
- buffer = require('vinyl-buffer'),
- clean = require('gulp-clean'),
- colors = require('colors'),
- concat = require('gulp-concat'),
- gutil = require('gulp-util'),
- imagemin = require('gulp-imagemin'),
- jeditor = require("gulp-json-editor"),
- merge = require('merge-stream'),
- notify = require('gulp-notify'),
- prompt = require('gulp-prompt'),
- rename = require('gulp-rename'),
- replace = require('gulp-replace'),
- sass = require('gulp-sass'),
- shell = require('gulp-shell'),
- sourcemaps = require('gulp-sourcemaps'),
- spritesmith = require('gulp.spritesmith'),
- uglify = require('gulp-uglify'),
- util = require('util');
- var theme_name = 'candeocreative';
- gulp.task('help', function() {
- console.log('');
- console.log('Simply run ' + 'gulp'.green + ' to do most things. If you need to run individual tasks, reference the list below.');
- console.log('');
- console.log('Pass the -' + 'p'.cyan + ', --' + 'prod'.cyan + ', or --' + 'production'.cyan + ' args to build for production.');
- console.log('');
- console.log('gulp' + ' ' + 'setup'.green + ' ' + '# Run the initial config. Only available if not already ran.'.grey);
- console.log('gulp' + ' ' + 'build'.green + ' ' + '# Build everything.'.grey);
- console.log('gulp' + ' ' + 'clean'.green + ' ' + '# Clean up built files.'.grey);
- // console.log('gulp' + ' ' + 'copy:themes'.green + ' ' + '# Copy theme files, building styles and scripts along the way.'.grey);
- // console.log('gulp' + ' ' + 'only_copy:themes'.green + ' ' + '# Copy theme files without building anything.'.grey);
- // console.log('gulp' + ' ' + 'copy:plugins'.green + ' ' + '# Copy plugin files, building styles and scripts along the way.'.grey);
- // console.log('gulp' + ' ' + 'only_copy:plugins'.green + ' ' + '# Copy plugin files without building anything.'.grey);
- console.log('gulp' + ' ' + 'styles'.green + ' ' + '# Render styles, inject to browsersync if available.'.grey);
- console.log('gulp' + ' ' + 'scripts'.green + ' ' + '# Concat and uglify javascript.'.grey);
- console.log('gulp' + ' ' + 'sprites'.green + ' ' + '# Build sprites (including retina), run imagemin on the results.'.grey);
- console.log('gulp' + ' ' + 'watch'.green + ' ' + '# Default gulp task.'.grey);
- console.log('gulp' + ' ' + 'write_version'.green + ' ' + '# Write a new cachebreaker manually'.grey);
- console.log('gulp' + ' ' + 'uninstall'.red + ' ' + '# Remove your configuration files and start over'.grey);
- console.log('');
- });
- // just to help with booleans
- function interpretBooleanInput(string) {
- if (string == "true")
- return true;
- if (string == "false")
- return false;
- return string;
- }
- // for true/false only...
- function interpretBooleanOnlyInput(string) {
- if (string == "true")
- return true;
- return false;
- }
- // try to include the config file
- try {
- var config = require('./gulp.config.json');
- }
- catch (err) {
- // It doesn't exist, setup mode!
- gutil.log('Configuration file not found. Entering setup mode!');
- gutil.log('Note: Defaults will be used if any line is left blank');
- gutil.log('Note: true/false will be interpretted as booleans. Strings will be used otherwise.');
- gulp.task('default', ['setup'], function() {
- });
- // gulp.config.json
- var browsersync_proxy = false;
- var browsersync_reload = false;
- var wpengine_name = false;
- // local.config.php
- var db_name = '';
- var db_user = 'root';
- var db_password = '';
- var db_host = 'localhost';
- var force_ssl = false;
- // setup task, this looks a bit unruly but it does get the job done.
- gulp.task('setup', function() {
- gulp.src('gulp.config.sample.json') // Doesn't actually matter
- .pipe(prompt.confirm('Continue with setup?')) // Give them the option to back out now
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'Browsersync Proxy & Virtual Host Domain? (Default: false)'
- }, function(res){
- var interpretted = interpretBooleanInput(res.val)
- if (interpretted)
- browsersync_proxy = interpretted;
- }))
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'WPEngine Site Name? (Default: false) [Used to automatically add git remotes for deployments]'
- }, function(res){
- var interpretted = interpretBooleanInput(res.val)
- if (interpretted)
- wpengine_name = interpretted;
- }))
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'Reload browsersync instead of injecting styles? (Default: false; Boolean only!) [This doesn\'t actually work right now...]'
- }, function(res){
- var interpretted = interpretBooleanOnlyInput(res.val)
- if (interpretted)
- browsersync_reload = interpretted;
- }))
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'Database name? (Will create a database for you. NO DEFAULT.)'
- }, function(res){
- var interpretted = interpretBooleanInput(res.val)
- if (interpretted)
- db_name = interpretted;
- }))
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'Database user? (Default: "root")'
- }, function(res){
- var interpretted = interpretBooleanInput(res.val)
- if (interpretted)
- db_user = interpretted;
- }))
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'Database password? (Default: "")'
- }, function(res){
- var interpretted = interpretBooleanInput(res.val)
- if (interpretted)
- db_password = interpretted;
- }))
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'Database host? (Database host: "localhost")'
- }, function(res){
- var interpretted = interpretBooleanInput(res.val)
- if (interpretted)
- db_host = interpretted;
- }))
- .pipe(prompt.prompt({
- type: 'input',
- name: 'val',
- message: 'Force SSL? (Default: false)'
- }, function(res){
- var interpretted = interpretBooleanInput(res.val)
- if (interpretted)
- force_ssl = interpretted;
- gutil.log("Applying configurations...");
- // nesting gulp pipes seems like a good idea...
- // first do the json config with jeditor
- gulp.src('gulp.config.sample.json')
- .pipe(jeditor({
- "browsersync_proxy":browsersync_proxy,
- "theme_name":theme_name,
- "browsersync_reload":browsersync_reload,
- "wpengine_name":wpengine_name
- }))
- .pipe(rename('gulp.config.json'))
- .pipe(gulp.dest('./'));
- // Then just replace the bracketed stuff with the variables in local.config.php and move it
- gulp.src('local.config.php.sample')
- .pipe(replace('{db_name}', db_name))
- .pipe(replace('{db_user}', db_user))
- .pipe(replace('{db_password}', db_password))
- .pipe(replace('{db_host}', db_host))
- .pipe(replace('{force_ssl}', force_ssl))
- .pipe(replace('{theme_name}', theme_name))
- .pipe(rename('local.config.php'))
- .pipe(gulp.dest('./'));
- // Create the database manually..
- var shellCommand;
- if (db_password == '') {
- shellCommand = util.format('mysql -u %s -e "CREATE DATABASE %s"', db_user, db_name)
- } else {
- shellCommand = util.format('mysql -u %s -p %s -e "CREATE DATABASE %s"', db_user, db_password, db_name);
- }
- gulp.src('local.config.php.sample').pipe(shell([shellCommand]));
- // Add git remotes
- var add_staging_remote = 'git remote add staging git@git.wpengine.com:staging/' + wpengine_name + '.git';
- var add_production_remote = 'git remote add production git@git.wpengine.com:production/' + wpengine_name + '.git';
- gulp.src('local.config.php.sample').pipe(shell([add_staging_remote]));
- gulp.src('local.config.php.sample').pipe(shell([add_production_remote]));
- // Create the install files that wpengine wont let us track, either because they are not allowed or will cause issues.
- gulp.src('./index.php.gulp.install')
- .pipe(rename('index.php'))
- .pipe(gulp.dest('./'));
- gulp.src('./wordpress/wpcfg.php.gulp.install')
- .pipe(rename('wp-config.php'))
- .pipe(gulp.dest('./wordpress/'));
- // Removed for now, if statement used to be...:
- // if (theme_name != "base") {
- if (false) {
- // Copy theme files, throw in style.css from the sample.
- gulp.src(['./src/themes/base/**/*', '!./src/themes/base/style.css'])
- .pipe(gulp.dest('./src/themes/' + theme_name));
- gulp.src('style.sample.css')
- .pipe(replace('{theme_name}', theme_name))
- .pipe(rename('style.css'))
- .pipe(gulp.dest('./src/themes/' + theme_name));
- }
- gutil.log("All set! Are you on Windows and using XAMPP? Make sure you run _win_add_host_entry.bat as well!");
- }));
- });
- }
- if (config) {
- // src paths
- var paths = {
- sprites: './src/sprites/**/*.png',
- sprites2x: './src/sprites/**/*@2x.png',
- scripts: './src/js/**/*.js',
- styles: './src/scss/**/*.scss',
- main_style: './src/scss/main.scss',
- themes: './src/themes/**/*',
- plugins: './src/plugins/**/*',
- site_version: './wp-content/themes/' + config.theme_name + '/_candeo_version.json',
- theme_php: './wp-content/themes/' + config.theme_name + '/**/*.php'
- };
- var dest_paths = {
- sprite_css: './src/scss',
- css: './wp-content/themes/' + config.theme_name + '/css',
- img: './wp-content/themes/' + config.theme_name + '/img',
- js: './wp-content/themes/' + config.theme_name + '/js',
- build_scss: './src/scss',
- site_version: './wp-content/themes/' + config.theme_name
- }
- gulp.task('default', ['watch'], function() {
- });
- gulp.task('clean', ['write_version'], function() {
- });
- /*
- gulp.task('copy:themes', ['styles', 'scripts'], function() {
- gulp.src(paths.themes).pipe(gulp.dest(dest_paths.themes));
- });
- gulp.task('only_copy:themes', function() { // only_copy is used for watch tasks
- gulp.src(paths.themes).pipe(gulp.dest(dest_paths.themes));
- });
- gulp.task('copy:plugins', ['styles', 'scripts'], function() {
- gulp.src(paths.plugins).pipe(gulp.dest(dest_paths.plugins));
- })
- gulp.task('only_copy:plugins', function() { // only_copy is used for watch tasks
- gulp.src(paths.plugins).pipe(gulp.dest(dest_paths.plugins));
- })
- */
- gulp.task('styles', ['clean', 'sprites'], function () {
- var production = false;
- if (argv.p || argv.prod || argv.production)
- production = true;
- if (production) {
- return gulp.src(paths.main_style)
- .pipe(sass({
- outputStyle: 'compressed'
- }))
- .on('error', notify.onError(function (error) {
- return 'Sass Error: ' + error;
- }))
- .pipe(autoprefixer())
- .pipe(gulp.dest(dest_paths.css))
- .pipe(browsersync.reload({stream: true}));
- }
- else {
- return gulp.src(paths.main_style)
- .pipe(sourcemaps.init())
- .pipe(sass({
- outputStyle: 'expanded'
- }))
- .on('error', notify.onError(function (error) {
- return 'Sass Error: ' + error;
- }))
- .pipe(sourcemaps.write())
- .pipe(autoprefixer())
- .pipe(gulp.dest(dest_paths.css))
- .pipe(browsersync.reload({stream: true}));
- }
- });
- gulp.task('scripts', ['clean'], function() {
- var production = false;
- if (argv.p || argv.prod || argv.production)
- production = true;
- if (production) {
- return gulp.src(paths.scripts)
- .pipe(concat('concat.js'))
- .pipe(gulp.dest(dest_paths.js))
- .pipe(rename('uglify.js'))
- .pipe(uglify().on('error', gutil.log))
- .pipe(gulp.dest(dest_paths.js));
- }
- else {
- return gulp.src(paths.scripts)
- .pipe(sourcemaps.init())
- .pipe(concat('concat.js'))
- .pipe(gulp.dest(dest_paths.js))
- .pipe(rename('uglify.js'))
- .pipe(uglify().on('error', gutil.log))
- .pipe(sourcemaps.write())
- .pipe(gulp.dest(dest_paths.js));
- }
- });
- gulp.task('sprites', function() {
- var spriteData;
- spriteData = gulp.src(paths.sprites).pipe(spritesmith({
- imgName: '../img/sprite.png',
- cssName: '_sprite.scss',
- retinaSrcFilter: [paths.sprites2x],
- retinaImgName: '../img/sprite@2x.png'
- }))
- .on('error', gutil.log);
- // Pipe image stream through image optimizer and onto disk
- var imgStream = spriteData.img
- // DEV: We must buffer our stream into a Buffer for `imagemin`
- .pipe(buffer())
- .pipe(imagemin())
- .pipe(gulp.dest(dest_paths.img));
- // Pipe CSS stream through CSS optimizer and onto disk
- var cssStream = spriteData.css
- .pipe(gulp.dest(dest_paths.sprite_css));
- // Return a merged stream to handle both `end` events
- return merge(imgStream, cssStream);
- });
- gulp.task('build', ['styles', 'scripts'], function() {
- });
- // Watch - run build first, then just update what is needed.
- gulp.task('watch', ['build'], function () {
- if (config.browsersync_proxy) {
- browsersync.init({
- proxy: config.browsersync_proxy,
- xip: true,
- reloadDelay: 0,
- open: "external",
- });
- }
- gulp.watch(paths.styles, ['styles']).on('change', browsersync.reload);
- gulp.watch(paths.scripts, ['scripts']).on('change', browsersync.reload);
- gulp.watch('src/sprites/**/*.png', ['sprites']).on('change', browsersync.reload);
- gulp.watch(paths.theme_php).on('change', browsersync.reload);
- if (config.browsersync_proxy && config.browsersync_reload) {
- // Also reload browsersync on theme/plugin changes.
- gulp.watch(paths.main_style).on('change', browsersync.reload);
- gulp.watch(paths.sprites).on('change', browsersync.reload);
- }
- });
- // Write version for cachebreaker automation. Just a random hexstring.
- gulp.task('write_version', function() {
- function randomstring(length, chars) {
- var result = '';
- for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
- return result;
- }
- var rstring = randomstring(32, '0123456789abcdef');
- return gulp.src(paths.site_version)
- .pipe(jeditor({
- "v":rstring,
- }))
- .pipe(gulp.dest(dest_paths.site_version));
- });
- gulp.task('uninstall', ['clean'], function() {
- return gulp.src(['./gulp.config.json', './local.config.php', './index.php', './wordpress/wp-config.php'])
- .pipe(prompt.confirm('Are you sure? This cannot be undone'))
- .pipe(clean());
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement