forked from publitas/svg-to-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (46 loc) · 1.31 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
var readdir = require('recursive-readdir');
var SVG = require('svgo');
var path = require('path');
var fs = require('fs');
var svg = new SVG();
var toReact = require('./src/svg-to-react');
exports.convert = function(svgstring) {
return toReact(svgstring);
};
exports.convertFile = function(filePath, callback) {
fs.readFile(filePath, function(err, content) {
if (err) callback(err);
svg.optimize(content.toString(), function(res) {
callback(null, toReact(res.data));
});
});
};
exports.convertDir = function(dirPath, callback) {
var done = false;
var processing = 0;
var components = {};
function bail(err) {
done = true;
callback(err);
}
function converFile(filePath) {
if (done || path.extname(filePath) !== '.svg') return;
processing++;
fs.readFile(filePath, function(err, content) {
if (err) return bail(err);
svg.optimize(content.toString(), function(res) {
var key = path.relative(dirPath, filePath).replace(/\.svg$/, '');
components[key] = toReact(res.data);
processing--;
if (processing === 0) {
callback(null, components);
}
});
});
}
// the ignore pattern seems buggy
readdir(dirPath, function(err, filePaths) {
if (err) return bail(err);
filePaths.map(converFile);
});
};