-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
168 lines (140 loc) · 5.38 KB
/
index.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
var gulp = require('gulp');
var iconify = require('./lib/iconify');
var del = require('del');
var svg2png = require('gulp-svg2png');
var sass = require('./lib/custom-gulp-sass');
var gutil = require('gulp-util');
var path = require('path');
var fs = require('fs');
function getErrors(opts) {
var error = {};
if(!opts.src) {
error.src = "Error: src not defined; please specify your source (src: './img/icons/*.svg').";
}
if(Object.keys(error).length) {
Object.keys(error).forEach(function(k) {
gutil.log(error[k]);
});
process.exit();
}
}
function setFallbacks(opts) {
var warning = {};
if(!opts.pngOutput) {
opts.pngOutput = path.dirname(opts.src)+'/png';
warning.pngOutput = "Info: No pngOutput folder defined. Using fallback ("+opts.pngOutput+").";
}
if(opts.cssOutput === undefined) {
opts.cssOutput = './css';
warning.cssOutput = "Info: No cssOutput folder defined. Using fallback ("+opts.cssOutput+").";
} else if (opts.cssOutput === false) {
opts.cssDisabled = true;
warning.cssOutput = "Info: CSS generation has been disabled. CSS files will not be saved.";
}
if(!opts.scssOutput) {
opts.scssOutput = './scss';
opts.scssDisabled = true;
// check if "./scss" exists, if not: remember to remove the folder lateron.
fs.stat(path.normalize(opts.scssOutput), function (err) {
if (err) {
// File doesn't exist - remove scss folder on finish.
opts.scssRemoveDir = true;
}
});
warning.scssOutput = "Info: No scssOutput folder defined. SCSS files will not be saved (temporary files will be saved to '/scss').";
}
if(!opts.styleTemplate) {
opts.styleTemplate = path.join(__dirname, 'lib/output.mustache');
warning.styleTemplate = "Info: No styleTemplate defined. Using default template.";
}
if(!opts.svgoOptions) {
opts.svgoOptions = { enabled: true };
warning.svgoOptions = "Info: No SVGO options defined, enabling SVGO by default.";
}
if(!opts.svg2pngOptions) {
opts.svg2pngOptions = {
options: {},
verbose: false,
concurrency: 8
};
warning.svg2pngOptions = "Info: No svg2png options defined. Using default settings.";
}
if(!opts.defaultWidth) {
opts.defaultWidth = "300px";
warning.defaultWidth = "Info: No defaultWidth defined. Using fallback ("+opts.defaultWidth+") if SVG has no width.";
}
if(!opts.defaultHeight) {
opts.defaultHeight = "200px";
warning.defaultHeight = "Info: No defaultHeight defined. Using fallback ("+opts.defaultHeight+") if SVG has no height.";
}
if(Object.keys(warning).length) {
Object.keys(warning).forEach(function(k) {
gutil.log(warning[k]);
});
}
}
module.exports = function(opts) {
opts = opts || {};
opts.scssDisabled = false;
getErrors(opts);
setFallbacks(opts);
gulp.task('iconify-clean', function(cb) {
del([opts.scssOutput+'/*icons.*.scss', opts.cssOutput+'/*icons.*.css', opts.pngOutput+'/*.png'], cb);
});
gulp.task('iconify-convert', ['iconify-clean'], function() {
gulp.src(opts.src)
.pipe(iconify({
styleTemplate: opts.styleTemplate,
styleName: '_icons.svg.scss',
svgoOptions: opts.svgoOptions,
defaultWidth: opts.defaultWidth,
defaultHeight: opts.defaultHeight
}))
.pipe(gulp.dest(opts.scssOutput));
var stream = gulp.src(opts.src)
.pipe(svg2png(opts.svg2pngOptions.options, opts.svg2pngOptions.verbose, opts.svg2pngOptions.concurrency))
.pipe(gulp.dest(opts.pngOutput))
.pipe(iconify({
styleTemplate: opts.styleTemplate,
styleName: '_icons.png.scss',
defaultWidth: opts.defaultWidth,
defaultHeight: opts.defaultHeight
}))
.pipe(gulp.dest(opts.scssOutput));
return stream;
});
gulp.task('iconify-fallback', ['iconify-clean', 'iconify-convert'], function() {
var stream = gulp.src(opts.pngOutput+'/*.png')
.pipe(iconify({
styleTemplate: opts.styleTemplate,
styleName: '_icons.fallback.scss',
noConvert: true,
cssOutputTarget: opts.cssOutput,
pngOutputTarget: opts.pngOutput
}))
.pipe(gulp.dest(opts.scssOutput));
return stream;
});
gulp.task('iconify-sass', ['iconify-convert', 'iconify-fallback'], function() {
if (opts.cssDisabled) {
return false;
}
var stream = gulp.src(opts.scssOutput+'/_icons.*.scss')
.pipe(sass({
outputStyle: 'compressed'
}))
.pipe(gulp.dest(opts.cssOutput));
return stream;
});
gulp.task('iconify', ['iconify-convert', 'iconify-fallback', 'iconify-sass'], function() {
// remove SCSS folder/files if SCSS output is disabled
if(opts.scssDisabled) {
if(opts.scssRemoveDir) {
del.sync([opts.scssOutput]);
} else {
del.sync([opts.scssOutput+'/_icons.*.scss']);
}
}
});
gulp.start('iconify');
};