-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
47 lines (42 loc) · 1.02 KB
/
index.ts
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
import axios from 'axios';
import { parseString } from 'xml2js';
import colors from 'colors';
export interface XmlData {
svg: {
symbol: Array<{
$: {
viewBox: string;
id: string;
};
path: Array<{
$: {
d: string;
fill?: string;
};
}>;
}>;
}
}
export const fetchXml = async (url): Promise<XmlData> => {
console.log('Fetching iconfont data...');
try {
const { data } = await axios.get<string>(url);
const matches = String(data).match(/'<svg>(.+?)<\/svg>'/);
if (matches) {
return new Promise<XmlData>((resolve, reject) => {
parseString(`<svg>${matches[1]}</svg>`, { rootName: 'svg' }, (err: Error, result: XmlData) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
throw new Error('You provide a wrong symbol url');
} catch (e) {
console.error(colors.red(e.message || 'Unknown Error'));
process.exit(1);
throw e;
}
};