forked from gavro/gulp-iconify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (159 loc) · 6.34 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
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
var gulp = require('gulp');
var iconify = require('./lib/iconify');
var del = require('del');
var svg2png = require('gulp-svg2png');
var sass = require('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 === undefined) {
opts.pngOutput = path.dirname(opts.src)+'/png';
warning.pngOutput = "Info: No pngOutput folder defined. Using fallback ("+opts.pngOutput+").";
}
else if (opts.pngOutput === false) {
warning.pngOutput = "Info: PNG generation has been disabled.";
}
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.scssSvgName) {
opts.scssSvgName = 'icons.svg.scss';
warning.scssSvgName = "Info: No name for svg icons sass file defined. Using fallback ("+opts.scssSvgName+").";
}
if (!opts.scssPngName && opts.pngOutput) {
opts.scssPngName = 'icons.png.scss';
warning.scssPngName = "Info: No name for png icons sass file defined. Using fallback ("+opts.scssPngName+").";
}
if (!opts.scssFallbackName && opts.pngOutput) {
opts.scssFallbackName = 'icons.fallback.scss';
warning.scssFallbackName = "Info: No name for fallback sass file defined. Using fallback ("+opts.scssFallbackName+").";
}
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 = {};
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) {
// var todelete = [];
// if (opts.scssOutput) todelete.push(opts.scssOutput+"/"+opts)
// del([opts.scssOutput+'/icons.*.scss', opts.cssOutput+'/icons.*.css', opts.pngOutput+'/*.png'], cb);
// });
gulp.task('iconify-convert', function() {
var stream = gulp.src(opts.src)
.pipe(iconify({
styleTemplate: opts.styleTemplate,
styleName: opts.scssSvgName,
svgoOptions: opts.svgoOptions,
defaultWidth: opts.defaultWidth,
defaultHeight: opts.defaultHeight
}))
.pipe(gulp.dest(opts.scssOutput));
if (opts.pngOutput) {
stream = gulp.src(opts.src)
.pipe(svg2png(opts.svg2pngOptions.scaling, opts.svg2pngOptions.verbose, opts.svg2pngOptions.concurrency))
.pipe(gulp.dest(opts.pngOutput))
.pipe(iconify({
styleTemplate: opts.styleTemplate,
styleName: opts.scssPngName,
defaultWidth: opts.defaultWidth,
defaultHeight: opts.defaultHeight
}))
.pipe(gulp.dest(opts.scssOutput));
};
return stream;
});
gulp.task('iconify-fallback', ['iconify-convert'], function() {
if (opts.pngOutput === false) return;
var stream = gulp.src(opts.pngOutput+'/*.png')
.pipe(iconify({
styleTemplate: opts.styleTemplate,
styleName: opts.scssFallbackName,
noConvert: true,
cssOutputTarget: opts.cssOutput,
pngOutputTarget: opts.pngOutput
}))
.pipe(gulp.dest(opts.scssOutput));
return stream;
});
gulp.task('iconify-sass', ['iconify-convert', 'iconify-fallback'], function() {
var stream = gulp.src(opts.scssOutput+'/icons.*.scss')
.pipe(sass({
outputStyle: 'compressed'
}))
.pipe(gulp.dest(opts.cssOutput));
return stream;
});
var tasks = [];
tasks.push('iconify-convert');
if (opts.pngOutput) {
tasks.push('iconify-fallback');
}
if (!opts.cssDisabled) tasks.push('iconify-sass');
gulp.task('iconify', tasks, 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');
};