-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
319 lines (277 loc) · 8.24 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
const fs = require('fs');
const path = require('path');
const del = require('del');
const log = require('fancy-log');
const gulp = require('gulp');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const handlebars = require('gulp-hb');
const merge = require('merge-stream');
const lazypipe = require('lazypipe');
const composer = require('gulp-composer');
// Load .env file if present
require('dotenv').config();
/**
* Parse the "package.json" file instead of using `require` because `require` caches
* multiple calls (so the version number won't be updated).
* @returns {number | string | boolean}
*/
const getPackageJSON = () => JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
/**
* Retrieve data from a YAML file.
* @param {string} path
* @returns {object}
*/
const getYaml = (path) => require('yaml-js').load(fs.readFileSync(path));
/**
* Get the changelog data
* @returns {object}
*/
const getChangelogInfo = () => getYaml('./.ci/data/changelog.yml');
/**
* Get the plugin data
* @returns {object}
*/
const getPluginInfo = () => getYaml('./.ci/data/plugin-info.yml');
/**
* Remove build artifacts.
*/
function clean(cb) {
log('Cleaning up build artifacts...');
del.sync(['./build/**', './dist/**']);
cb();
}
/**
* Create the CHANGELOG.md
*/
function changelog() {
const changelog = getChangelogInfo();
const plugin = getPluginInfo();
const hb = handlebars({ handlebars: require('handlebars') }).data({ changelog, plugin });
log('Creating “CHANGELOG.md”.');
return gulp.src('./.ci/templates/changelog.hbs')
.pipe(hb)
.pipe(rename('CHANGELOG.md'))
.pipe(gulp.dest('./', { overwrite: true }));
}
/**
* Create the manifest.json
*/
function manifest() {
const json_editor = require('gulp-json-editor');
const { version } = getPackageJSON();
const changelog = getChangelogInfo();
const plugin = getPluginInfo();
const releases = changelog.releases
.filter(release => !release.prerelease) // No pre-releases!
.filter((release, index) => index < 3); // Get the last 3
const hb = handlebars({ handlebars: require('handlebars') })
.helpers({
'markdown': require('helper-markdown')({ html: true, typographer: true }),
'format': (str) => {
if (typeof str !== 'string') return '';
return str.replace(/"/g, "'").replace(/(<\/?p>|\n)/g, '');
},
'iso_date': (str) => {
if (typeof str !== 'string') return '';
return new Date(`${str} GMT-0500`).toISOString();
},
'nice_date': (str) => {
if (typeof str !== 'string') return '';
const date = new Date(`${str} GMT-0500`);
const options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Intl.DateTimeFormat('en-US', options).format(date);
},
'first': (arr, n) => {
if (!Array.isArray(arr)) return '';
if (typeof n !== 'number') return arr[0];
return arr[n];
},
})
.data({ changelog, plugin, releases, version });
log('Creating “manifest.json”...');
return gulp.src('./.ci/templates/manifest.hbs')
.pipe(hb)
.pipe(json_editor(json => json))
.pipe(rename('manifest.json'))
.pipe(gulp.dest('./dist', { overwrite: true }));
}
/**
* Create the plugin's readme.txt
*/
function readme() {
const changelog = getChangelogInfo();
const plugin = getPluginInfo();
const hb = handlebars({ handlebars: require('handlebars') })
.helpers({
'count': (index) => parseInt(index) + 1,
})
.data({ changelog, plugin });
log('Creating “readme.txt”.');
return gulp.src('./.ci/templates/readme.hbs')
.pipe(hb)
.pipe(rename('readme.txt'))
.pipe(gulp.dest('./build', { overwrite: true }));
}
/**
* Create release notes from changelog data
*/
function releaseNotes() {
const { version } = getPackageJSON();
const changelog = getChangelogInfo();
// Get a the associated release changes from the changelog data.
const release = changelog.releases.find(({ tag_name = '' }) => tag_name === `v${version}`);
if (!release) {
throw Error(`ERROR: "v${version}" is not a valid release in the changelog!`);
}
const hb = handlebars({ handlebars: require('handlebars') }).data(release);
log('Creating “release_notes.txt”...');
return gulp.src('./.ci/templates/release.hbs')
.pipe(hb)
.pipe(rename('release_notes.txt'))
.pipe(gulp.dest('./dist', { overwrite: true }));
}
/**
* Compile plugin assets and create a ZIP
*/
function build() {
const { version } = getPackageJSON();
const {
author,
author_uri,
description,
download_uri,
name,
requires,
requires_php,
short_description,
slug,
text_domain,
uri,
} = getPluginInfo();
const replacePlaceholders = lazypipe()
.pipe(replace, /\{\{VERSION\}\}/g, version)
.pipe(replace, /\{\{NAME\}\}/g, name)
.pipe(replace, /\{\{SLUG\}\}/g, slug)
.pipe(replace, /\{\{TEXT_DOMAIN\}\}/g, text_domain)
.pipe(replace, /\{\{DESCRIPTION\}\}/g, description)
.pipe(replace, /\{\{SHORT_DESCRIPTION\}\}/g, short_description)
.pipe(replace, /\{\{REQUIRES\}\}/g, requires)
.pipe(replace, /\{\{REQUIRES_PHP\}\}/g, requires_php)
.pipe(replace, /\{\{URI\}\}/g, uri)
.pipe(replace, /\{\{AUTHOR\}\}/g, author)
.pipe(replace, /\{\{AUTHOR_URI\}\}/g, author_uri)
.pipe(replace, /\{\{DOWNLOAD_URI\}\}/g, download_uri);
log('Compiling plugin PHP files...');
return merge(
// Root files (plugin base files)
gulp.src('./*.php')
.pipe(replacePlaceholders())
.pipe(gulp.dest('./build', { overwrite: true })),
// Include files (classes)
gulp.src('./includes/**/*')
.pipe(replacePlaceholders())
.pipe(gulp.dest('./build/includes', { overwrite: true }))
);
}
/**
* Copy files into the dist directory.
*/
function copy() {
log('Copying over assets and vendor PHP files...');
return merge(
gulp.src('./vendor/**/*')
.pipe(gulp.dest('./build/vendor', { overwrite: true })),
gulp.src('./lib/**/*')
.pipe(gulp.dest('./build/lib', { overwrite: true })),
);
}
/**
* Create the ZIP file for distribution.
*/
function zip() {
const { version } = getPackageJSON();
const { slug } = getPluginInfo();
log('Creating ZIP file for distribution...');
return gulp.src('./build/**/*')
.pipe(rename((file) => {
file.dirname = path.join(slug, file.dirname);
}))
.pipe(require('gulp-zip')(`${slug}-v${version}.zip`, { modifiedTime: new Date() }))
.pipe(gulp.dest('./dist', { overwrite: true }));
}
/**
* Run tests
*/
function test(cb) {
const { execSync } = require('child_process');
const { version } = getPackageJSON();
// Make sure the tag name matched the version in "package.json"
if (process.env.TAG_NAME && process.env.TAG_NAME !== `v${version}`) {
return cb(new Error('The version in “package.json” does not match the tag name!'));
}
// Check `git status` for any changes. If found, recommend running a build and committing files.
// See: https://unix.stackexchange.com/a/155077
let stdout = execSync('git status --porcelain', { encoding: 'utf-8' });
// If `stdout` contains anything, it means there are uncommitted changes.
if (stdout) {
return cb(new Error('Changes detected! Make sure to run a build and commit any changed files before deploying!'));
}
return cb();
}
function composerUpdate(cb) {
composer( 'update' );
return cb();
}
/**
* Watch files and build on change.
*/
function watch() {
gulp.watch(['./includes/**/*', './*.php'], gulp.series(composerUpdate, build));
gulp.watch(['./lib/**/*', './vendor/**/*'], copy);
}
/**
*
* @param dir
* @returns {string[]}
*/
function getZips(dir) {
return fs.readdirSync(dir)
.filter(function (file) {
return file.endsWith(".zip");
});
}
/**
* Unzip included plugins to an ignored repo for local dev
*/
function installPlugins(cb) {
log('Installing plugins for local dev...');
const dir = './.ci/plugins/';
if (!fs.existsSync(dir)) {
log('Nothing to install!');
return cb();
}
const zips = getZips(dir);
if (zips.length < 1) {
log('Nothing to install!');
return cb();
}
return zips.map(function( zip ) {
return gulp.src(`${dir}${zip}`)
.pipe(require('gulp-unzip')({ keepEmpty: true }))
.pipe(gulp.dest('./.plugins/', { overwrite: true }))
.on('end', function() {
cb();
});
});
}
exports.build = gulp.series(
clean,
gulp.parallel(build, copy),
gulp.parallel(changelog, releaseNotes, manifest, readme),
);
exports.clean = clean;
exports.test = test;
exports.watch = watch;
exports.zip = zip;
exports.install = installPlugins;