-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgulpfile.js
144 lines (121 loc) · 3.43 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
'use strict';
const fs = require('fs');
const gulp = require('gulp');
const moment = require('moment');
const nunjucks = require('gulp-nunjucks-render');
const rename = require('gulp-rename');
const merge = require('gulp-merge-json');
const pkg = require('./package.json');
// Config
const paths = {
data: {
src: './tests/mock/*.json',
db: './tests/mock/db/db.json',
dest: './tests/mock/db/',
},
twig: {
src: './all/views/*.twig',
all: './all/views/**/*.twig',
views: './all/views/',
dest: './tests/views/',
},
theme: '',
};
if (paths.theme) {
paths.twig.dest = './' + paths.theme + '/views';
}
const manageEnvironment = function(environment) {
environment.addFilter('moment', (date, format, fromNow) => {
if (fromNow) {
date = moment(date, format).fromNow();
} else {
date = moment(date, format);
}
return date;
});
environment.addFilter('numFormatter', number => {
const SI_SYMBOL = [ '', 'k', 'M', 'G', 'T', 'P', 'E' ];
// what tier? (determines SI symbol)
const tier = Math.log10(number) / 3 | 0;
if (tier === 0) {
return number;
}
const suffix = SI_SYMBOL[tier];
const scale = 10 ** (tier * 3);
const scaled = number / scale;
return scaled.toFixed(1) + suffix;
});
environment.addFilter('icon', (icon, type, classlist, hidden = false, title = '', viewbox = '0 0 24 24' ) => {
let source = '';
let html;
hidden = hidden ? hidden = 'true' : hidden = 'false';
type = type.toLowerCase();
if (type === 'svg') {
title = title === '' ? '' : 'alt="' + title + '" ';
} else {
title = title === '' ? '' : 'title="' + title + '" ';
}
// $icon = icon.isArray() ? $this->get_first_icon($icon) : $icon;
if (icon === '') {
return '';
}
if (type === 'iconify') {
source = icon.split(':');
source = source[0];
html = `<span class="iconify o-icon o-icon-src-${source} ${classlist}" ${title} data-icon="${icon}" data-inline="false" aria-hidden="${hidden}"></span>`;
} else if (type === 'inline') {
let item = {};
let db = JSON.parse(fs.readFileSync(paths.data.db));
for (item in db.icons) {
if (db.icons[item].name) {
item = db.icons[item];
if (item.name === icon) {
html = `
<svg class="${classlist}" viewBox="${viewbox}" xmlns="http://www.w3.org/2000/svg" aria-hidden="${hidden}" role="img">
<path fill-rule="evenodd" d="${item.path}" />
</svg>`;
}
}
}
} else if (type === 'svg') {
html = `<img class="o-icon o-icon-svg ${classlist}" ${title}src="../svg/${icon}.svg" aria-hidden="${hidden}">`;
}
return html;
});
};
function data() {
return gulp.src(paths.data.src)
.pipe(merge({
fileName: 'db.json',
edit: (parsedJson, file) => {
parsedJson.version = pkg.version;
return parsedJson;
},
}))
.pipe(gulp.dest(paths.data.dest));
let db = JSON.parse(fs.readFileSync(paths.data.db));
db.version = pkg.version;
}
function twig() {
let db = JSON.parse(fs.readFileSync(paths.data.db));
return gulp.src(paths.twig.src)
.pipe(nunjucks({
data: db,
path: [ paths.twig.views ],
manageEnv: manageEnvironment,
autoescape: false,
}))
/* eslint-enable camelcase */
.pipe(rename({
extname: '.html',
}))
.pipe(gulp.dest(paths.twig.dest));
}
function watchAll() {
gulp.watch(paths.twig.all, gulp.series(twig));
gulp.watch(paths.data.src, gulp.series(data, twig));
}
exports.data = data;
exports.twig = twig;
exports.serve = watchAll;
exports.default = gulp.series(data, twig, watchAll);