-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
112 lines (97 loc) · 3.05 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
const ejs = require('gulp-ejs');
const rename = require('gulp-rename');
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
function findFiles(dir) {
return fs.readdirSync(dir)
.reduce((files, file) => {
return fs.statSync(path.join(dir, file)).isDirectory()
? files.concat(findFiles(path.join(dir, file)))
: files.concat(path.join(dir, file));
}, []);
}
function makePrintFriendly(s) {
return s.replace('-', ' ')
.replace('+', ' ')
.replace('_', ' ')
.replace('-', ' ')
.replace('-', ' ')
.replace('_', ' ')
.toLowerCase()
.split(' ')
.map(w => w.charAt(0).toUpperCase() + w.substring(1))
.join(' ');
}
/*
Category/Dimension/icon.svg
Category/Dimension/icon_dimension.svg
Category/icon_dimension.svg
Category/icon.svg
*/
function parsePath(p) {
const [ category, ...rest ] = p.split('/');
let iconName = rest[rest.length - 1];
iconName = path.basename(iconName, '.svg');
let size;
let name;
let sizeless = false;
switch (rest.length) {
// we have either (Category/)Dimension/icon.svg or (Category/)Dimension/icon_dimension.svg
case 2:
size = rest[0];
name = makePrintFriendly(iconName.replace(new RegExp(`_${rest[0]}$`), ''));
break;
case 1:
const icon = iconName.split('_');
const maybeSize = Number(icon[icon.length - 1]);
if (!isNaN(maybeSize)) {
size = maybeSize;
icon.pop();
name = makePrintFriendly(icon.join(' '));
} else {
size = 36;
sizeless = true;
name = makePrintFriendly(icon.join(' '));
}
break;
}
return {
category: makePrintFriendly(category),
size,
name,
sizeless
};
}
function build() {
const categories = findFiles('./public/icons')
.filter(f => f.match(/\.svg$/))
.map(f => f.replace('public/icons/', ''))
.reduce((categories, fullPath) => {
const { category, size, name, sizeless } = parsePath(fullPath);
categories[category] = categories[category] || {};
if (!categories[category][name]) {
categories[category][name] = categories[category][name] || {
name,
sizeless,
sizes: [{ size, fullPath }]
};
} else {
categories[category][name].sizes.push({ size, fullPath });
categories[category][name].sizes.sort((a, b) => a.size - b.size);
}
return categories;
}, {});
return gulp.src('./assets/template/index.ejs')
.pipe(ejs({ categories }))
.pipe(rename({ extname: '.html' }))
.pipe(gulp.dest('./public'));
}
function watch() {
gulp.watch('assets/template/*.ejs', build);
}
exports.default = build;
exports.watch = gulp.series(
build,
watch
);