-
Notifications
You must be signed in to change notification settings - Fork 12
/
gulpfile.babel.js
308 lines (273 loc) · 6.05 KB
/
gulpfile.babel.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict';
let gulp = require('gulp');
let plugins = require('gulp-load-plugins');
let yargs = require('yargs');
let rimraf = require('rimraf');
let yaml = require('js-yaml');
let fs = require('fs');
let named = require('vinyl-named');
let through2 = require('through2');
let sass = require('gulp-sass')(require('node-sass'));
// Load all Gulp plugins into one variable
const $ = plugins();
let PRODUCTION = !!(yargs.argv.production); // Check for --production flag
let VERSION_BUMP = yargs.argv.release; // Check for --release (x.x.x semver version number)
// Load settings from settings.yml
const {COMPATIBILITY, PORT, UNCSS_OPTIONS, PATHS, LOCAL_PATH} = loadConfig();
let sassConfig = {
mode: (PRODUCTION ? true : false)
};
/**
* Load in additional config files
*/
function loadConfig() {
let ymlFile = fs.readFileSync('config.yml', 'utf8');
return yaml.load(ymlFile);
}
/**
* Set production mode during the build process
*
* @param done
*/
function setProductionMode(done) {
PRODUCTION = false;
sassConfig.production = true;
done();
}
/**
* Build the "dist" folder by running all of the below tasks
* Sass must be run later so UnCSS can search for used classes in the others assets.
* */
gulp.task(
'build:release',
gulp.series(
setProductionMode,
clean,
javascript,
buildSass,
bumpPluginFile,
bumpPackageJson,
bumpReadmeStableTag,
bumpComposerJson,
copy
)
);
/**
* Build the "dist" folder by running all of the below tasks
* Sass must be run later so UnCSS can search for used classes in the others assets.
* */
gulp.task(
'build',
gulp.series(
setProductionMode,
clean,
javascript,
buildSass,
copy
)
);
// Build the site, run the server, and watch for file changes
gulp.task(
'default',
gulp.series(
clean,
javascript,
buildSass,
copy,
gulp.parallel(watch)
)
);
/**
* This happens every time a build starts
*
* @since 1.4.0
*
* @param done
*/
function clean(done) {
rimraf('css', done);
rimraf('js', done);
done();
}
/**
* Bump the version number within the define method of our plugin file
* PHP Constant: example `define( 'MESH_VERSION', '1.0.0' );`
*
* Bump the version number within our meta data of the plugin file
*
* Update the release date with today's date
*
* @since 1.4.0
*
* @return {*}
*/
function bumpPluginFile(done) {
let constant = 'MESH_VERSION';
let define_bump_obj = {
key: constant,
regex: new RegExp('([<|\'|"]?(' + constant + ')[>|\'|"]?[ ]*[:=,]?[ ]*[\'|"]?[a-z]?)(\\d+.\\d+.\\d+)(-[0-9A-Za-z.-]+)?(\\+[0-9A-Za-z\\.-]+)?([\'|"|<]?)', 'ig')
};
let bump_obj = {
key: 'Version',
};
if (VERSION_BUMP) {
bump_obj.version = VERSION_BUMP;
define_bump_obj.version = VERSION_BUMP;
}
let today = getReleaseDate();
return gulp.src('./mesh.php')
.pipe($.bump(bump_obj))
.pipe($.bump(define_bump_obj))
.pipe($.replace(/(((0)[0-9])|((1)[0-2]))(\/)([0-2][0-9]|(3)[0-1])(\/)\d{4}/ig, today))
.pipe(through2.obj(function (file, enc, cb) {
let date = new Date();
file.stat.atime = date;
file.stat.mtime = date;
cb(null, file);
}))
.pipe(gulp.dest('./'));
}
/**
* Update the what's new template with the date of the release instead of having to manually update it every release
*
* @since 1.4.0
*
* @return {*}
*/
function getReleaseDate() {
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0');
let yyyy = today.getFullYear();
return mm + '/' + dd + '/' + yyyy;
}
/**
* Bump the composer.json
*
* @since 1.4.0
*
* @return {*}
*/
function bumpComposerJson() {
let bump_obj = {
key: 'version'
};
if (VERSION_BUMP) {
bump_obj.version = VERSION_BUMP;
}
return gulp.src('./composer.json')
.pipe($.bump(bump_obj))
.pipe(through2.obj(function (file, enc, cb) {
let date = new Date();
file.stat.atime = date;
file.stat.mtime = date;
cb(null, file);
}))
.pipe(gulp.dest('.'));
}
/**
* bump readme file stable tag to our latest version
*
* @since 1.4.0
*
* @return {*}
*/
function bumpReadmeStableTag() {
let bump_obj = {key: "Stable tag"};
if (VERSION_BUMP) {
bump_obj.version = VERSION_BUMP;
}
return gulp.src('./readme.txt')
.pipe($.bump(bump_obj))
.pipe(through2.obj(function (file, enc, cb) {
let date = new Date();
file.stat.atime = date;
file.stat.mtime = date;
cb(null, file);
}))
.pipe(gulp.dest('./'));
}
/**
* Bump the package.json
*
* @since 1.4.0
*
* @return {*}
*/
function bumpPackageJson() {
let bump_obj = {
key: 'version'
};
if (VERSION_BUMP) {
bump_obj.version = VERSION_BUMP;
}
return gulp.src('./package.json')
.pipe($.bump(bump_obj))
.pipe(through2.obj(function (file, enc, cb) {
let date = new Date();
file.stat.atime = date;
file.stat.mtime = date;
cb(null, file);
}))
.pipe(gulp.dest('.'));
}
/**
* Copy files out of the assets folder
* This task skips over the "img", "js", and "scss" folders, which are parsed separately
*
* @since 1.4.0
*
* @return {*}
*/
function copy() {
return gulp.src(PATHS.assets)
.pipe(gulp.dest('css/fonts'));
}
/**
* In production, the CSS is compressed
*
* @since 1.4.0
*
* @return {*}
*/
function buildSass() {
return gulp.src('assets/scss/*.scss')
.pipe($.sourcemaps.init())
.pipe(sass({
includePaths: PATHS.sass
}).on('error', sass.logError))
.pipe( gulp.dest('css') );
}
/**
* In production, the file is minified
*
* @since 1.4.0
*
* @return {*}
*/
function javascript() {
return gulp.src(PATHS.entries)
.pipe(named())
.pipe($.sourcemaps.init())
.pipe($.if(PRODUCTION, $.uglify()
.on('error', e => {
console.log(e);
})
))
.pipe($.if(!PRODUCTION, $.sourcemaps.write()))
.pipe(gulp.dest('js'));
}
/**
* Watch for changes to static assets
* Sass
* JavaScript
* readme.txt
*
* @since 1.4.0
*/
function watch() {
gulp.watch('readme.txt', readme);
gulp.watch('assets/scss/**/*.scss').on('all', buildSass);
gulp.watch('assets/js/**/*.js').on('all', gulp.series(javascript));
gulp.watch(PATHS.assets, copy);
}