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