forked from TYPO3/TYPO3.Icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
491 lines (449 loc) · 14 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//
// Require
//
const fs = require('fs');
const pkg = require('./package.json');
const path = require('path');
const del = require('del');
const twig = require('gulp-twig');
const svgmin = require('gulp-svgmin');
const rename = require('gulp-rename');
const gulp = require('gulp');
const svgSprite = require('gulp-svg-sprite');
const yaml = require('js-yaml');
const merge = require('deepmerge');
const execSync = require('child_process').execSync;
const minifyCSS = require('gulp-clean-css');
const sass = require('gulp-sass');
sass.compiler = require('node-sass');
//
// Options
//
const options = {
rendering: {
actions: {
preview: true,
buttons: true,
},
apps: {
preview: true,
buttons: true,
},
avatar: {
preview: true,
buttons: true,
},
content: {
preview: true,
buttons: true,
},
default: {
preview: true,
buttons: true,
},
files: {
preview: true,
},
form: {
preview: true,
buttons: true,
},
information: {
preview: true,
},
install: {
preview: true,
},
mimetypes: {
preview: true,
buttons: true,
},
miscellaneous: {
preview: true,
buttons: true,
},
module: {
preview: true,
},
modulegroup: {
preview: true,
},
overlay: {
overlay: true,
},
spinner: {
preview: true,
buttons: true,
spinning: true,
},
status: {
preview: true,
buttons: true,
},
},
src: './src/',
dist: './dist/',
dist_svgs: './dist/svgs/',
dist_sprites: './dist/sprites/',
assets: './assets/',
site: './_site/',
meta: './meta/',
material: './material/'
};
//
// Custom Functions
//
function getSvgoConfig() {
let config = fs.readFileSync('svgo.yaml', 'utf8');
config = yaml.load(config)
return config;
}
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function (file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
function getIcons(dir) {
return fs.readdirSync(dir)
.filter(function (file) {
var fileExtension = path.extname(path.join(dir, file));
if (fileExtension === ".svg") {
return true;
} else {
return false;
}
});
}
function getMetaFiles(dir) {
return fs.readdirSync(dir)
.filter(function (file) {
var fileExtension = path.extname(path.join(dir, file));
if (fileExtension === ".yaml") {
return true;
} else {
return false;
}
});
}
function getCategories() {
let categories = getFolders(options.src);
return categories.map((category) => {
let icons = getIcons(path.join(options.dist_svgs, category)).map((icon) => { return path.basename(icon, '.svg') });
return {
identifier: category,
title: category.charAt(0).toUpperCase() + category.slice(1),
sprite: 'sprites/' + category + '.svg',
rendering: options.rendering[category] ?? {},
icons: icons,
count: icons.length
};
});
}
function getMetaDataFileName(identifier, folder) {
return path.join(options.meta, folder, identifier + '.yaml' );
}
function getMetaData(identifier, folder) {
let defaultmeta = {
alias: [
],
changes: [
],
tags: [
]
};
let metafile = path.join(options.meta, folder, identifier + '.yaml' );
let metadata = {};
if (fs.existsSync(metafile)) {
metadata = yaml.load(fs.readFileSync(metafile, 'utf8'));
}
return merge.all([defaultmeta, metadata]);
}
function updateMetaData(identifier, folder, metadata) {
let metafile = getMetaDataFileName(identifier, folder);
if (!fs.existsSync(path.dirname(metafile))){
fs.mkdirSync(path.dirname(metafile));
}
fs.writeFileSync(metafile, yaml.dump(metadata), 'utf8');
}
function getVersionChanges(identifier, folder) {
let filename = path.join(options.src, folder, identifier + '.svg');
let commithashes = execSync('git log --find-renames --find-renames=100% --pretty=format:"%H" ' + filename).toString().split("\n");
let fileversions = {};
for (let key in commithashes) {
let hashversions = execSync('git tag --contains ' + commithashes[key]).toString().split("\n");
for (let versionKey in hashversions) {
if (hashversions[versionKey].length > 0) {
hashversion = hashversions[versionKey].charAt(0) === 'v' ? hashversions[versionKey].substr(1) : hashversions[versionKey];
fileversions[hashversion] = hashversion;
}
}
}
let versions = [];
for (let key in fileversions) {
versions.push(key);
}
return sortVersions(versions);
}
function sortVersions(versions) {
return versions.sort((a, b) => a.localeCompare(b, undefined, { numeric:true }) );
}
function generateVersionData(identifier, folder) {
let metadata = getMetaData(identifier, folder)
metadata.changes = getVersionChanges(identifier, folder);
updateMetaData(identifier, folder, metadata);
}
function getData() {
let data = {};
data.icons = {};
data.aliases = {};
let folders = getFolders(options.dist_svgs);
for (var folderCount = 0; folderCount < folders.length; folderCount++) {
let folder = folders[folderCount];
let iconFiles = getIcons(path.join(options.dist_svgs, folder));
for (var i = 0; i < iconFiles.length; i++) {
let file = path.join('svgs', folder, iconFiles[i]);
let identifier = path.basename(file, '.svg');
let metaData = getMetaData(identifier, folder);
if (metaData.alias.length > 0) {
for (let aliasKey in metaData.alias) {
data.aliases[metaData.alias[aliasKey]] = identifier;
}
}
data.icons[identifier] = {};
data.icons[identifier].identifier = identifier;
data.icons[identifier].category = folder;
data.icons[identifier].svg = 'svgs/' + folder + '/' + identifier + '.svg';
data.icons[identifier].sprite = 'sprites/' + folder + '.svg' + '#' + identifier;
}
}
return data;
}
//
// Icons Clean
//
gulp.task('icons-clean', () => {
return del([options.dist], { force: true });
});
//
// Icons Sass
//
gulp.task('icons-sass', () => {
let tasks = [];
tasks.push(new Promise((resolve) => {
gulp.src(path.join(options.assets, 'scss/icons.scss'))
.pipe(gulp.dest(options.dist))
.pipe(sass().on('error', sass.logError))
.pipe(minifyCSS())
.pipe(gulp.dest(options.dist))
.on('end', resolve);
}));
tasks.push(new Promise((resolve) => {
gulp.src(path.join(options.assets, 'scss/icons-bootstrap.scss'))
.pipe(gulp.dest(options.dist))
.pipe(sass().on('error', sass.logError))
.pipe(minifyCSS())
.pipe(gulp.dest(options.dist))
.on('end', resolve);
}));
return Promise.all(tasks);
});
//
// Icons Minify SVGs
//
gulp.task('icons-min', () => {
return gulp.src([options.src + '**/*.svg'])
.pipe(svgmin(getSvgoConfig()))
.pipe(gulp.dest(options.dist_svgs));
});
//
// Icons SVG Sprites
//
gulp.task('icons-sprites', () => {
let processFolder = (folder) => {
return new Promise((resolve, reject) => {
gulp.src([path.join(options.dist_svgs, folder) + '/*.svg'])
.pipe(svgSprite({
svg: {
rootAttributes: {
class: 'typo3-icons-' + folder,
style: 'display: none;'
},
namespaceIDs: true,
namespaceClassnames: false
},
mode: {
symbol: {
dest: '',
sprite: folder + '.svg'
}
}
}))
.on('error', reject)
.pipe(gulp.dest(options.dist_sprites))
.on('end', resolve);
});
};
return Promise.all(getFolders(options.dist_svgs).map(processFolder));
});
//
// Icons Data
//
gulp.task('icons-data', (cb) => {
let data = JSON.stringify(getData(), null, 2);
fs.writeFile(options.dist + 'icons.json', data, 'utf8', () => {
cb();
});
});
//
// Versions History
//
gulp.task('version-history', () => {
let tasks = [];
tasks.push(new Promise((resolve) => {
let folders = getFolders(options.meta);
for (var folderCount = 0; folderCount < folders.length; folderCount++) {
let folder = folders[folderCount];
let files = getMetaFiles(path.join(options.meta, folder));
for (var i = 0; i < files.length; i++) {
let file = path.join(folder, files[i]);
let identifier = path.basename(file, '.yaml');
fs.stat(path.join(options.src, folder, identifier + '.svg'), (error) => {
if (error !== null && error.code === 'ENOENT') {
del(path.join(options.meta, file), { force: true });
}
});
}
}
resolve();
}));
tasks.push(new Promise((resolve) => {
let folders = getFolders(options.src);
for (var folderCount = 0; folderCount < folders.length; folderCount++) {
var folder = folders[folderCount];
var iconFiles = getIcons(options.src + folder);
for (var i = 0; i < iconFiles.length; i++) {
let file = path.join(folder, iconFiles[i]);
let identifier = path.basename(file, '.svg');
generateVersionData(identifier, folder);
}
}
resolve();
}));
return Promise.all(tasks);
});
/**
* Docs
*/
gulp.task('site-clean', () => {
return del([options.site], { force: true });
});
gulp.task('site-build', function (cb) {
// Build Docs CSS
gulp.src(path.join(options.assets, 'scss/docs.scss'))
.pipe(sass().on('error', sass.logError))
.pipe(minifyCSS())
.pipe(gulp.dest(path.join(options.site, 'assets', 'css')))
// Copy static assets
gulp.src([path.join(options.assets, '**/*'),
'!' + path.join(options.assets, '**/*(*.scss)'),
], { base: options.assets })
.pipe(gulp.dest(path.join(options.site, 'assets')));
gulp.src([path.join(options.dist, '**/*')], { base: options.dist } )
.pipe(gulp.dest(path.join(options.site, 'dist')));
// Fetch generated data
let typo3 = JSON.parse(fs.readFileSync('./typo3.json', 'utf8'))
let categories = getCategories();
let data = JSON.parse(fs.readFileSync(options.dist + 'icons.json', 'utf8'));
let icons = data.icons;
for(let iconKey in icons) {
icons[iconKey]._meta = getMetaData(icons[iconKey].identifier, icons[iconKey].category);
icons[iconKey]._inline = fs.readFileSync(path.join(options.dist, icons[iconKey].svg), 'utf8')
}
// Index
gulp.src('./tmpl/html/docs/index.html.twig')
.pipe(twig({
data: {
pkg: pkg,
typo3: typo3,
icons: icons,
category: {},
categories: categories,
rendering: {},
pathPrefix: '',
}
}))
.pipe(rename('index.html'))
.pipe(gulp.dest(path.join(options.site)));
// Guide
gulp.src('./tmpl/html/docs/guide.html.twig')
.pipe(twig({
data: {
pkg: pkg,
typo3: typo3,
icons: icons,
category: {},
categories: categories,
rendering: {},
pathPrefix: '',
}
}))
.pipe(rename('guide.html'))
.pipe(gulp.dest(path.join(options.site)));
// Build pages
for (let categoryKey in categories) {
let category = categories[categoryKey];
gulp.src('./tmpl/html/docs/section.html.twig')
.pipe(twig({
data: {
pkg: pkg,
typo3: typo3,
icons: icons,
category: category,
categories: categories,
pathPrefix: '../',
}
}))
.pipe(rename(category.identifier + '.html'))
.pipe(gulp.dest(path.join(options.site, 'icons')));
for (let iconKey in category.icons) {
let iconIdentifier = category.icons[iconKey];
let icon = icons[iconIdentifier];
gulp.src('./tmpl/html/docs/single.html.twig')
.pipe(twig({
data: {
pkg: pkg,
typo3: typo3,
icon: icon,
icons: icons,
category: category,
categories: categories,
pathPrefix: '../../',
}
}))
.pipe(rename(iconIdentifier + '.html'))
.pipe(gulp.dest(path.join(options.site, 'icons', category.identifier)));
}
}
cb();
});
//
// Tasks
//
gulp.task('icons', gulp.series(
'icons-clean',
'icons-sass',
'icons-min',
'icons-sprites',
'icons-data'
));
gulp.task('default', gulp.series(
'icons'
));
gulp.task('version', gulp.series(
'version-history'
));
gulp.task('site', gulp.series(
'site-clean',
'site-build'
));