forked from vaadin/vaadin-lumo-styles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
222 lines (201 loc) · 6.23 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
'use strict';
var gulp = require('gulp');
var eslint = require('gulp-eslint');
var htmlExtract = require('gulp-html-extract');
var stylelint = require('gulp-stylelint');
var find = require('gulp-find');
var replace = require('gulp-replace');
var expect = require('gulp-expect-file');
var grepContents = require('gulp-grep-contents');
var clip = require('gulp-clip-empty-files');
var git = require('gulp-git');
gulp.task('lint', ['lint:js', 'lint:html', 'lint:css']);
gulp.task('lint:js', function() {
return gulp.src([
'*.js',
'test/*.js'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError('fail'));
});
gulp.task('lint:html', function() {
return gulp.src([
'*.html',
'demo/**/*.html',
'test/**/*.html'
])
.pipe(htmlExtract({
sel: 'script, code-example code',
strip: true
}))
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError('fail'));
});
gulp.task('lint:css', function() {
return gulp.src([
'*.html',
'demo/**/*.html',
'mixins/**/*.html',
'test/**/*.html'
])
.pipe(htmlExtract({
sel: 'style'
}))
.pipe(stylelint({
reporters: [
{formatter: 'string', console: true}
]
}));
});
gulp.task('version:check', function() {
const expectedVersion = new RegExp('^' + require('./package.json').version + '$');
return gulp.src(['*.html'])
.pipe(htmlExtract({sel: 'script'}))
.pipe(find(/static get version.*\n.*/))
.pipe(clip()) // Remove non-matching files
.pipe(replace(/.*\n.*return '(.*)'.*/, '$1'))
.pipe(grepContents(expectedVersion, {invert: true}))
.pipe(expect({reportUnexpected: true}, []));
});
gulp.task('version:update', function() {
// Should be run from 'preversion'
// Assumes that the old version is in package.json and the new version in the `npm_package_version` environment variable
const oldversion = require('./package.json').version;
const newversion = process.env.npm_package_version;
if (!oldversion) {
throw new 'No old version found in package.json';
}
if (!newversion) {
throw new 'New version must be given as a npm_package_version environment variable.';
}
return gulp.src(['*.html'])
.pipe(replace(oldversion, newversion))
.pipe(gulp.dest('.'))
.pipe(git.add());
});
/* Generate icon formats (font icons and iron-iconset-svg) from source SVGs */
var iconfont = require('gulp-iconfont');
var exec = require('child_process').exec;
var fs = require('fs');
var svgpath = require('svgpath');
var svgmin = require('gulp-svgmin');
gulp.task('icons', function() {
var folder = 'icons/svg/';
var glyphs;
// Optimize the source files
gulp.src(folder + '*.svg')
.pipe(svgmin({
plugins: [{
removeTitle: true
}, {
cleanupNumericValues: {
floatPrecision: 6
}
}, {
convertPathData: {
floatPrecision: 6
}
}]
}))
.pipe(gulp.dest(folder))
.on('finish', function(args) {
// iron-iconset-svg
fs.readdir(folder, function(err, filenames) {
if (err) {
console.error(err);
return;
}
var output = `<!-- NOTICE: Generated with 'gulp icons' -->
<link rel="import" href="version.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-iconset-svg/iron-iconset-svg.html">
<iron-iconset-svg size="1000" name="lumo">
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
`;
filenames.forEach(function(filename) {
// Skip non-svg files
if (filename.indexOf('.svg') === -1) {
return;
}
var content = fs.readFileSync(folder + filename, 'utf-8');
var path = content.match(/<path d="([^"]*)"/);
if (path) {
// var xScale = Math.min(1, fontHeight / glyphWidth);
// var yScale = -1 * Math.min(1, fontHeight / glyphWidth);
// var xTranslate = Math.max(0, (fontHeight - glyphWidth) / 2);
// var yTranslate = -1 * fontAscent * (2 - Math.min(1, fontHeight / glyphWidth));
var newPath = new svgpath(path[1])
// .translate(xTranslate, yTranslate)
.scale(1000/24, 1000/24)
.round(0)
.toString();
var name = filename.replace('.svg', '').replace(/\s/g, '-').toLowerCase();
output += `<g id="${name}"><path d="${newPath}"/></g>\n`;
}
});
output += `</defs>
</svg>
</iron-iconset-svg>
`;
fs.writeFile('icons.html', output, function(err) {
if (err) {
return console.error(err);
}
});
});
// icon font
gulp.src(folder + '*.svg')
.pipe(iconfont({
fontName: 'lumo-icons',
formats: ['woff'],
fontHeight: 1000,
ascent: 850,
descent: 150,
fixedWidth: true,
normalize: true,
}))
.on('glyphs', function(glyphData, options) {
// Store for later use
glyphs = glyphData;
})
.pipe(gulp.dest('.'))
.on('finish', function(args) {
// Generate base64 version of the font
exec('base64 lumo-icons.woff', function(err, stdout, stderr) {
// Write the output to font-icons.html
var output = `<!-- NOTICE: Generated with 'gulp icons' -->
<link rel="import" href="../polymer/lib/elements/custom-style.html">
<link rel="import" href="version.html">
<custom-style>
<style>
@font-face {
font-family: 'lumo-icons';
src: url(data:application/font-woff;charset=utf-8;base64,${stdout.trim()}) format('woff');
font-weight: normal;
font-style: normal;
}
html {
`;
glyphs.forEach(g => {
var name = g.name.replace(/\s/g, '-').toLowerCase();
var unicode = '\\' + g.unicode[0].charCodeAt(0).toString(16);
output += ` --lumo-icons-${name}: "${unicode}";\n`;
});
output += ` }
</style>
</custom-style>
`;
fs.writeFile('font-icons.html', output, function(err) {
if (err) {
return console.error(err);
}
});
// Cleanup
fs.unlink('lumo-icons.woff');
});
});
});
});