-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgulpfile.js
200 lines (168 loc) · 5.45 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
'use strict';
/**
* Define configurations
*/
const info = require( './package.json' );
const config = {
init: './' + info.name + '.php',
src: {
scss: [ './assets/scss/**/*.scss' ],
css: [ './assets/css/**/*.css', '!./assets/css/vendors/*' ],
js: [ './assets/js/**/*.js', '!./assets/js/vendors/*' ],
pot: [ './**/*.php', '!./__build/**/*.php', '!./__bak/**/*.php' ],
build: [
'./*',
'./assets/css/**/*',
'./assets/icons/**/*',
'./assets/images/**/*',
'./assets/js/**/*',
'./inc/**/*',
'./languages/**/*',
'./page-templates/**/*',
'./template-parts/**/*',
// exclude files and folders
'!**/Thumbs.db',
'!**/.DS_Store',
'!./.gitignore',
'!./package*.json',
'!./gulpfile.js',
'!./node_modules',
'!./README.md',
'!./LICENSE.md',
'!./__build',
'!./__bak',
],
},
dest: {
scss: './assets/scss',
css: './assets/css',
js: './assets/js',
icons: './assets/icons',
pot: './languages',
build: './__build/' + info.name,
zip: './__build/zip',
},
};
/**
* Init Gulp and plugins
*/
const gulp = require( 'gulp' );
// Translation
const wpPot = require( 'gulp-wp-pot' );
// Others
const fs = require( 'fs' );
const del = require( 'del' );
const replace = require( 'gulp-replace' );
const watch = require( 'gulp-watch' );
const zip = require( 'gulp-zip' );
/**
* Task: Change plugin / theme info based on package.json values.
*/
gulp.task( 'main_info', function() {
var info = JSON.parse( fs.readFileSync( './package.json' ) );
// Change plugin / theme info
return gulp.src( [ config.init ] )
.pipe( replace( new RegExp( '((?:Plugin|Theme) Name: ).*' ), '$1' + info.title ) )
.pipe( replace( new RegExp( '((?:Plugin|Theme) URI: ).*' ), '$1' + info.uri ) )
.pipe( replace( new RegExp( '(Description: ).*' ), '$1' + info.description ) )
.pipe( replace( new RegExp( '(Version: ).*' ), '$1' + info.version ) )
.pipe( replace( new RegExp( '(Author: ).*' ), '$1' + info.author.name ) )
.pipe( replace( new RegExp( '(Author URI: ).*' ), '$1' + info.author.url ) )
.pipe( replace( new RegExp( '(Text Domain: ).*' ), '$1' + info.name ) )
.pipe( replace( new RegExp( '(Tags: ).*' ), '$1' + info.keywords.join( ', ' ) ) )
.pipe( replace( new RegExp( '(\'' + info.name.toUpperCase().split( '-' ).join( '_' ) + '_VERSION\', \').*?(\'.*)' ), '$1' + info.version + '$2' ) )
.pipe( gulp.dest( './' ) );
} );
/**
* Task: Change info on readme.txt based on package.json values.
*/
gulp.task( 'readme_txt', function() {
var info = JSON.parse( fs.readFileSync( './package.json' ) );
var contributors = info.contributors.map(function( contributor ) {
return contributor.name;
});
// Change plugin / theme version on readme.txt
return gulp.src( [ './readme.txt' ] )
.pipe( replace( new RegExp( '(===).*(===)' ), '$1 ' + info.title + ' $2' ) )
.pipe( replace( new RegExp( '(Contributors: ).*' ), '$1' + contributors.join( ', ' ) ) )
.pipe( replace( new RegExp( '(Tags: ).*' ), '$1' + info.keywords.join( ', ' ) ) )
.pipe( replace( new RegExp( '(Stable tag: ).*' ), '$1' + info.version ) )
.pipe( replace( new RegExp( '(\n\n).*(\n\n== Description ==)' ), '$1' + info.description + '$2' ) )
// .pipe( replace( new RegExp( '(== Description ==\n\n).*(\n\n)' ), '$1' + info.description + '$2' ) )
.pipe( gulp.dest( './' ) );
} );
/**
* Wrapper Task: Set plugin / theme info files.
*/
gulp.task( 'info', gulp.series( 'main_info', 'readme_txt' ) );
/**
* Task: Generate .pot file for translation.
*/
gulp.task( 'pot', function() {
var info = JSON.parse( fs.readFileSync( './package.json' ) );
return gulp.src( config.src.pot.concat( [ config.init ] ) )
.pipe( wpPot( {
domain: info.name,
package: info.title,
metadataFile: config.init,
} ).on( 'error', function( error ) {
console.error( error );
this.emit( 'end' );
} ) )
.pipe( gulp.dest( config.dest.pot + '/' + info.name + '.pot' ) );
} );
/**
* Task: Watch all files and copy to 'build' folder.
*/
gulp.task( 'watch', function() {
watch( './package.json', function() {
gulp.task( 'info' )();
} );
watch( config.src.pot, function() {
gulp.task( 'pot' )();
} );
watch( config.src.scss, function() {
gulp.task( 'css' )();
} );
watch( config.src.js.concat( [ '!./assets/js/**/*.min.js' ] ), function( obj ) {
gulp.task( 'js' )();
} );
watch( config.src.build, { base: './' }, function( obj ) {
if ( 'unlink' === obj.event ) {
del( config.dest.build + '/' + obj.relative, { force: true } );
} else {
gulp.src( obj.path, { base: './' } )
.pipe( gulp.dest( config.dest.build ) );
}
} );
} );
/**
* Task: Clean files in "__build" directory.
*/
gulp.task( 'clean', function() {
return del( config.dest.build + '/*', { force: true } );
} );
/**
* Task: Copy selected files from sources to "__build" directory.
*/
gulp.task( 'copy', function() {
return gulp.src( config.src.build, { base: './' } )
.pipe( gulp.dest( config.dest.build ) );
} );
/**
* Wrapper Task: Build.
*/
gulp.task( 'build', gulp.series( 'pot', 'info', 'clean', 'copy' ) );
/**
* Wrapper Task: Default task.
*/
gulp.task( 'default', gulp.series( 'build', 'watch' ) );
/**
* Wrapper Task: Build to zip.
*/
gulp.task( 'zip', function() {
var info = JSON.parse( fs.readFileSync( './package.json' ) );
return gulp.src( config.dest.build + '/**/*', { buffer: false, base: config.dest.build + '/../' } )
.pipe( zip( info.name + '-' + info.version + '.zip' ) )
.pipe( gulp.dest( config.dest.zip ) );
} );