forked from lab19digital/wp19
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
446 lines (388 loc) · 12.3 KB
/
gulpfile.babel.js
File metadata and controls
446 lines (388 loc) · 12.3 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Dependencies
import gulp from 'gulp'
import path from 'path'
import shell from 'gulp-shell'
import prompt from 'gulp-prompt'
import replace from 'gulp-replace'
import rename from 'gulp-rename'
import request from 'request'
import source from 'vinyl-source-stream'
import del from 'del'
import connectPHP from 'gulp-connect-php'
import plumber from 'gulp-plumber'
import notify from 'gulp-notify'
import colors from 'colors/safe'
import dartSass from 'sass'
import gulpSass from 'gulp-sass'
import postcss from 'gulp-postcss'
// import postcssScss from 'postcss-scss'
// import postcssBemLinter from 'postcss-bem-linter'
import autoprefixer from 'autoprefixer'
import sourcemaps from 'gulp-sourcemaps'
import webpack from 'webpack'
import webpackStream from 'webpack-stream'
import webpackConfigDEV from './webpack.dev'
import webpackConfigPROD from './webpack.prod'
import promptConfig from './prompt-config'
import packageJSON from './package.json'
import themeJSON from './theme.json'
import { create as browserSyncCreate } from 'browser-sync'
const sass = gulpSass(dartSass)
// Settings
const wpCli = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar'
let theme = themeJSON.theme
const basePath = __dirname
const themePath = path.resolve(__dirname, `wp-content/themes/${theme}`).replace(/\\/g, '/')
const nodePath = path.resolve(__dirname, 'node_modules').replace(/\\/g, '/')
const destPath = `${themePath}/dist`
const baseName = path.basename(basePath)
// console.log(colors.bold('Base name: ') + baseName);
// console.log(colors.bold('Base path: ') + basePath);
// console.log(colors.bold('Theme name: ') + theme);
// console.log(colors.bold('Theme path: ') + themePath);
// console.log(colors.bold('Build path: ') + destPath);
const browserSync = browserSyncCreate()
const browserSyncProxy = `${baseName}.test`
// const bemUtilitySelectors = /^\.u-/
// const bemIgnoreSelectors = [/^\.has-/, /\.container/, /\.row/, /\.col/, /#{\$this}/]
// PROJECT SETUP
// =======================================================================
// Fetch WP CLI
function get_wp_cli() {
return request(wpCli).pipe(source('wp-cli.phar')).pipe(gulp.dest('./'))
}
// Initialize Setup
// Runs through a series of prompts, altering the wp-config template and
// finally outputting a wp-config file and a wp-cli template file, used
// for the remaining installation. You can add more prompt options in
// prompt-config.js and use the prompt values returned below.
async function wp_init(done) {
try {
const res = await wp_prompt()
await wp_preflight(res)
return wp_setup()
} catch (err) {
process.stderr.write(colors.red.bold(err) + '\n')
done()
}
}
function wp_prompt() {
let result = {}
return new Promise((resolve, reject) => {
gulp
.src('wp-config.template.php')
.pipe(
prompt.prompt(promptConfig, (res) => {
result = res
gulp
.src('wp-config.template.php')
.pipe(replace('{ DB_NAME }', res.dbname))
.pipe(replace('{ DB_USER }', res.dbuser))
.pipe(replace('{ DB_PASSWORD }', res.dbpassword))
.pipe(replace('{ DB_HOST }', res.dbhost))
.pipe(replace('{ WP_USER }', res.wpuser))
.pipe(replace('{ WP_PASSWORD }', res.wppassword))
.pipe(rename('wp-config.php'))
.pipe(gulp.dest('./'))
gulp
.src('wp-config-staging.php')
.pipe(replace('{ DB_NAME }', res.dbname))
.pipe(replace('{ WP_USER }', res.wpuser))
.pipe(replace('{ WP_PASSWORD }', res.wppassword))
.pipe(gulp.dest('./'))
gulp
.src('wp-cli.template.yml')
.pipe(replace('{ DB_NAME }', res.dbname))
.pipe(replace('{ DB_USER }', res.dbuser))
.pipe(replace('{ DB_PASSWORD }', res.dbpassword))
.pipe(replace('{ DB_HOST }', res.dbhost))
.pipe(replace('{ WP_USER }', res.wpuser))
.pipe(replace('{ WP_PASSWORD }', res.wppassword))
.pipe(replace('{ WP_EMAIL }', res.wpemail))
.pipe(replace('{ WP_SITE_TITLE }', res.wpsitetitle))
.pipe(replace('{ WP_BASE_URL }', res.wpbase))
.pipe(rename('wp-cli.yml'))
.pipe(gulp.dest('./'))
gulp
.src('wp19/style.css')
.pipe(replace('wp19', res.wpsitetitle))
.pipe(gulp.dest('./wp19'))
gulp.src('theme.json').pipe(replace(theme, res.wptheme)).pipe(gulp.dest('./'))
theme = res.wptheme
})
)
.on('end', () => resolve(result))
.on('error', () => reject())
})
}
// Copy WP Base Theme
function copy_wp_base_theme() {
return gulp.src(['wp19/**/*']).pipe(gulp.dest(themePath))
}
// Copy 'pre-commit' git hook
function copy_git_pre_commit_hook() {
return gulp.src('git-pre-commit-hook').pipe(rename('pre-commit')).pipe(gulp.dest('./.git/hooks'))
}
// Check if requirements are met
function wp_preflight(res) {
const exec = require('child_process').execSync
const mysqlCheckPath = exec(`mysql --version || echo 'NO_MYSQL'`)
const mysqlCheckCred = exec(
`mysql -u ${res.dbuser} ${
res.dbpassword && res.dbpassword != '""' ? `-p${res.dbpassword}` : ''
} -e "SHOW DATABASES" || echo 'NO_MYSQL'`
)
return new Promise((resolve, reject) => {
if (mysqlCheckPath.toString().indexOf('NO_MYSQL') > -1) {
reject('mysql not available on PATH and required by wp-cli')
} else if (mysqlCheckCred.toString().indexOf('NO_MYSQL') > -1) {
reject('mysql credentials invalid or server not started')
} else {
resolve()
}
})
}
// Main WP Setup Task
// Runs through all remaining commands to install Wordpress and plugins
// NOTE: More plugins can be defined in package.json
function wp_setup() {
let plugins = packageJSON.wpcli.plugins
let pluginsString = ''
let cmd = []
for (let i = 0; i < plugins.length; i++) {
pluginsString = `${pluginsString} ${plugins[i]}`
}
cmd = cmd.concat([
`echo ${colors.blue.bold(
'Wordpress download, installation, and configuration will take a few minutes...'
)}`,
// Get the CLI tool
`echo ${colors.blue.bold('Fetching the CLI tool...')}`,
`gulp get_wp_cli`,
// Install WP
`php wp-cli.phar core download --force`,
`php wp-cli.phar db create`,
`php wp-cli.phar core install`,
// Copy the config
`gulp copy_wp_base_theme`,
`php wp-cli.phar theme activate ${theme}`,
// Create basic menu
`php wp-cli.phar menu create Primary`,
`php wp-cli.phar menu location assign Primary primary-nav`,
`php wp-cli.phar menu item add-custom Primary Home /`,
// Install plugins
`echo ${colors.blue.bold('Download and install plugins...')}`,
`php wp-cli.phar plugin install${pluginsString} --activate`,
// Remove unused themes and plugins
`echo ${colors.blue.bold('Removing unused themes and plugins...')}`,
`php wp-cli.phar plugin uninstall hello akismet`,
`php wp-cli.phar theme delete twentysixteen twentyseventeen twentynineteen twentytwenty twentytwentyone`,
// Change Permalinks
`echo ${colors.blue.bold('Saving permalinks...')}`,
`php wp-cli.phar rewrite structure '/%postname%/'`,
// Clean up
`echo ${colors.blue.bold('Cleaning the project...')}`,
`gulp cleanup`,
`gulp build`,
// Setting up new repo Git
`echo ${colors.blue.bold('Setting up new git repository...')}`,
`git init`,
`git add . --all`,
`git commit -m "Initial commit"`,
`gulp copy_git_pre_commit_hook`,
`git commit --amend --no-edit`,
// Done
`echo ${colors.bgGreen.white.bold('All set! Thanks for waiting.')}`
])
// Run these tasks
return shell.task(cmd)()
}
// Used for automated tests
async function wp19_test(done) {
let { test_prompt, test_theme } = require('./prompt-test.js').default()
theme = test_theme
try {
const res = await test_prompt()
await wp_preflight(res)
await wp_setup()
return shell.task([`yarn run block test-block-create`])()
} catch (err) {
done()
process.stderr.write(colors.red.bold(err) + '\n')
process.exit(1)
}
}
// Download WordPress
function download_wp() {
let cmd = []
cmd = cmd.concat([
`echo ${colors.blue.bold('Downloading Wordpress...')}`,
// Download WP
`php wp-cli.phar core download --skip-content --force`,
// Copy git hook
`echo ${colors.blue.bold('Copying git hook...')}`,
`gulp copy_git_pre_commit_hook`,
// Done
`echo ${colors.bgGreen.white.bold('All set! Thanks for waiting.')}`
])
// Run these tasks
return shell.task(cmd)()
}
// Cleanup
// Removes the .git folder. This repo should be cloned and used for another
// project, and does not require a git history.
// Removes other setup files that won't be required
function cleanup() {
return del(['.git', 'wp19', 'wp-cli.template.yml', 'wp-config.template.php'])
}
// DEV TASKS
// =======================================================================
// Plumber
const plumberHandler = {
errorHandler: notify.onError({
title: 'Gulp Error',
message: '<%= error.message %>'
})
}
// Reload
function reload(done) {
browserSync.reload()
done()
}
// SCSS
function scss() {
return (
gulp
.src(`${themePath}/scss/**/*.scss`)
.pipe(plumber(plumberHandler))
// .pipe(
// postcss(
// [
// postcssBemLinter({
// preset: 'bem',
// implicitComponents: `wp-content/themes/${theme}/scss/blocks/**/*.scss`,
// implicitUtilities: `wp-content/themes/${theme}/scss/utils/**/*.scss`,
// utilitySelectors: bemUtilitySelectors,
// ignoreSelectors: bemIgnoreSelectors
// })
// ],
// {
// syntax: postcssScss
// }
// )
// )
.pipe(sourcemaps.init())
.pipe(
sass({
precision: 10,
includePaths: [nodePath]
})
)
.pipe(postcss([autoprefixer({ cascade: false })]))
.pipe(sourcemaps.write())
.pipe(gulp.dest(destPath))
.pipe(browserSync.stream())
)
}
function scss_prod() {
return (
gulp
.src(`${themePath}/scss/**/*.scss`)
// .pipe(
// postcss(
// [
// postcssBemLinter({
// preset: 'bem',
// implicitComponents: `wp-content/themes/${theme}/scss/blocks/**/*.scss`,
// implicitUtilities: `wp-content/themes/${theme}/scss/utils/**/*.scss`,
// utilitySelectors: bemUtilitySelectors,
// ignoreSelectors: bemIgnoreSelectors
// })
// ],
// {
// syntax: postcssScss
// }
// )
// )
.pipe(
sass({
precision: 10,
outputStyle: 'compressed',
includePaths: [nodePath]
})
)
.pipe(postcss([autoprefixer({ cascade: false })]))
.pipe(gulp.dest(destPath))
)
}
// JS
function js() {
return gulp
.src(`${themePath}/js/main.js`)
.pipe(plumber(plumberHandler))
.pipe(webpackStream(webpackConfigDEV, webpack))
.pipe(gulp.dest(destPath))
}
function js_prod() {
return gulp
.src(`${themePath}/js/main.js`)
.pipe(webpackStream(webpackConfigPROD, webpack))
.pipe(gulp.dest(destPath))
}
// Watch
function watch_files() {
gulp.watch(`${themePath}/scss/**/*.scss`, scss)
gulp.watch(`${themePath}/js/**/*.js`, gulp.series(js, reload))
gulp.watch(`${themePath}/**/*.twig`, reload)
gulp.watch(`${themePath}/**/*.php`, reload)
gulp.watch(`${themePath}/**/*.html`, reload)
}
// PHP
function php_fn() {
connectPHP.server(
{
port: 8000,
open: false,
hostname: '127.0.0.1',
base: basePath,
stdio: 'ignore'
},
() => {
browserSync.init({
ghostMode: false,
ui: false,
notify: false,
proxy: '127.0.0.1:8000'
})
}
)
}
// Proxy
function proxy_fn() {
browserSync.init({
ghostMode: false,
ui: false,
notify: false,
proxy: browserSyncProxy
})
}
const php = gulp.parallel(php_fn, watch_files)
const proxy = gulp.parallel(proxy_fn, watch_files)
const build = gulp.parallel(scss_prod, js_prod)
export {
wp_init,
wp_setup,
wp_preflight,
wp19_test,
get_wp_cli,
download_wp,
copy_wp_base_theme,
copy_git_pre_commit_hook,
cleanup,
scss_prod,
js_prod,
php,
proxy,
build
}
export default php