forked from StaticMania/blogge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·336 lines (274 loc) · 9.31 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
/*
npm run gulp watch
//generate sections
npm run gulp sections
//generate blocks
npm run gulp blocks
//take screenshots for all sections
npm run gulp screenshots
//take screenshots for specified section groups
npm run gulp screenshots -hero -features
*/
const gulp = require('gulp');
const fileinclude = require('gulp-file-include');
//const sass = require('gulp-sass')(require('sass'));
const sass = require('gulp-sass')(require('node-sass'));
const sassGlob = require('gulp-sass-glob');
const formatHtml = require('gulp-format-html');
//const gutil = require('gulp-util');
const rename = require("gulp-rename");
const imageResize = require('gulp-image-resize');
const through2 = require( 'through2' );
const connect = require('gulp-connect');
const puppeteer = require('puppeteer');
const fs = require('fs')
const path = require('path');
let baseHref = '';
let blockPrefix = "landing";
//get command line parameters after some specified parameter
function parameters() {
return process.argv.filter(a => a[0] == '-' ).map(a => a.slice(1));
}
const touch = () => through2.obj( function( file, enc, cb ) {
if ( file.stat ) {
file.stat.atime = file.stat.mtime = file.stat.ctime = new Date();
}
cb( null, file );
});
gulp.task('fileinclude', function() {
return gulp.src(['./src/*.html', './src/**/*.html', '!**/-*/**', '!**/_*/**'])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(formatHtml())
.pipe( touch() )
.pipe(gulp.dest('./'));
});
gulp.task('sass', function() {
return gulp.src(['./scss/*.scss', './scss/**/*.scss', '!**/_*/**'])
.pipe(sassGlob())
.pipe(sass())
.pipe(gulp.dest('./css'));
});
gulp.task('watch', function () {
gulp.watch(['./src/*.html', './src/**/*.html'], gulp.series('fileinclude'));
gulp.watch(['./scss/*.scss', './scss/**/*.scss', './scss/**/**/*.scss'], gulp.series('sass'));
});
var current_section = "posts";
async function screenshots(dirs = []) {
let sectionsDir = path.resolve("./sections/");
let screenshotDir = path.resolve("./screenshots/");
let styleCss = path.resolve("./css/style.css");
let selector = "body > section";
let sections = [];
let baseDir = path.resolve(".");
if (!dirs.length) {
dirs = fs.readdirSync(sectionsDir).map(fileName => {
let filePath = `${sectionsDir}/${fileName}`;
let stat = fs.statSync(filePath)
if (stat.isDirectory()) {
let files = fs.readdirSync(filePath).map(sectionFile => {
//if (filePath.indexOf(current_section) > 0)
{
sections.push(`${filePath}/${sectionFile}`);
}
});
}
});
}
const browser = await puppeteer.launch({
args: [
'--start-maximized'
],
headless: true,
//slowMo: 250,
//devtools: true,
});
const page = await browser.newPage();
//await page.setRequestInterception(true);
await page.setViewport({
width: 1500,
height: 800,
//deviceScaleFactor: 2
});
page.on('console', (msg) => console.log('PAGE LOG:', msg.text));
await page.evaluate(() => console.log(`url is ${location.href}`));
/*
page.on('request', (request) => {
if (request.resourceType() === 'image') request.abort();
else request.continue();
});
*/
let tempFiles = [];
for (i in sections) {
section = sections[i];
screenshot = section.replace(sectionsDir, screenshotDir).replace('.html', '.png');
tempFiles.push(screenshot);
sectionScreenshot = section.replace('.html', '-screenshot.html');;
tempFiles.push(sectionScreenshot);
//gutil.log(`Start screenshot for '${gutil.colors.cyan(section)}' to '${gutil.colors.magenta(screenshot)}'`);
console.log(`Start screenshot for '${section}' to '${screenshot}'`);
folderName = path.dirname(screenshot);
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName)
}
let content = fs.readFileSync(section,'utf8');
let html = `<html><head><base href="../../"><link href="https://fonts.googleapis.com/css2?family=Nunito:wght@300;500;700&family=Inter:wght@400;700&" rel="stylesheet"><link href="/css/style.css" rel="stylesheet"><link href="/css/screenshots.css" rel="stylesheet"></head><body>${content}</body></html>`;
fs.writeFileSync(sectionScreenshot, html);
//await page.setContent(html, {"waitUntil":"networkidle0"});
url = "http://127.0.0.1:8008" sectionScreenshot.replace(baseDir, '');
/*
await Promise.all([ page.goto(url, { waitUntil: "load", timeout: 10000 }).catch(e => {
if (e.message.includes('net::ERR_ABORTED')) { console.log('PAGE LOG:', e.message) }
}), page.waitForNavigation() ]);
*/
await page.goto(url, { waitUntil: "load", timeout: 10000 }).catch(e => {
console.log('PAGE LOG:', e.message);
//if (e.message.includes('net::ERR_ABORTED')) { console.log('PAGE LOG:', e.message) }
});
//await page.goto("file://" section);
/*
await page.addStyleTag({url: `file://${styleCss}`})
const result = await page.evaluate((baseHref) => {
let head = document.querySelector('head');
let base = document.createElement("base");
base.href= `file://${baseHref}/`;
head.append(base);
}, baseHref);
*/
const element = await page.$$("body > *");
//await page.screenshot({ path: screenshot, fullPage: true, type: 'png' });
await element[0].screenshot({ path: screenshot, type: 'png' });
gulp.src(screenshot)
.pipe(imageResize({
width : 480,
format: "jpeg",
quality:0,
}))
.pipe(rename(function (path) { path.basename = "-thumb"; }))
.pipe(gulp.dest(folderName)).on('end', () => {
//remove original screenshot and keep only thumb
});
}
await browser.close();
tempFiles.map(screenshot => {
fs.stat(screenshot, function (err, stats) {
if (err) {
return console.error(err);
}
fs.unlink(screenshot,function(err){
if(err) {
return console.log(err);
}
//console.log('file deleted successfully');
});
})
});
}
let templates = {
'section':{
'group':`
Vvveb.SectionsGroup["{{group}}"] = [{{groupList}}];
`,
'section':
`Vvveb.Sections.add("{{id}}", {
name: "{{name}}",
image: Vvveb.themeBaseUrl "/{{image}}",
html: \`{{html}}\`
});`
},
'block': {
'group':`
Vvveb.BlocksGroup['{{group}}'] = [{{groupList}}];
`,
'section':
`Vvveb.Blocks.add("{{id}}", {
name: "{{name}}",
image: Vvveb.themeBaseUrl "/{{image}}",
html: \`{{html}}\`
});`
}};
function template(template, data) {
return template.replace(/{{(. ?)}}/gm, function(match, p1) {
return data[p1];
})
}
function prettify(str) {
return str.charAt(0).toUpperCase() str.slice(1).replace(/[-_]/g,' ');
}
let sectionsSortOrder = [ "hero", "features", "banner", "services"];
//generate sections for VvvebJs
function sections(type = "section", dirs = []) {
let sectionsDir = path.resolve(`./${type}s/`);
let screenshotDir = path.resolve("./screenshots/");
let styleCss = path.resolve("./css/style.css");
let baseDir = path.resolve(".");
let dir = [];
let sectionsJs = '';
if (!dirs.length) {
dirs = fs.readdirSync(sectionsDir).map(fileName => {
let stat = fs.statSync(`${sectionsDir}/${fileName}`);
if (stat.isDirectory()) {
dir.push(fileName);
}
});
}
//console.dir(dir);
sectionsSortOrder.reverse();
//sort sections
dir.sort((a, b) => sectionsSortOrder.indexOf(b) - sectionsSortOrder.indexOf(a));
//dir = [current_section];
for (i in dir) {
let fileName = dir[i];
let filePath = `${sectionsDir}/${fileName}`;
let group = fileName;
let name;
let id;
let html;
let sectionsHtml = '<html><head><base src="../"><link href="../css/style.css" rel="stylesheet"><link href="../css/screenshots.css" rel="stylesheet"></head><body>';
let image = '';
let sections = [];
let files = fs.readdirSync(filePath).
sort((a, b) => parseInt(a.replace( /^\D /g, '')) - parseInt(b.replace( /^\D /g, ''))).
map(sectionFile => {
name = sectionFile.replace(".html","");
id = `${group}/${name}`;
name = prettify(name);
image = `screenshots/${group}/` sectionFile.replace(".html", "-thumb.jpeg");
html = fs.readFileSync(`${filePath}/${sectionFile}`,'utf8');
sections.push(id);
sectionsJs = template(templates[type]['section'], {id, name, image, html});
sectionsHtml = html.replace('<section', '<section title="' name '"');
});
fs.writeFileSync(filePath ".html", sectionsHtml "</body></html>");
let groupList = '"' sections.join('","') '"';
group = prettify(group);
sectionsJs = template(templates[type]['group'], {group, groupList});
}
return fs.writeFileSync(/*path.resolve(".") *//*`${sectionsDir}/${type}s.js`*/path.resolve(".") `/${type}s/${type}s.js`, sectionsJs);
}
gulp.task('connect', async function (done) {
await connect.server({
port: 8008,
//root: './',
});
});
gulp.task('take-screenshots', async function (done) {
await screenshots(parameters());
});
gulp.task('take-screenshots-blocks', async function (done) {
await screenshots("./blocks/");
});
gulp.task('sections', async function () {
return sections("section", parameters());
});
gulp.task('blocks', async function () {
return sections("block", parameters());
});
// Default Task
gulp.task('default', gulp.series('fileinclude', 'sass'));
gulp.task('screenshots', gulp.series('connect', 'take-screenshots'));
gulp.task('screenshots-blocks', gulp.series('connect', 'take-screenshots-blocks'));
gulp.task('sections', gulp.series('sections'));
gulp.task('blocks', gulp.series('blocks'));