This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
137 lines (117 loc) · 3.5 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
(function() {
var Mustache, addMeasurements, defaults, encode, fixAppendix, fs, fsp, getTemplate, mkdirp, parseString, path, pmkdirp, readFile, render, spriteName, write, _extend, _merge;
fs = require('fs');
path = require('path');
mkdirp = require('mkdirp');
fsp = require('fs-promise');
Mustache = require('mustache');
parseString = require('xml2js').parseString;
defaults = {
base64: false,
cwd: './',
templateCSS: "" + __dirname + "/templateCSS.mst",
templateSCSS: "" + __dirname + "/templateSCSS.mst",
spriteFileName: 'svg',
style: 'css'
};
pmkdirp = function(dir) {
return new Promise(function(resolve, reject) {
return mkdirp(dir, function(error) {
if (error) {
return reject(error);
}
return resolve(dir);
});
});
};
_extend = function(object, properties) {
var key, val;
for (key in properties) {
val = properties[key];
object[key] = val;
}
return object;
};
_merge = function(options, overrides) {
return _extend(_extend({}, options), overrides);
};
getTemplate = function(options) {
return fsp.readFile(options.style.toLowerCase() === 'scss' ? options.templateSCSS : options.templateCSS, 'utf8');
};
write = function(rendered, cwd, dest) {
return pmkdirp(cwd).then(function() {
return fsp.writeFile("" + cwd + dest, rendered).then(function() {
return rendered;
});
});
};
spriteName = function(options) {
if (options.sprite != null) {
return options.sprite;
}
return options.spriteFileName + '.' + options.style.toLowerCase();
};
render = function(files, template) {
return Mustache.render(template, {
files: files
});
};
fixAppendix = function(string) {
if (string.indexOf("px") === -1) {
return string;
}
return string.slice(0, string.lastIndexOf("px"));
};
addMeasurements = function(file) {
return new Promise(function(resolve, reject) {
return parseString(file.data, (function(_this) {
return function(err, result) {
if (result.svg.$.width && result.svg.$.height) {
file.width = fixAppendix(result.svg.$.width);
file.height = fixAppendix(result.svg.$.height);
}
return resolve(file);
};
})(this));
});
};
encode = function(data, base64) {
if (base64 === true) {
return new Buffer(data).toString('base64');
}
return encodeURIComponent(data);
};
readFile = function(filename, base64) {
return fsp.readFile(filename, 'utf8').then(function(data) {
return {
name: path.basename(filename, '.svg'),
data: data,
encoded: encode(data, base64),
base64: base64
};
}).then(addMeasurements);
};
module.exports = {
defaults: defaults,
encode: function(files, params) {
var file, options;
options = _merge(defaults, params);
return Promise.all((function() {
var _i, _len, _ref, _results;
_ref = [].concat(files);
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
_results.push(readFile(file, options.base64));
}
return _results;
})()).then(function(files) {
return getTemplate(options).then(function(template) {
return render(files, template);
});
}).then(function(rendered) {
return write(rendered, options.cwd, spriteName(options));
});
}
};
}).call(this);