-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvgmake.js
More file actions
71 lines (70 loc) · 1.99 KB
/
svgmake.js
File metadata and controls
71 lines (70 loc) · 1.99 KB
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
const fs = require('fs')
const path = require('path')
const filePath = `${path.resolve('app')}/components/icontsvg`
const readPath = `${path.resolve('fontsvg')}/iconfont.js`
// 判断文件夹是否存在
const isDirectory = () => {
return new Promise((resolve) => {
fs.access(filePath, (err) => {
if (err) {
resolve('faild')
}
resolve('ok')
})
})
}
// 创建文件夹
const creactMkdir = () => {
return new Promise((resolve) => {
fs.mkdir(filePath, (err) => {
if (err) { return }
resolve(true)
})
})
}
const writeSvg = () => {
console.log('读取处理中...')
fs.readFile(`${readPath}`, 'utf8', (err, data) => {
if (err) {
throw err
} else {
const svgArr = data.split('</symbol>')
svgArr.pop()
let svgTmp = ''
let viewBox = ''
svgArr.forEach((svg) => {
const tmpPath = svg.split('<path')
const viewBoxKey = tmpPath[0].match('id=.*"')[0].split('"')[1].replace('icon-', '').replace(/-(\w)/g, x => x.slice(1).toUpperCase())
const viewBoxValue = tmpPath[0].match('viewBox=.*"')[0].split('"')[1]
if (viewBoxValue !== '0 0 1024 1024') {
viewBox[viewBoxKey] = viewBoxValue
viewBox += ` ${viewBoxKey}: '${viewBoxValue}',\n`
}
const pathArr = []
tmpPath.shift()
tmpPath.forEach((paths) => {
pathArr.push(`{${paths.replace(/=/g, ': ').replace('fill', ', fill').replace('></path>', '')}}`)
})
svgTmp += ` ${viewBoxKey}: [${pathArr}],\n`
})
const svgStr = `export default {\n${svgTmp}\n}`
fs.writeFile(`${filePath}/iconfont.svg.js`, svgStr, 'utf8', () => {
if (err) return
console.log('写入成功')
})
}
})
}
async function WriteFontSvg() {
const directory = await isDirectory()
if (directory === 'ok') {
writeSvg()
} else {
const creact = await creactMkdir()
if (creact) {
console.log('创建成功')
writeSvg()
}
}
}
WriteFontSvg()