-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (49 loc) · 1.53 KB
/
index.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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const rootPath = process.cwd();
const {
template,
consoleString,
parseSvg2Object,
parseSvg,
getConfigTem,
} = require("./utils");
const downloadFile = require("./download");
const configJSONPath = path.join(rootPath, "./iconfont.json");
const configJSPath = path.join(rootPath, "./iconfont.js");
let config = null;
if (fs.existsSync(configJSONPath)) {
config = require(configJSONPath);
}
if (fs.existsSync(configJSPath)) {
config = require(configJSPath);
}
if (!config) {
throw new Error(
"没查询到当前目录存在config,你可以使用iconfont.json 或者 iconfont.js来配置config"
);
}
if (!config.iconURL) {
throw new Error("无法获取iconURL,请确认iconfont.json文件配置");
}
const toDir = path.join(rootPath, config.save_dir);
async function run() {
if (!fs.existsSync(toDir)) {
fs.mkdirSync(toDir);
}
const res = await downloadFile(config.iconURL);
const mathes_1 = String(res).match(/'<svg>(.+?)<\/svg>'/);
if (!mathes_1[1]) {
throw new Error("无法解析出正确的SVG");
}
consoleString(`开始解析iconfont文件`, "green");
const result = await parseSvg(mathes_1[1]);
const iconObj = parseSvg2Object(result);
const iconStr = getConfigTem(iconObj);
consoleString(`解析完毕,开始生成文件`, "green");
fs.writeFileSync(path.join(toDir, "./index.ets"), template);
fs.writeFileSync(path.join(toDir, "./icon.ets"), iconStr);
consoleString(`生成文件完毕,Success`, "green");
}
run();