-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
70 lines (57 loc) · 1.82 KB
/
index.mjs
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
import { writeFile } from 'node:fs/promises';
import { request } from 'node:https';
const config = {
url: 'https://at.alicdn.com/t/c/font_4622387_tu0pg1aur2b.js',
savePath: './demo/icons.tsx',
// 是否是多色图标 is colorful icon
isColorFul: false,
// 是否保留 svg 标签 is keep svg tag
keepSVG: false,
}
function download(callback) {
const req = request(config.url, (res) => {
let data = '';
res.on('data', (d) => {
data += d.toString();
});
res.on('end', () => {
callback({ svgList: formatResponse(data) })
})
});
req.on('error', (e) => {
console.error(e.message);
});
req.end();
}
function formatResponse(content = '') {
const symbolStrList = content.match(/(\<symbol).*(\<\/symbol\>)/g)?.[0].split('</symbol>').filter(Boolean).map(item => item += '</symbol>');
if (!symbolStrList?.length) {
console.log(content);
console.log('fail');
return {};
}
const symbols = {};
symbolStrList.forEach(item => {
const id = item.match(/id="([\w-]+)"/)[1];
if (id) {
if(config.keepSVG) {
item = item.replaceAll('symbol', 'svg');
} else {
item = `<>${item.replace(/(\<symbol [^\>]*\>)|(\<\/\s*symbol\>)/g, '')}</>`;
}
if (!config.isColorFul) {
item = item.replace(/fill="[^"]+"/g, "");
}
symbols[id] = item;
} else {
console.log('ERROR -> ', item)
}
});
return symbols;
}
download(async ({ svgList }) => {
await writeFile(config.savePath, `export default {
${Object.entries(svgList).map(([key, value]) => `'${key}': ${value}`)}
}`);
console.log('Success:', Object.keys(svgList).length);
});