-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgulpfile.js
317 lines (279 loc) · 6.94 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
const path = require('path')
const bs = require('browser-sync').create()
const gulp = require('gulp')
const version = require('standard-version')
const autoprefixer = require('gulp-autoprefixer')
const babel = require('gulp-babel')
const cleanCSS = require('gulp-clean-css')
const eslint = require('gulp-eslint')
const imagemin = require('gulp-imagemin')
const php = require('gulp-connect-php')
const phpcs = require('gulp-phpcs')
const rename = require('gulp-rename')
const sass = require('gulp-sass')
const sourcemaps = require('gulp-sourcemaps')
const stylelint = require('gulp-stylelint')
const uglify = require('gulp-uglify')
const when = require('gulp-if')
const wpPot = require('gulp-wp-pot')
const zip = require('gulp-zip')
require('dotenv').config()
const { configure, args, watch, isProduction } = require('./config/build-util')
const tasks = configure('packages', 'resources/releases', {
/**
* Lint PHP fiels and generate translation file.
*
* @param {TaskConfig} param0
* @return {ReadableStream}
*/
php ({ src, dest, config }) {
config.wpPot = {
domain: config.name,
package: `${config.name} v${config.version}`,
relativeTo: config.path,
lastTranslator: config.author,
team: config.author
}
config.phpcs.standard = 'packages/phpcs.xml'
return gulp.src(src)
.pipe(phpcs(config.phpcs))
.pipe(phpcs.reporter('log'))
.pipe(wpPot(config.wpPot))
.pipe(gulp.dest(dest))
},
/**
* Compile SCSS files to CSS & minify.
*
* @param {TaskConfig} param0
* @return {ReadableStream}
*/
css ({ src, dest, config }) {
return gulp.src(src, config.gulp)
.pipe(stylelint(config.stylelint))
.pipe(sass(config.sass).on('error', sass.logError))
.pipe(autoprefixer())
.pipe(gulp.dest(dest, config.gulp))
.pipe(when(isProduction, cleanCSS()))
.pipe(when(isProduction, rename(config.rename)))
.pipe(gulp.dest(dest))
.pipe(bs.stream())
},
/**
* Minify javascripts.
*
* @param {TaskConfig} param0
* @return {ReadableStream}
*/
js ({ src, dest, config }) {
return gulp.src(src, config.gulp)
.pipe(eslint(config.eslint))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.pipe(babel(config.babel))
.pipe(when(isProduction, uglify(config.uglify)))
.pipe(when(isProduction, rename(config.rename)))
.pipe(when(isProduction, gulp.dest(dest, config.gulp)))
.pipe(bs.stream())
},
/**
* Optimize images.
*
* @param {TaskConfig} param0
* @return {ReadableStream}
*/
img ({ src, dest, config }) {
return gulp.src(src)
.pipe(imagemin(config.imagemin))
.pipe(gulp.dest(dest))
},
/**
* Generate CHANGELOG.md for each then Packaging them.
*
* @param {TaskConfig} param0
* @return {ReadableStream}
*/
zip: async ({ src, dest, config }) => {
config.release.path = config.path
config.release.infile = `${config.path}/CHANGELOG.md`
config.release.releaseAs = config.version
config.release.scripts = {
postbump: `node config/build-util.js bump ${config.path}`
}
// Generate CHANGELOG.md file inside source directory
await version(config.release)
return gulp.src(src, config.gulp)
.pipe(zip(`${config.name}-${config.version}.zip`, config.zip))
.pipe(gulp.dest(dest))
}
})
/**
* Start php server.
*
* @async
* @returns {String}
*/
const phpServer = () => new Promise((resolve, reject) => {
const { argv } = args.options({
proxy: {
alias: 'p',
describe: 'Enable proxy',
default: process.env.WP_HOME,
type: 'string'
}
})
let url
try {
url = new URL(argv.proxy)
} catch (err) {
return reject(err)
}
// Don't start php dev server if the hostname isn't localhost
// In case you've already set your envvar to something like:
// WP_HOME = http://wpdev.local
if (url.hostname !== 'localhost') {
return resolve(url)
} else {
if (url.port === '') {
url.port = 8080
}
}
process.on('exit', () => {
php.closeServer()
})
php.server({
port: url.port,
hostname: url.hostname,
ini: 'public/.user.ini',
base: 'public',
router: './server.php',
configCallback (type, args) {
if (type === php.OPTIONS_PHP_CLI_ARR) {
return [
'-e',
'-d', 'cli_server.color=on'
].concat(args)
}
return args
}
}, () => {
resolve(url)
})
})
/**
* Start php server.
*
* @async
* @param {URL} url
* @returns {String}
*/
const bSync = (url) => new Promise((resolve, reject) => {
// if (process.env.NODE_ENV === 'testing') {
// return resolve(url)
// }
const { argv } = args.options({
open: {
alias: 'o',
describe: 'Open in browser',
default: false,
type: 'boolean'
},
notify: {
alias: 'n',
describe: 'Enable notification',
default: false,
type: 'boolean'
},
})
const routes = [
'/wp-admin/css',
'/wp-admin/images',
'/wp-admin/js',
'/wp-includes/css',
'/wp-includes/images',
'/wp-includes/js'
]
bs.init({
proxy: url.toString(),
baseDir: './public',
notify: argv.notify,
open: argv.open,
https: url.protocol === 'https',
serveStatic: routes.map(route => ({
route,
dir: `public/wp${route}`
}))
}, (err) => {
if (err) return reject(err)
url = new URL(url.toString())
url.host = 'localhost:3000'
return resolve(url)
})
})
/**
* Execute end-to-end test using webdriver.io and browser-stack.
*
* @async
* @param {Function} done
*/
exports.e2e = (done) => {
const { default: Launcher } = require('@wdio/cli')
return phpServer().then(bSync).then(url => {
const wdio = new Launcher('tests/wdio.config.js', {
baseUrl: url.toString(),
onComplete (code) {
done()
process.exit(code)
}
})
return wdio.run()
})
}
/**
* Generate CHANGELOG.md file.
*/
exports.release = async () => {
const { argv } = args.options({
pre: {
describe: 'Bump as prerelase version',
type: 'string'
},
as: {
describe: 'Bump as version',
type: 'string'
},
sign: {
describe: 'Sign the release tag',
type: 'boolean',
default: isProduction
}
})
const releaseConfig = {
commitAll: true,
sign: argv.sign,
skip: {},
scripts: {
postbump: `yarn archive --mode ${process.env.NODE_ENV}`,
}
}
if (!isProduction) {
releaseConfig.skip.tag = true
releaseConfig.skip.commit = true
} else {
releaseConfig.scripts.precommit = 'git add -A'
}
if (typeof argv.pre === 'string') {
releaseConfig.prerelease = argv.pre
}
if (typeof argv.as === 'string') {
releaseConfig.releaseAs = argv.as
}
await version(releaseConfig)
}
/**
* Start php development server and watch files changes through browserSync.
*/
exports.default = async () => {
const url = await phpServer()
await bSync(url)
watch(tasks, bs)
}