directory-encoder.js
5.17 KB
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
/*global require:true*/
/*global module:true*/
(function(){
"use strict";
var fs = require( 'fs-extra' );
var path = require( 'path' );
var Handlebars = require( 'handlebars' );
var SvgURIEncoder = require( './svg-uri-encoder' );
var PngURIEncoder = require( './png-uri-encoder' );
function DirectoryEncoder( input, output, options ){
var files;
if( typeof input === "string" && fs.lstatSync( input ).isDirectory()){
files = fs.readdirSync( input ).map( function( file ){
return path.join( input, file );
});
} else if( Array.isArray( input ) ){
files = input;
} else {
throw new Error( "Input must be Array of files or String that is a directory" );
}
if( typeof output !== "string" ){
throw new Error( "Output must be file name of desired encoded file" );
}
this.output = output;
this.options = options || {};
this.files = files;
this.prefix = this.options.prefix || ".icon-";
this.options.pngfolder = this.options.pngfolder || "";
this.options.pngpath = this.options.pngpath || this.options.pngfolder;
this.customselectors = this.options.customselectors || {};
this.template = this._loadTemplate( this.options.template );
this.templatePrepend = this.options.templatePrepend || "";
this.templateAppend = this.options.templateAppend || "";
}
DirectoryEncoder.encoders = {
svg: SvgURIEncoder,
png: PngURIEncoder
};
DirectoryEncoder.prototype.encode = function() {
var self = this, seen = {};
// remove the file if it's there
if( fs.existsSync(this.output) ) {
fs.unlinkSync( this.output );
}
if( !fs.existsSync(path.dirname( this.output )) ){
fs.mkdirpSync( path.dirname( this.output ) );
}
// add templatePrepend to the output file
fs.appendFileSync( self.output, self.templatePrepend );
// append each selector
this.files.filter(function( filepath ){
var file = path.basename( filepath ),
extension = path.extname( file );
return extension === ".svg" || extension === ".png";
})
.filter(function( filepath ){
return fs.lstatSync( filepath ).isFile();
})
.forEach(function( filepath ) {
var css, datauri, stats,
file = path.basename( filepath ),
extension = path.extname( file );
self._checkName(seen, file.replace( extension, '' ));
stats = self._stats( filepath );
datauri = self._datauri( filepath );
css = self._css( file.replace( extension, '' ), datauri, stats );
fs.appendFileSync( self.output, css + "\n\n" );
});
// add templateAppend to the output file
fs.appendFileSync( self.output, self.templateAppend );
};
DirectoryEncoder.prototype._css = function( name, datauri, stats ) {
var self = this, width, height;
if( stats ){
width = stats.width;
height = stats.height;
}
this.customselectors = this.customselectors || {};
this.prefix = this.prefix || ".icon-";
if( this.customselectors[ "*" ] ){
this.customselectors[ name ] = this.customselectors[ name ] || [];
var selectors = this.customselectors[ "*" ];
selectors.forEach( function( el ){
var s = name.replace( new RegExp( "(" + name + ")" ), el );
if( self.customselectors[ name ].indexOf( s ) === -1 ) {
self.customselectors[ name ].push( s );
}
});
}
var data = {
prefix: this.prefix,
name: name,
datauri: datauri,
width: width,
height: height,
customselectors: this.customselectors[ name ]
}, css = "";
if( this.template ){
css = this.template( data );
} else {
for( var i in data.customselectors ){
if( data.customselectors.hasOwnProperty( i ) ){
css += data.customselectors[i] + ",\n";
}
}
css += this.prefix + name +
" { background-image: url('" +
datauri +
"'); background-repeat: no-repeat; }";
}
return css;
};
DirectoryEncoder.prototype._stats = function( file ){
var encoder, extension = path.extname( file );
if( typeof DirectoryEncoder.encoders[extension.replace(".", "")] === "undefined" ){
throw new Error( "Encoder does not recognize file type: " + file );
}
encoder = new DirectoryEncoder.encoders[extension.replace(".", "")]( file );
return encoder.stats();
};
DirectoryEncoder.prototype._datauri = function( file ) {
var encoder, extension = path.extname( file );
if( typeof DirectoryEncoder.encoders[extension.replace(".", "")] === "undefined" ){
throw new Error( "Encoder does not recognize file type: " + file );
}
encoder = new DirectoryEncoder.encoders[extension.replace(".", "")]( file );
// TODO passthrough of options is generally a code smell
return encoder.encode( this.options );
};
DirectoryEncoder.prototype._checkName = function( seen, name ) {
if( seen[name] ){
throw new Error("Two files with the same name: `" + name + "` exist in the input directory");
}
seen[name] = true;
};
DirectoryEncoder.prototype._loadTemplate = function( templateFile ) {
var tmpl;
if( !templateFile ) { return false; }
if( fs.existsSync( templateFile ) && fs.lstatSync( templateFile ).isFile() ){
var source = fs.readFileSync( templateFile ).toString( 'utf-8' );
tmpl = Handlebars.compile(source);
} else {
throw new Error( "Template file either doesn't exist or isn't a file" );
}
return tmpl;
};
module.exports = DirectoryEncoder;
}());