-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
331 lines (274 loc) · 10.4 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const plugin = 'customify',
source_SCSS = 'scss/*.scss',
dest_CSS = './css/',
gulp = require('gulp'),
fs = require('fs'),
plugins = require('gulp-load-plugins')(),
del = require('del'),
cp = require('child_process'),
cleanCSS = require('gulp-clean-css'),
commandExistsSync = require('command-exists').sync
require('es6-promise').polyfill();
const scriptsSrc = ['./js/customizer/*.js','./js/*.js','!./js/**/*.min.js'];
// -----------------------------------------------------------------------------
// This is a helper function used to update the Google fonts list and recreate the php file holding it (includes/resources/google.fonts.php).
// -----------------------------------------------------------------------------
function updatePhpGoogleFontsList(done) {
const endpoint = 'https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyAn2JiVvY0QL1T1430udIHS-nB3vBnjrf4';
require('request').get({
url: endpoint,
}, function(error, response, body) {
if (error || !body) throw error
body = JSON.parse(body);
//require('fs').writeFileSync('post.json', JSON.stringify(body))
// First lets inspect the body and make sure it is something we can manage.
if ( typeof body.items === 'undefined' ) {
log.error( 'There was no `items` entry in the response from Google apis. Please manually check the response from `'+endpoint+'`');
return done();
}
let fontsList = {};
// Go through the items and clean it up to our liking.
body.items.map( function(item){
delete item.kind;
delete item.version;
delete item.lastModified;
delete item.files;
fontsList[item.family] = item;
})
// Start the PHP code list.
let php = ['<?php'];
php.push( '// Returns an associative array with fonts.' );
php.push( 'return json_decode( \'' + JSON.stringify( fontsList ) + '\', true );' );
require('fs').writeFileSync('includes/resources/google.fonts.php', php.join( '\r\n' ));
return done();
})
}
updatePhpGoogleFontsList.description = 'Fetch and recreate the PHP file holding the Google Fonts list.';
gulp.task( 'update-php-google-fonts-list', updatePhpGoogleFontsList );
// -----------------------------------------------------------------------------
// Styles related
// -----------------------------------------------------------------------------
function logError( err, res ) {
log.error( 'Sass failed to compile' );
log.error( '> ' + err.file.split( '/' )[err.file.split( '/' ).length - 1] + ' ' + 'line ' + err.line + ': ' + err.message );
}
var log = require('fancy-log');
function stylesDev () {
return gulp.src(source_SCSS)
.pipe(plugins.sass({'sourcemap': true, outputStyle: 'expanded'})).on('error', logError)
.pipe(plugins.autoprefixer())
.pipe(plugins.replace(/^@charset \"UTF-8\";\n/gm, ''))
.pipe(gulp.dest(dest_CSS))
.pipe(plugins.rtlcss())
.pipe(plugins.rename(function (path) {
path.basename += '-rtl';
}))
.pipe(gulp.dest(dest_CSS));
}
gulp.task('styles-dev', stylesDev);
function stylesProd () {
return gulp.src(source_SCSS)
.pipe(plugins.sass({'sourcemap': false, outputStyle: 'compressed'}))
.pipe(plugins.autoprefixer())
.pipe(plugins.replace(/^@charset \"UTF-8\";\n/gm, ''))
.pipe(cleanCSS())
.pipe(gulp.dest(dest_CSS))
.pipe(plugins.rtlcss())
.pipe(cleanCSS())
.pipe(plugins.rename(function (path) {
path.basename += '-rtl';
}))
.pipe(gulp.dest(dest_CSS));
}
gulp.task('styles-prod', stylesProd)
function stylesSequence(cb) {
return gulp.series( 'styles-prod' )(cb);
}
stylesSequence.description = 'Compile styles';
gulp.task( 'styles', stylesSequence );
// Create minified versions of scripts.
function minifyScripts() {
return gulp.src(scriptsSrc,{base: './js/'})
.pipe( plugins.terser({
warnings: true,
compress: true, mangle: true,
output: { comments: 'some' }
}))
.pipe(plugins.rename({
suffix: ".min"
}))
.pipe(gulp.dest('./js'));
}
gulp.task( 'minify-scripts', minifyScripts );
function scriptsCompileSequence(cb) {
gulp.series( 'minify-scripts' )(cb);
}
gulp.task( 'scripts', scriptsCompileSequence );
gulp.task('styles-watch', function () {
return gulp.watch('scss/**/*.scss', stylesDev);
});
gulp.task('scripts-watch', function () {
return gulp.watch( scriptsSrc, scriptsCompileSequence );
});
gulp.task('watch', function(cb) {
return gulp.parallel('styles-watch', 'scripts-watch')(cb);
});
// usually there is a default task for lazy people who just wanna type gulp
gulp.task('start', function (cb) {
return gulp.series( 'styles', 'scripts' )(cb);
});
// ============
// TASKS FOR CREATING THE PLUGIN ZIP FILE
// ============
// -----------------------------------------------------------------------------
// Copy plugin folder outside in a build folder
// -----------------------------------------------------------------------------
function copyFolder() {
var dir = process.cwd();
return gulp.src( './*' )
.pipe( plugins.exec( 'rm -Rf ./../build; mkdir -p ./../build/'+plugin+';', {
silent: true,
continueOnError: true // default: false
} ) )
.pipe(plugins.rsync({
root: dir,
destination: '../build/'+plugin+'/',
progress: false,
silent: true,
compress: false,
recursive: true,
emptyDirectories: true,
clean: true,
exclude: ['node_modules']
}));
}
copyFolder.description = 'Copy plugin production files to a build folder';
gulp.task( 'copy-folder', copyFolder );
// -----------------------------------------------------------------------------
// Remove unneeded files and folders from the build folder
// -----------------------------------------------------------------------------
function removeUnneededFiles() {
// files that should not be present in build zip
files_to_remove = [
'**/codekit-config.json',
'node_modules',
'node-tasks',
'config.rb',
'gulpfile.js',
'package.json',
'package-lock.json',
'pxg.json',
'build',
'.idea',
'**/*.css.map',
'**/.git*',
'*.sublime-project',
'.DS_Store',
'**/.DS_Store',
'__MACOSX',
'**/__MACOSX',
'+development.rb',
'+production.rb',
'README.md',
'.labels',
'.csscomb',
'.csscomb.json',
'.codeclimate.yml',
'tests',
'circle.yml',
'.circleci',
'.labels',
'.jscsrc',
'.jshintignore',
'browserslist',
'palettes.md',
'scss',
];
files_to_remove.forEach( function( e, k ) {
files_to_remove[k] = '../build/'+plugin+'/' + e;
} );
return del( files_to_remove, {force: true} );
}
removeUnneededFiles.description = 'Remove unneeded files and folders from the build folder';
gulp.task( 'remove-unneeded-files', removeUnneededFiles );
function maybeFixBuildDirPermissions(done) {
cp.execSync('find ./../build -type d -exec chmod 755 {} \\;');
return done();
}
maybeFixBuildDirPermissions.description = 'Make sure that all directories in the build directory have 755 permissions.';
gulp.task( 'fix-build-dir-permissions', maybeFixBuildDirPermissions );
function maybeFixBuildFilePermissions(done) {
cp.execSync('find ./../build -type f -exec chmod 644 {} \\;');
return done();
}
maybeFixBuildFilePermissions.description = 'Make sure that all files in the build directory have 644 permissions.';
gulp.task( 'fix-build-file-permissions', maybeFixBuildFilePermissions );
function maybeFixIncorrectLineEndings(done) {
if (!commandExistsSync('dos2unix')) {
log( c.red( 'Could not ensure that line endings are correct on the build files since you are missing the "dos2unix" utility! You should install it.' ) );
log( c.red( 'However, this is not a very big deal. The build task will continue.' ) );
} else {
cp.execSync('find ./../build -type f -print0 | xargs -0 -n 1 -P 4 dos2unix');
}
return done();
}
maybeFixIncorrectLineEndings.description = 'Make sure that all line endings in the files in the build directory are UNIX line endings.';
gulp.task( 'fix-line-endings', maybeFixIncorrectLineEndings );
function buildSequence(cb) {
return gulp.series( 'styles', 'scripts', 'copy-folder', 'remove-unneeded-files', 'fix-build-dir-permissions', 'fix-build-file-permissions', 'fix-line-endings' )(cb);
}
buildSequence.description = 'Sets up the build folder';
gulp.task( 'build', buildSequence );
// -----------------------------------------------------------------------------
// Create the plugin installer archive and delete the build folder
// -----------------------------------------------------------------------------
function makeZip() {
var versionString = '';
// get plugin version from the main plugin file
var contents = fs.readFileSync("./" + plugin + ".php", "utf8");
// split it by lines
var lines = contents.split(/[\r\n]/);
function checkIfVersionLine(value, index, ar) {
var myRegEx = /^[\s\*]*[Vv]ersion:/;
if (myRegEx.test(value)) {
return true;
}
return false;
}
// apply the filter
var versionLine = lines.filter(checkIfVersionLine);
versionString = versionLine[0].replace(/^[\s\*]*[Vv]ersion:/, '').trim();
versionString = '-' + versionString.replace(/\./g, '-');
return gulp.src('./')
.pipe(plugins.exec('cd ./../; rm -rf ' + plugin[0].toUpperCase() + plugin.slice(1) + '*.zip; cd ./build/; zip -r -X ./../' + plugin[0].toUpperCase() + plugin.slice(1) + versionString + '.zip ./; cd ./../; rm -rf build'));
}
makeZip.description = 'Create the plugin installer archive and delete the build folder';
gulp.task( 'make-zip', makeZip );
function zipSequence(cb) {
return gulp.series( 'build', 'make-zip' )(cb);
}
zipSequence.description = 'Creates the zip file';
gulp.task( 'zip', zipSequence );
/**
* Short commands help
*/
gulp.task('help', function () {
var $help = '\nCommands available : \n \n' +
'=== General Commands === \n' +
'start (default)Compiles all styles and scripts and makes the theme ready to start \n' +
'zip Generate the zip archive \n' +
'build Generate the build directory with the cleaned theme \n' +
'help Print all commands \n' +
'=== Style === \n' +
'styles Compiles styles in production mode\n' +
'styles-dev Compiles styles in development mode \n' +
'styles-admin Compiles admin styles \n' +
'=== Scripts === \n' +
'scripts Concatenate all js scripts \n' +
'scripts-dev Concatenate all js scripts \n' +
'=== Watchers === \n' +
'watch Watches all js and scss files \n' +
'styles-watch Watch only styles\n' +
'scripts-watch Watch scripts only \n';
console.log($help);
});