This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
grunticon-lib.js
268 lines (223 loc) · 7.59 KB
/
grunticon-lib.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*global require:true*/
/*global module:true*/
/*global __dirname:true*/
/*global console:true*/
(function(){
"use strict";
var path = require( "path" );
var os = require( "os" );
var fs = require( "fs-extra" );
var uglify = require("uglify-js");
var jsp = uglify;
var DirectoryColorfy = require( "directory-colorfy" );
var DirectoryEncoder = require( "directory-encoder" );
var svgToPng = require( "svg-to-png" );
var _ = require( "lodash" );
var loadCSSpath = require.resolve( "fg-loadcss" );
var helper = require( "./grunticon-helper.js" );
var defaults = {
datasvgcss: "icons.data.svg.css",
datapngcss: "icons.data.png.css",
urlpngcss: "icons.fallback.css",
previewhtml: "preview.html",
loadersnippet: "grunticon.loader.js",
cssbasepath: path.sep,
customselectors: {},
cssprefix: ".icon-",
defaultWidth: "400px",
defaultHeight: "300px",
colors: {},
dynamicColorOnly: false,
pngfolder: "png",
pngpath: "",
template: "",
tmpPath: os.tmpdir(),
tmpDir: "grunticon-tmp",
previewTemplate: path.join( __dirname, "..", "static", "preview.hbs" ),
compressPNG: false,
optimizationLevel: 3,
enhanceSVG: false,
corsEmbed: false,
verbose: true,
logger: {
verbose: console.info,
fatal: console.error,
ok: console.log
}
};
var disabledLogger = {
verbose: function() {},
fatal: console.error,
ok: function() {}
};
var Grunticon = function(files, output, config){
config = config || {};
if( !Array.isArray( files ) ){
throw new Error( "The first argument passed into the Grunticon constructor must be an array of files" );
}
if( typeof output !== "string" ){
throw new Error( "The second argument passed into the Grunticon constructor must be a string, a path to an output directory" );
}
this.files = files;
this.output = output;
this.options = _.defaultsDeep( config, defaults );
this.logger = this.options.verbose ? this.options.logger : disabledLogger;
};
Grunticon.generateLoader = function( opts ){
opts = opts || {};
var enhanceSVG = opts.enhanceSVG || false;
var corsEmbed = opts.corsEmbed || false;
var logger = opts.verbose || defaults.verbose ? (opts.logger || defaults.logger) : disabledLogger;
var files = {
loader: path.join( __dirname, "..", "static", "grunticon.loader.js"),
embed: path.join( __dirname, "..", "static", "grunticon.embed.js"),
corsEmbed: path.join( __dirname, "..", "static", "grunticon.embed.cors.js"),
banner: path.join( __dirname, "..", "static", "grunticon.loader.banner.js")
};
var min = [];
// minify the source of the grunticon loader and write that to the output
logger.verbose( "grunticon now minifying the stylesheet loader source." );
var banner = fs.readFileSync( files.banner ).toString( "utf-8" );
var loadCSS = fs.readFileSync( loadCSSpath ).toString( "utf-8" );
var onloadCSS = fs.readFileSync( path.dirname(loadCSSpath) + "/onloadCSS.js" ).toString( "utf-8" );
var loaderContents = fs.readFileSync( files.loader ).toString( "utf-8" );
var embedContents, corsEmbedContents;
min = min.concat( "(function(){" );
min = min.concat( loadCSS );
min = min.concat( onloadCSS );
min = min.concat( loaderContents );
if( enhanceSVG ){
embedContents = fs.readFileSync( files.embed ).toString( "utf-8" );
min = min.concat( embedContents );
if( corsEmbed ){
corsEmbedContents = fs.readFileSync( files.corsEmbed ).toString( "utf-8" );
min = min.concat( corsEmbedContents );
}
}
min = min.concat( "})();" );
var code = min.join( "\n" );
var compressed = jsp.minify(code).code;
var ret = banner + "\n" + compressed;
return ret;
};
Grunticon.prototype.process = function(cb){
// folder name (within the output folder) for generated png files
var config = this.options;
var logger = this.logger;
var pngfolder = path.join.apply( null, config.pngfolder.split( path.sep ) );
// create the output directory
fs.mkdirpSync( this.output );
// minify the source of the grunticon loader and write that to the output
var loader = config.min = Grunticon.generateLoader({
enhanceSVG: config.enhanceSVG,
corsEmbed: config.corsEmbed,
logger: logger
});
if( config.loadersnippet === false ) {
} else {
fs.writeFileSync( path.join( this.output, config.loadersnippet ), loader );
logger.verbose( "grunticon loader file created." );
}
var svgToPngOpts = {
defaultWidth: config.defaultWidth,
defaultHeight: config.defaultHeight,
compress: config.compressPNG,
optimizationLevel: config.optimizationLevel,
debug: config.verbose
};
var o = {
pngfolder: pngfolder,
pngpath: config.pngpath,
customselectors: config.customselectors,
template: config.template ? path.resolve( config.template ) : "",
previewTemplate: path.resolve( config.previewTemplate ),
noencodepng: false,
prefix: config.cssprefix
};
var o2 = JSON.parse(JSON.stringify(o)); /* clone object */
o2.noencodepng = true;
logger.verbose("Coloring SVG files");
// create the tmp directory
var tmp = path.join( config.tmpPath, config.tmpDir );
if( fs.existsSync( tmp ) ){
fs.removeSync( tmp );
}
fs.mkdirpSync( tmp );
var dc;
try{
dc = new DirectoryColorfy( this.files, tmp, {
colors: config.colors,
dynamicColorOnly: config.dynamicColorOnly
});
} catch( e ){
logger.fatal(e);
cb( false );
}
dc.convert()
.then(function(){
//copy non color config files into temp directory
var transferFiles = this.files.filter( function( f ){
return !f.match( /\.colors/ );
});
transferFiles.forEach( function( f ){
var filename = path.basename(f);
fs.copySync( f, path.join( tmp, filename ) );
});
logger.verbose("Converting SVG to PNG");
var tmpFiles = fs.readdirSync( tmp )
.map( function( file ){
return path.join( tmp, file );
});
var svgFiles = tmpFiles.filter( function( file ){
return path.extname( file ) === ".svg";
}),
pngFiles = tmpFiles.filter( function( file ){
return path.extname( file ) === ".png";
});
pngFiles.forEach(function( f ){
var filename = path.basename(f);
fs.copySync( f, path.join( this.output, pngfolder, filename ) );
}, this);
// svg-to-png requires a non-empty input array; in such a case skip it and use
// a dummy thenable.
(
svgFiles.length ?
svgToPng.convert( svgFiles, path.join( this.output, pngfolder ), svgToPngOpts ) :
{then: function (callback) {callback();}}
).then( function( result , err ){
if( err ){
logger.fatal( err );
cb( false );
}
var pngs = fs.readdirSync( path.join( this.output, pngfolder ) )
.map(function( file ){
return path.join( this.output, pngfolder, file );
}, this);
var svgde = new DirectoryEncoder( tmpFiles, path.join( this.output, config.datasvgcss ), Object.assign( {}, o, { forcedatauri: true } ) ),
pngde = new DirectoryEncoder( pngs, path.join( this.output, config.datapngcss ), o ),
pngdefall = new DirectoryEncoder( pngs, path.join( this.output, config.urlpngcss ), o2 );
logger.verbose("Writing CSS");
try {
svgde.encode();
pngde.encode();
pngdefall.encode();
} catch( e ){
logger.fatal( e );
cb( false );
}
logger.verbose( "Grunticon now creating Preview File" );
try {
helper.createPreview( tmp, this.output, config );
} catch(er) {
logger.fatal(er);
cb( false );
}
logger.verbose( "Delete Temp Files" );
fs.removeSync( tmp );
logger.ok( "Grunticon processed " + this.files.length + " files." );
cb();
}.bind( this ), logger.fatal);
}.bind( this ));
};
module.exports = Grunticon;
}(typeof exports === 'object' && exports || this));