forked from mapbox/directions-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (54 loc) · 2.19 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
var fs = require('fs');
var path = require('path');
var mapnik = require('mapnik');
var base = path.join(__dirname, 'src');
var zipdir = require('zip-dir');
var vectorDrawable = require('./lib/vector-drawable');
var colors = [{
name: 'dark',
color: '#000'
}, {
name: 'light',
color: '#fff'
}];
fs.writeFileSync('./icons.md', '### Icons\n\n');
fs.readdir(path.join(base, 'svg'), (err, svgs) => {
if (err) throw err;
svgs.forEach((svgFileName) => {
if (svgFileName.indexOf('.svg') === -1) return false;
fs.readFile(path.join(base, 'svg', svgFileName), (err, data) => {
if (err) throw err;
var iconName = `direction_${svgFileName.split('.svg')[0]}`;
fs.appendFileSync('./icons.md', '* [' + iconName +'](https://github.com/mapbox/directions-icons/blob/master/src/png/dark/' + iconName + '.png)\n');
colors.forEach((color) => {
var replaced = data.toString().replace(/fill="#000000"/g, `fill="${color.color}"`);
var svgBuffer = new Buffer(replaced);
new mapnik.Image.fromSVGBytes(svgBuffer, {
scale: 10
}, (err, mSVG) => {
if (err) throw err;
mSVG.encode('png8:c=128:m=h', (err, image) => {
if (err) throw err;
fs.writeFileSync(path.join(base, 'png', color.name, iconName + '.png'), image);
});
});
try {
var vectorDrawableSVG = vectorDrawable.createFromSVG(replaced).toString();
fs.writeFileSync(path.join(base, 'android', color.name, iconName + '.xml'), vectorDrawableSVG);
} catch (e) {
console.log('Error on ', iconName);
}
});
});
});
zipdir(path.join(base), {
filter: (path, stat) => !/\.zip$/.test(path),
filter: (path, stat) => !/\.DS_Store$/.test(path)
}, (err, buffer)=> {
if (err) throw err;
fs.writeFile(path.join(base, 'icons.zip'), buffer, (err) => {
if (err) throw err;
console.log('Saved zip');
});
});
});