-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.js
executable file
·96 lines (76 loc) · 2.93 KB
/
build.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
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
#!/usr/bin/env node
const fs = require("fs-extra");
const path = require("path");
const { pascalCase } = require("pascal-case");
const babel = require('@babel/core');
const PATH = path.resolve("node_modules/@tabler/icons/icons");
const componentTemplate = (name, svg) =>
`
export default {
name: '${name}',
props: {
size: {
type: [Number, String],
default: 24,
}
},
render() {
const size = this.$props.size + 'px';
const attrs = this.$data.attrs || {};
const allAttrs = {
width: attrs.width || size,
height: attrs.height || size,
}
return ${svg.replace(/<svg([^>]+)>/, "<svg$1 {...allAttrs}>").replace('icon icon-tabler ', 'icon-tabler ')}
}
}
`.trim();
const typingTemplate = `
import { App, FunctionalComponent } from '@vue/runtime-core';
import { SVGAttributes, VNodeProps } from '@vue/runtime-dom';
export declare const Plugin: {
install(app: App, ...options: any[]): any;
};
export declare function install(app: App, ...options: any[]): any;
export default Plugin;
export type TablerIconProps = {
size?: number | string;
}
export type TablerIconComponent = FunctionalComponent<TablerIconProps & SVGAttributes & VNodeProps>;
`.trim();
const aliases = {
"2fa.svg": "two-factor-auth.svg",
"3d-cube-sphere.svg": "threed-cube-sphere.svg",
"3d-cube-sphere-off.svg": "threed-cube-sphere-off.svg",
"3d-rotate.svg": "threed-rotate.svg",
"123.svg": "onetwotree.svg",
"360-view.svg": "deg360-view.svg",
"360.svg": "deg360.svg",
"24-hours.svg": "twenty-four-hours.svg",
};
fs.readdir(PATH, (err, items) => {
let index = [];
let typings = [];
items
.filter((name) => name.endsWith(".svg"))
.forEach((name, pos) => {
process.stdout.write(`Building ${pos}/${items.length}: ` + name.padEnd(42) + '\r');
let content = fs.readFileSync(`${PATH}/${name}`, "utf-8").replace(/\n/gm, " ");
// make name
if (name in aliases) name = aliases[name];
let nameCamel = pascalCase(name.replace(".svg", "")).replace(/_(\d)/g, "$1") + "Icon";
// create and transform component
let component = componentTemplate(nameCamel, content);
const compiled = babel.transform(component, {plugins: ['@vue/babel-plugin-jsx']}).code;
// write icon component
let filePath = path.resolve(`icons/${nameCamel}.js`);
fs.ensureDirSync(path.dirname(filePath));
fs.writeFileSync(filePath, compiled, "utf-8");
index.push(`export { default as ${nameCamel} } from './${nameCamel}.js';`);
typings.push(`export const ${nameCamel}: TablerIconComponent;`);
});
index.push("");
typings.push("");
fs.writeFileSync("./icons/index.js", index.join("\n"), "utf-8");
fs.writeFileSync("./index.d.ts", typingTemplate + "\n\n" + typings.join("\n"), "utf-8");
});