-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.ts
155 lines (147 loc) · 4.63 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import degit from "degit";
import fs from "fs";
import fsAsync from "fs/promises";
import { JSDOM } from "jsdom";
import { exec } from "child_process";
import svgtojsx from "svg-to-jsx";
import svgo from "svgo";
interface ConfigEntry {
name: string;
tags: string[];
versions: {
svg: string[];
font: string[];
};
color: string;
aliases: {
base: string;
alias: string;
}[];
}
(async () => {
const index: [string, string][] = [];
if (fs.existsSync(`${__dirname}/tmp`))
fs.rmSync(`${__dirname}/tmp`, { recursive: true });
if (fs.existsSync(`${__dirname}/dist`))
fs.rmSync(`${__dirname}/dist`, { recursive: true });
fsAsync.mkdir(`${__dirname}/tmp`);
await degit("devicons/devicon").clone(`${__dirname}/tmp/devicon`);
const deviconConfig: ConfigEntry[] = JSON.parse(
(await fsAsync.readFile(`${__dirname}/tmp/devicon/devicon.json`)).toString()
);
await fsAsync.mkdir(`${__dirname}/tmp/dist`);
await fsAsync.copyFile(
`${__dirname}/readme.md`,
`${__dirname}/tmp/dist/readme.md`
);
await fsAsync.copyFile(
`${__dirname}/package.json`,
`${__dirname}/tmp/dist/package.json`
);
await Promise.all(
deviconConfig.map(async (entry) => {
await fsAsync.mkdir(`${__dirname}/tmp/dist/${entry.name}`);
await Promise.all(
entry.versions.svg.map(async (version) => {
const name = `${entry.name}-${version}`;
const icon = await fsAsync.readFile(
`${__dirname}/tmp/devicon/icons/${entry.name}/${name}.svg`
);
const optimizedIcon = svgo.optimize(icon, {
plugins: [
{
name: "inlineStyles",
params: {
onlyMatchedOnce: false,
},
},
"removeStyleElement",
],
}) as svgo.OptimizedSvg;
const { document } = new JSDOM(optimizedIcon.data).window;
const dir = `${__dirname}/tmp/dist/${entry.name}/${version}`;
const reactName =
name
.split("-")
.map((el) => el.charAt(0).toUpperCase() + el.slice(1))
.join("") + "Icon";
index.push([reactName, `./${entry.name}/${version}`]);
await fsAsync.mkdir(dir);
// await fsAsync.writeFile(
// `${dir}/index.js`,
// `export { default } from "./${reactName}"`
// );
const svg = document.getElementsByTagName("svg")[0];
svg.removeAttribute("width");
svg.removeAttribute("height");
svg.removeAttribute("xmlns:xlink");
const isPlain =
version.includes("plain") ||
version.includes("line") ||
!!entry.aliases.find(
(x) =>
x.base == version &&
(x.alias.includes("plain") || x.alias.includes("line"))
);
if (isPlain) {
svg.removeAttribute("fill");
const elements = svg.getElementsByTagName("*");
for (let i = 0; i < elements.length; i++) {
const element = elements[i] as SVGElement;
element.removeAttribute("fill");
element.style.removeProperty("fill");
element.style.removeProperty("fill-rule");
element.style.removeProperty("fill-opacity");
}
}
await fsAsync.writeFile(
`${dir}/index.js`,
`const React = require("react");
module.exports = function ${reactName}({size = "1em", ${
isPlain ? `color = "${entry.color}",` : ""
} ...props}){
props = {
...props,
style: {
...(props.style ? props.style : {}),
width: size,
height: size,${
isPlain
? `
...(color ? { fill: color } : {} )`
: ""
}
}
}
return (${(
await svgtojsx(svg.outerHTML)
).replace("<svg", "<svg {...props}")});
}`
);
const definitions = `import React from "react";
interface Props extends React.SVGProps<SVGElement> {
size?: number | string;${
isPlain
? `
color?: string;`
: ""
}
}
declare const ${reactName}: React.FunctionComponent<Props>;
export default ${reactName};`;
await fsAsync.writeFile(`${dir}/index.d.ts`, definitions);
})
);
})
);
await fsAsync.writeFile(
`${__dirname}/tmp/dist/index.js`,
index.map((e) => `const ${e[0]} = require("${e[1]}")`).join(";\n") +
";\n" +
`module.exports = {${index.map((e) => e[0]).join(",")}}`
);
await fsAsync.writeFile(
`${__dirname}/tmp/dist/index.d.ts`,
index.map((e) => `export { default as ${e[0]} } from "${e[1]}"`).join(";\n")
);
})();