forked from VictorCazanave/svg-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svgToJs.js
89 lines (76 loc) · 2.19 KB
/
svgToJs.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
const path = require('path')
const fs = require('fs')
const svgson = require('svgson')
/**
* Convert SVG file into JS file
*
* @param {string} dir - Name of directory
* @param {string} file - Name of SVG file
*/
const svgToJs = (dir, file) => {
const svgFile = path.join(__dirname, 'packages', dir, file)
const jsFile = path.join(__dirname, 'packages', dir, 'index.js')
if (fs.existsSync(jsFile)) {
console.log(`File ${jsFile} already exists`)
return
}
fs.readFile(svgFile, 'utf8', (err, data) => {
if (err) {
console.error(`Unable to read file ${svgFile}`, err)
return
}
console.log(`Parsing file ${svgFile}`)
svgson.parse(data)
.then(json => {
const obj = {
label: json.attributes['aria-label'],
viewBox: json.attributes.viewBox,
locations: json.children
.filter(child => {
if (child.name !== 'path') {
console.warn(`<${child.name}> tag will be ignored`)
return false
}
return true
})
.map(child => ({
name: child.attributes.name,
id: child.attributes.id,
path: child.attributes.d,
}))
}
const js = `export default ${JSON.stringify(obj)}`
console.log(`Writing file ${jsFile}`)
fs.writeFile(jsFile, js, 'utf8', err => {
if (err) {
console.error(`Unable to write file ${jsFile}`, err)
return
}
})
}).catch(err => {
console.error(`Unable ton parse file ${svgFile}`, err)
})
})
}
// Read packages directory
fs.readdir(path.join(__dirname, 'packages'), (err, dirs) => {
if (err) {
console.log('Unable to scan packages directory', err)
return
}
// Read all sub directories
dirs.forEach((dir) => {
fs.readdir(path.join(__dirname, 'packages', dir), (err, files) => {
if (err) {
console.log(`Unable to scan directory ${dir}`, err)
return
}
// Convert all SVG files to JS files
files.forEach(file => {
if (path.extname(file) === '.svg') {
svgToJs(dir, file)
}
})
})
})
})