-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
executable file
·45 lines (43 loc) · 1.15 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
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var dom = require('cheerio')
var args = process.argv.slice(2)
var $ = dom.load('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0" style="display:none;"></svg>')
var dir
var fileName
var svgNode
var symbolNode
function parse (file) {
if (path.extname(file) === '.svg') {
fileName = file.slice(0, -4)
file = fs.readFileSync(path.join(dir, file), 'utf8')
svgNode = $(file)
symbolNode = $('<symbol></symbol>')
symbolNode.attr('viewBox', svgNode.attr('viewbox'))
symbolNode.attr('id', fileName)
symbolNode.append(svgNode.contents())
symbolNode
.children()
.each(function (i, kid) {
$(kid)
.removeAttr('fill')
.removeAttr('stroke')
.removeAttr('style')
})
$('svg').append(symbolNode)
}
}
if (args && args.length) {
dir = args[0]
fs.readdir(dir, function (err, files) {
if (err) {
process.stderr.write(err)
return
}
files.forEach(parse)
process.stdout.write($.html())
})
} else {
process.stderr.write('Directory not found.')
}