-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
gulpfile.js
380 lines (333 loc) Β· 11.1 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
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
import pkg from "gulp";
import babel from "gulp-babel";
import browserSync from "browser-sync";
import concat from "gulp-concat";
import { deleteAsync } from "del";
import log from "fancy-log";
import fs from "fs";
import partialimport from "postcss-easy-import";
import plumber from "gulp-plumber";
import postcss from "gulp-postcss";
import postCSSMixins from "postcss-mixins";
import autoprefixer from "autoprefixer";
import postcssPresetEnv from "postcss-preset-env";
import sourcemaps from "gulp-sourcemaps";
import uglify from "gulp-uglify";
import zip from "gulp-vinyl-zip";
import cleanCSS from "gulp-clean-css";
const { series, dest, src, watch } = pkg;
/* -------------------------------------------------------------------------------------------------
Theme Name
-------------------------------------------------------------------------------------------------- */
const themeName = process.env.THEME_NAME || "wordpressify";
/* -------------------------------------------------------------------------------------------------
PostCSS Plugins
-------------------------------------------------------------------------------------------------- */
const pluginsListDev = [
partialimport,
postcssPresetEnv({
stage: 0,
features: {
"nesting-rules": true,
"custom-media-queries": true,
},
}),
postCSSMixins,
autoprefixer,
];
const pluginsListProd = [
partialimport,
postcssPresetEnv({
stage: 0,
features: {
"nesting-rules": true,
"custom-media-queries": true,
},
}),
postCSSMixins,
autoprefixer,
];
/* -------------------------------------------------------------------------------------------------
Header & Footer JavaScript Bundles
-------------------------------------------------------------------------------------------------- */
const headerJS = ["./node_modules/jquery/dist/jquery.js"];
const footerJS = ["./src/assets/js/**"];
/* -------------------------------------------------------------------------------------------------
Environment Tasks
-------------------------------------------------------------------------------------------------- */
function registerCleanup(done) {
process.on("SIGINT", () => {
process.exit(0);
});
done();
}
/* -------------------------------------------------------------------------------------------------
Development Tasks
-------------------------------------------------------------------------------------------------- */
function devServer() {
const portnumber = parseInt(process.env.PROXY_PORT) || 3010;
browserSync({
logPrefix: "π³ WordPressify",
proxy: {
target: "webserver:8080",
proxyReq: [
function (proxyReq, req) {
proxyReq.setHeader("Host", req.headers.host);
},
],
},
host: "localhost",
port: portnumber,
notify: false,
open: false,
logConnections: true,
});
const watchOptions = process.env.USE_POLLING === "true" && {
usePolling: true,
interval: 1000,
};
const watcherCSS = watch(["./src/assets/css/**/*.css", "!./**/.DS_Store"], watchOptions);
const watcherJs = watch(["./src/assets/js/**", "!./**/.DS_Store"], watchOptions);
const watcherImg = watch(["./src/assets/img/**", "!./**/.DS_Store"], watchOptions);
const watcherFonts = watch(["./src/assets/fonts/**", "!./**/.DS_Store"], watchOptions);
const watcherTheme = watch(["./src/theme/**", "!./**/.DS_Store"], watchOptions);
const watcherPlugins = watch(["./src/plugins/**", "!./**/.DS_Store"], watchOptions);
watcherCSS.on("all", function (event, path) {
console.log(`${wpFy} - CSS Watcher Event "${event}" triggered for file "${path}" βοΈ`);
stylesDev();
});
watcherJs.on("all", function (event, path) {
console.log(`${wpFy} - JS Watcher Event "${event}" triggered for file "${path}" βοΈ`);
footerScriptsDev();
Reload();
});
watcherImg.on("all", function (event, path) {
console.log(`${wpFy} - IMG Watcher Event "${event}" triggered for file "${path}" βοΈ`);
copyImagesDev();
Reload();
});
watcherImg.on("unlink", async function (path) {
await deleteFiles(path, "isAssets");
});
watcherFonts.on("all", function (event, path) {
console.log(`${wpFy} - Fonts Watcher Event "${event}" triggered for file "${path}" βοΈ`);
copyFontsDev();
Reload();
});
watcherFonts.on("unlink", async function (path) {
await deleteFiles(path, "isAssets");
});
watcherTheme.on("all", function (event, path) {
console.log(`${wpFy} - Theme Watcher Event "${event}" triggered for file "${path}" βοΈ`);
copyThemeDev();
Reload();
});
watcherTheme.on("unlink", async function (path) {
await deleteFiles(path);
});
watcherPlugins.on("all", function (event, path) {
console.log(`${wpFy} - Plugins Watcher Event "${event}" triggered for file "${path}" βοΈ`);
pluginsDev();
Reload();
});
}
async function deleteFiles(path, isAassets) {
console.log(`${wpFy} - File ${path} was removed!`);
let pathName;
if (isAassets) {
pathName = path.replace("src/assets/", "");
} else {
pathName = path.replace("src/theme/", "");
}
console.log("Deleting: " + pathName + " π«");
await deleteAsync("./build/wordpress/wp-content/themes/" + themeName + "/" + pathName);
}
async function cleanTheme() {
await deleteAsync("./build/wordpress/wp-content/themes/" + themeName);
}
function Reload() {
browserSync.reload();
}
function copyThemeDev() {
if (!fs.existsSync("./build")) {
log(buildNotFound);
process.exit(1);
} else {
return src("./src/theme/**", { encoding: false }).pipe(
dest("./build/wordpress/wp-content/themes/" + themeName),
);
}
}
function copyImagesDev() {
return src("./src/assets/img/**", { encoding: false }).pipe(
dest("./build/wordpress/wp-content/themes/" + themeName + "/img"),
);
}
function copyFontsDev() {
return src("./src/assets/fonts/**", { encoding: false }).pipe(
dest("./build/wordpress/wp-content/themes/" + themeName + "/fonts"),
);
}
function stylesDev() {
return src("./src/assets/css/style.css")
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(postcss(pluginsListDev))
.pipe(sourcemaps.write("."))
.pipe(dest("./build/wordpress/wp-content/themes/" + themeName))
.pipe(browserSync.stream({ match: "**/*.css" }));
}
function headerScriptsDev() {
return src(headerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(concat("header-bundle.js"))
.pipe(sourcemaps.write("."))
.pipe(dest("./build/wordpress/wp-content/themes/" + themeName + "/js"));
}
function footerScriptsDev() {
return src(footerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(sourcemaps.init())
.pipe(
babel({
presets: ["@babel/preset-env"],
}),
)
.pipe(concat("footer-bundle.js"))
.pipe(sourcemaps.write("."))
.pipe(dest("./build/wordpress/wp-content/themes/" + themeName + "/js"));
}
function pluginsDev() {
return src(["./src/plugins/**", "!./src/plugins/README.md"], {
encoding: false,
}).pipe(dest("./build/wordpress/wp-content/plugins"));
}
const dev = series(
registerCleanup,
cleanTheme,
copyThemeDev,
copyImagesDev,
copyFontsDev,
stylesDev,
headerScriptsDev,
footerScriptsDev,
pluginsDev,
devServer,
);
dev.displayName = "dev";
export { dev };
/* -------------------------------------------------------------------------------------------------
Production Tasks
-------------------------------------------------------------------------------------------------- */
async function cleanProd() {
await deleteAsync(["./dist/*"]);
}
function copyThemeProd() {
return src(["./src/theme/**", "!./src/theme/**/node_modules/**"], {
encoding: false,
}).pipe(dest("./dist/themes/" + themeName));
}
function copyFontsProd() {
return src("./src/assets/fonts/**", { encoding: false }).pipe(
dest("./dist/themes/" + themeName + "/fonts"),
);
}
function stylesProd() {
return src("./src/assets/css/style.css")
.pipe(plumber({ errorHandler: onError }))
.pipe(postcss(pluginsListProd))
.pipe(cleanCSS())
.pipe(dest("./dist/themes/" + themeName));
}
function headerScriptsProd() {
return src(headerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(concat("header-bundle.js"))
.pipe(uglify())
.pipe(dest("./dist/themes/" + themeName + "/js"));
}
function footerScriptsProd() {
return src(footerJS)
.pipe(plumber({ errorHandler: onError }))
.pipe(
babel({
presets: ["@babel/preset-env"],
}),
)
.pipe(concat("footer-bundle.js"))
.pipe(uglify())
.pipe(dest("./dist/themes/" + themeName + "/js"));
}
function pluginsProd() {
return src(["./src/plugins/**", "!./src/plugins/**/*.md"], {
encoding: false,
}).pipe(dest("./dist/plugins"));
}
function processImages() {
return src("./src/assets/img/**", { encoding: false })
.pipe(plumber({ errorHandler: onError }))
.pipe(dest("./dist/themes/" + themeName + "/img"));
}
function zipProd() {
return src("./dist/themes/" + themeName + "/**/*")
.pipe(zip.dest("./dist/" + themeName + ".zip"))
.on("end", () => {
log(pluginsGenerated);
log(filesGenerated);
log(thankYou);
});
}
const prod = series(
cleanProd,
copyThemeProd,
copyFontsProd,
stylesProd,
headerScriptsProd,
footerScriptsProd,
pluginsProd,
processImages,
zipProd,
);
prod.displayName = "prod";
export { prod };
/* -------------------------------------------------------------------------------------------------
Utility Tasks
-------------------------------------------------------------------------------------------------- */
const onError = (err) => {
log(wpFy + " - " + errorMsg + " " + err.toString());
};
function Backup() {
if (!fs.existsSync("./build")) {
log(buildNotFound);
process.exit(1);
} else {
return src("./build/**/*")
.pipe(zip.dest("./backups/" + date + ".zip"))
.on("end", () => {
log(backupsGenerated);
log(thankYou);
});
}
}
Backup.displayName = "backup";
export { Backup };
/* -------------------------------------------------------------------------------------------------
Messages
-------------------------------------------------------------------------------------------------- */
const date = new Date().toLocaleDateString("en-GB").replace(/\//g, ".");
const errorMsg = "\x1b[41mError\x1b[0m";
const buildNotFound =
errorMsg +
" β οΈγ- You need to build the project first. Run the command: $ \x1b[1mdocker compose build\x1b[0m";
const filesGenerated =
"Your ZIP template file was generated in: \x1b[1m" + "/dist/" + themeName + ".zip\x1b[0m - β
";
const pluginsGenerated = "Plugins are generated in: \x1b[1m" + "/dist/plugins/\x1b[0m - β
";
const backupsGenerated =
"Your backup was generated in: \x1b[1m" + "/backups/" + date + ".zip\x1b[0m - β
";
const wpFy = "\x1b[42m\x1b[1mWordPressify\x1b[0m";
const wpFyUrl = "\x1b[2m - https://www.wordpressify.co/\x1b[0m";
const thankYou = "Thank you for using " + wpFy + wpFyUrl;
/* -------------------------------------------------------------------------------------------------
End of all Tasks
-------------------------------------------------------------------------------------------------- */