-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
203 lines (193 loc) · 4.57 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
/**
* Gulpfile
*
* Use Gulp to build a WordPress block.
*
* Implements:
* - BrowserSync (with watch task)
* - Pot file generation
* - Zip folder without development files
* - Copy `package.json` version to WP files
* - A one-time task to init your block
*
* @author Armand Philippot <contact@armandphilippot.com>
*/
/**
* Load Gulp configuration and error handler.
*/
const config = require('./gulp/config');
/**
* Load Gulp plugins.
*/
const { src, dest, parallel } = require('gulp');
/* Utilities */
const del = require('del');
const fs = require('fs');
const notify = require('gulp-notify');
const { pipeline } = require('stream');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const vinylPaths = require('vinyl-paths');
const zip = require('gulp-zip');
/**
* Load package.json data.
*/
const package = JSON.parse(fs.readFileSync('./package.json'));
const packageVersion = package.version;
/**
* Task: `zipBlock`
*
* Generate a zip version of the plugin without development files.
*/
function zipBlock(done) {
return pipeline(
[
src(config.zip.src),
zip(config.zip.filename),
dest(config.zip.dest),
notify({
title: 'zipBlock task complete',
message: 'Block have been zipped.',
}),
],
done()
);
}
/**
* Task: `bumpPHP`
*
* Copy package.json version in main plugin file.
*/
function bumpPHP(done) {
return pipeline(
[
src(config.bump.php.src),
replace(/Version: (.{5})/g, function () {
return 'Version: ' + packageVersion;
}),
replace(/_VERSION', '(.{5})'/g, function () {
return "_VERSION', '" + packageVersion + "'";
}),
dest(config.bump.php.dest),
],
done()
);
}
/**
* Task: `bumpTXT`
*
* Copy package.json version in readme.txt.
*/
function bumpTXT(done) {
return pipeline(
[
src(config.bump.txt.src),
replace(/Stable tag: (.{5})/g, function () {
return 'Stable tag: ' + packageVersion;
}),
dest(config.bump.txt.dest),
],
done()
);
}
/**
* Task: `renameMainFile`
*
* Init the block folder by renaming the main PHP file.
*/
function renameMainFile(done) {
return pipeline(
[
src(config.init.file.main, { base: './' }),
vinylPaths(del),
rename({
basename: config.init.packageName.toLowerCase(),
}),
dest(config.init.dest),
notify({
title: 'renameMainFile task complete',
message: 'Main PHP file has been renamed.',
onLast: config.notify.onLastOption,
}),
],
done()
);
}
/**
* Task: `replaceBlockInfo`
*
* Init the block folder by replacing some placeholders.
*/
function replaceBlockInfo(done) {
return pipeline(
[
src(config.init.src, { base: './' }),
replace('Firstname Lastname', function () {
return config.init.authorName;
}),
replace('your@email.com', function () {
return config.init.authorEmail;
}),
replace('https://www.yourWebsite.com/', function () {
return config.init.authorUrl;
}),
replace('(WordPress.org usernames)', function () {
return config.init.contributors;
}),
replace('2020 Company Name', function () {
return config.init.copyright;
}),
replace('Your block name', function () {
return config.init.name;
}),
replace('Your block description.', function () {
return config.init.description;
}),
replace('Your-Package-Name', function () {
return config.init.packageName;
}),
replace('Your_Package_Name', function () {
return config.init.packageName.replace(/-/g, '_');
}),
replace('your-package-name', function () {
return config.init.packageName.toLowerCase();
}),
replace('your_package_name', function () {
return config.init.packageName.toLowerCase().replace(/-/g, '_');
}),
replace('YourPrefix', function () {
return config.init.prefix;
}),
replace('yourprefix', function () {
return config.init.prefix.toLowerCase();
}),
replace('YOURPREFIX', function () {
return config.init.prefix.toUpperCase();
}),
replace('https://github.com/your/repo', function () {
return config.init.repo;
}),
replace('yourTextDomain', function () {
return config.init.textDomain;
}),
replace('your-vendor-name', function () {
return config.init.vendorName;
}),
dest(config.init.dest),
notify({
title: 'replaceBlockInfo task complete',
message:
'The placeholders have been replaced by your dotenv data.',
onLast: config.notify.onLastOption,
}),
],
done()
);
}
/**
* Public tasks
*/
exports.bump = parallel(bumpPHP, bumpTXT);
exports.initBlock = replaceBlockInfo;
exports.renameFiles = renameMainFile;
exports.zipBlock = zipBlock;