|
| 1 | +// Require all dev dependencies. |
| 2 | +var gulp = require('gulp'), |
| 3 | + minify = require('gulp-minify'), |
| 4 | + watch = require('gulp-watch'), |
| 5 | + cleanCSS = require('gulp-clean-css'), |
| 6 | + rename = require('gulp-rename'), |
| 7 | + postcss = require('gulp-postcss'), |
| 8 | + sass = require('gulp-sass'), |
| 9 | + zip = require('gulp-zip'), |
| 10 | + autoprefixer = require('autoprefixer'), |
| 11 | + browserSync = require('browser-sync').create(), |
| 12 | + sourcemaps = require('gulp-sourcemaps'), |
| 13 | + request = require('request'), |
| 14 | + spawn_shell = require('spawn-shell'), |
| 15 | + fs = require('fs'), |
| 16 | + imagemin = require('gulp-imagemin'); |
| 17 | + |
| 18 | + var exec = require('child_process').exec; |
| 19 | + |
| 20 | +// me.js contains vars to your specific setup |
| 21 | +var me = require('./gulpconf.js'); |
| 22 | +var environment = Object.assign({}, process.env, { PATH: process.env.PATH + ':/usr/local/bin' }); |
| 23 | + |
| 24 | +var WEBSITE = me.WEBSITE; |
| 25 | +var CONTENT_TYPE = me.CONTENT_TYPE; |
| 26 | +var BASE_NAME = __dirname.match(/([^\/]*)\/*$/)[1]; |
| 27 | + |
| 28 | +// JS source, destination, and excludes. |
| 29 | +var JS_EXCLD = '!assets/js/*.min.js', |
| 30 | + JS_SRC = 'assets/js/*.js', |
| 31 | + JS_DEST = 'assets/js/'; |
| 32 | + |
| 33 | +// CSS and SASS src, dest, and exclude. |
| 34 | +var CSS_SRC = 'assets/css/*.css', |
| 35 | + CSS_DEST = 'assets/css/', |
| 36 | + CSS_EXCLD = '!assets/css/*.min.css', |
| 37 | + SASS_WATCH = 'assets/scss/*.scss'; |
| 38 | +if( 'plugin' === CONTENT_TYPE){ |
| 39 | + SASS_SRC = 'assets/scss/*.scss'; |
| 40 | +}else{ |
| 41 | + SASS_SRC = ['assets/scss/*.scss', '!assets/scss/style.scss' ]; |
| 42 | +} |
| 43 | + |
| 44 | +// Image src and dest. |
| 45 | +var IMG_SRC = 'assets/images/*', |
| 46 | + IMG_DEST = 'assets/images'; |
| 47 | + |
| 48 | +// Zip src and options. |
| 49 | +var ZIP_SRC_ARR = [ |
| 50 | + './**', |
| 51 | + '!**/composer.*', |
| 52 | + '!**/gulpfile.js', |
| 53 | + '!**/gulpconf.js', |
| 54 | + '!**/package.json', |
| 55 | + '!**/README.md', |
| 56 | + '!**/phpcs.xml', |
| 57 | + '!**/phpcs.ruleset.xml', |
| 58 | + '!**/phpdoc.dist.xml', |
| 59 | + '!**/phpunit.xml.dist', |
| 60 | + '!**/{node_modules,node_modules/**}', |
| 61 | + '!**/{bin,bin/**}', |
| 62 | + '!./{dist,dist/**}', |
| 63 | + '!./{vendor,vendor/**}', |
| 64 | + '!**/{tests,tests/**}' |
| 65 | +]; |
| 66 | +var ZIP_OPTS = { base: '..' }; |
| 67 | + |
| 68 | +// PHP Source. |
| 69 | +var PHP_SRC = '**/*.php'; |
| 70 | + |
| 71 | +/******************************************************************************* |
| 72 | + * Gulp Tasks |
| 73 | + ******************************************************************************/ |
| 74 | + |
| 75 | +/** |
| 76 | + * Default gulp task. Initializes browserSync proxy server and watches src files |
| 77 | + * for any changes. |
| 78 | + * |
| 79 | + * CMD: gulp |
| 80 | + */ |
| 81 | +gulp.task('default', function() { |
| 82 | + |
| 83 | + browserSync.init({ |
| 84 | + proxy: WEBSITE |
| 85 | + }); |
| 86 | + |
| 87 | + gulp.watch( SASS_SRC, ['build-sass']); |
| 88 | + gulp.watch( JS_SRC , ['js-watch']); |
| 89 | + gulp.watch( PHP_SRC, function(){ |
| 90 | + browserSync.reload(); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +/** |
| 95 | + * JS Watch task. This is a dependency task for the default gulp task that |
| 96 | + * builds the js files and reloads the browser in the correct order |
| 97 | + * |
| 98 | + * CMD: None. Not meant to be run as standalone command. |
| 99 | + */ |
| 100 | +gulp.task('js-watch', ['build-js'], function(){ |
| 101 | + browserSync.reload(); |
| 102 | +}); |
| 103 | + |
| 104 | +/** |
| 105 | + * Compiles SCSS into regular CSS. |
| 106 | + * |
| 107 | + * CMD: gulp build-sass |
| 108 | + */ |
| 109 | +gulp.task('build-sass', function() { |
| 110 | + gulp.src( SASS_SRC ) |
| 111 | + .pipe(sourcemaps.init()) |
| 112 | + .pipe(sass().on('error', sass.logError)) |
| 113 | + .pipe(postcss([ |
| 114 | + autoprefixer({browsers: ['> 5% in US']}) |
| 115 | + ])) |
| 116 | + .pipe(cleanCSS({compatibility: 'ie8'})) |
| 117 | + .pipe(sourcemaps.write('.')) |
| 118 | + .pipe(gulp.dest(CSS_DEST)); |
| 119 | + |
| 120 | + gulp.src( 'assets/scss/style.scss' ) |
| 121 | + .pipe(sourcemaps.init()) |
| 122 | + .pipe(sass().on('error', sass.logError)) |
| 123 | + .pipe(postcss([ |
| 124 | + autoprefixer({browsers: ['> 5% in US']}) |
| 125 | + ])) |
| 126 | + .pipe(cleanCSS({compatibility: 'ie8'})) |
| 127 | + .pipe(sourcemaps.write('.')) |
| 128 | + .pipe(gulp.dest('.')) |
| 129 | + .pipe(browserSync.stream()); |
| 130 | +}); |
| 131 | + |
| 132 | +/** |
| 133 | + * Minifies JS files. |
| 134 | + * |
| 135 | + * CMD: gulp build-js |
| 136 | + */ |
| 137 | +gulp.task('build-js', function(){ |
| 138 | + gulp.src( [ JS_SRC, JS_EXCLD ] ) |
| 139 | + .pipe(minify({ |
| 140 | + ext:{ |
| 141 | + src:'.js', |
| 142 | + min:'.min.js' |
| 143 | + }, |
| 144 | + noSource: true |
| 145 | + })) |
| 146 | + .pipe(gulp.dest( JS_DEST )); |
| 147 | +}); |
| 148 | + |
| 149 | +gulp.task('build-img', function(){ |
| 150 | + gulp.src('assets/images/*') |
| 151 | + .pipe(imagemin()) |
| 152 | + .pipe(gulp.dest('assets/images')); |
| 153 | +}); |
| 154 | + |
| 155 | +/** |
| 156 | + * Executes all of the build tasks in the correct sequence. |
| 157 | + * |
| 158 | + * CMD: gulp build |
| 159 | + */ |
| 160 | +gulp.task('build', ['build-sass','build-js', 'build-img']); |
| 161 | + |
| 162 | +/** |
| 163 | + * Creates a zip file of the current project without any of the config and dev |
| 164 | + * files and saves it under the 'dist' folder. |
| 165 | + * |
| 166 | + * CMD: gulp zip |
| 167 | + */ |
| 168 | +gulp.task('zip', function(){ |
| 169 | + return gulp.src( ZIP_SRC_ARR, ZIP_OPTS ) |
| 170 | + .pipe( zip( BASE_NAME + '.zip' ) ) |
| 171 | + .pipe( gulp.dest('dist') ); |
| 172 | +}); |
| 173 | + |
| 174 | +/** |
| 175 | + * Initializes dev dependencies. |
| 176 | + */ |
| 177 | +gulp.task('init',['wp-enforcer'], function(){ |
| 178 | + return request('https://gist.githubusercontent.com/sfgarza/32258b7332a715de4e3948892ba415d3/raw/8a499cfe32749f4cabe2f6fe0d95653ce98e14e2/pre-commit-gulp.bash').pipe(fs.createWriteStream('.git/hooks/pre-commit')); |
| 179 | +}); |
| 180 | + |
| 181 | + |
| 182 | +/** |
| 183 | + * Runs composer install. |
| 184 | + */ |
| 185 | +gulp.task('composer', function(cb) { |
| 186 | + //Install composer packages |
| 187 | + return shell_exec('composer install', cb ); |
| 188 | +}); |
| 189 | + |
| 190 | +/** |
| 191 | + * Runs composer update |
| 192 | + */ |
| 193 | +gulp.task('composer-update', function(cb) { |
| 194 | + //Install composer packages |
| 195 | + return shell_exec('composer update', cb ); |
| 196 | +}); |
| 197 | + |
| 198 | +/** |
| 199 | + * Installs wp-enforcer |
| 200 | + */ |
| 201 | +gulp.task('wp-enforcer', ['composer'], function(cb){ |
| 202 | + return shell_exec('./vendor/bin/wp-enforcer', cb ); |
| 203 | +}); |
| 204 | + |
| 205 | + |
| 206 | +/** |
| 207 | + * Execute Shell script within node. |
| 208 | + * |
| 209 | + * Not currently being used. |
| 210 | + * @param {String} command : Command to execute. |
| 211 | + * @param {Function} callback : Callback function. |
| 212 | + * @return {Function} : Callback function. |
| 213 | + */ |
| 214 | +function shell_exec( command, callback ){ |
| 215 | + // Execute bash script. |
| 216 | + // command = path.join( __dirname , '/scripts/gulpconf.sh'); |
| 217 | + shell = spawn_shell(command, { shell: '/bin/bash', env: environment }); |
| 218 | + shell.on('exit', function(data){ |
| 219 | + return callback(); |
| 220 | + }); |
| 221 | +} |
0 commit comments