-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateIcons.js
74 lines (66 loc) · 1.64 KB
/
generateIcons.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
const SVGO = require('svgo')
const fs = require('fs')
const path = require('path')
const svgo = new SVGO({
plugins: [
{
removeXMLNS: true
},
{
removeDimensions: true
}
]
})
function run() {
const base = './svgs'
const dirs = ['regular', 'solid', 'brands']
dirs.forEach(dirName => {
transformSvgFromDir(path.resolve(base, dirName))
})
}
run()
function transformSvgFromDir(dir) {
fs.readdir(dir, (err, files) => {
files.forEach(fileName => {
if (!/\.svg$/.test(fileName)) return
let componentName = fileName
.replace(/\.svg$/, '')
.replace(/^(.)/, (match, p1) => p1.toUpperCase())
.replace(/\-(.)/g, (match, p1) => p1.toUpperCase())
if (/svgs\/regular\/?$/.test(dir)) {
componentName += 'Regular'
}
transformSvgToVue(path.resolve(dir, fileName), './src/icons', componentName)
})
})
}
function transformSvgToVue(filePath, outputDir, componentName) {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir)
}
fs.readFile(filePath, function (err, data) {
svgo.optimize(data, {path: filePath}).then((ret) => {
const template = `<template>
<w-icon v-bind="$attrs">
${ret.data}
</w-icon>
</template>
<script>
/* This file is auto generated by generateIcon.js */
import WIcon from '../index.js'
export default {
name: '${componentName}',
components: {
WIcon
},
}
</script>
`
fs.writeFile(path.resolve(outputDir, `${componentName}.vue`), template, (err) => {
if (err) {
console.log(err)
}
})
})
})
}