forked from alex-oleshkevich/vue-tabler-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·86 lines (74 loc) · 2.47 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
#!/usr/bin/env node
const fs = require('fs-extra');
const path = require('path');
const { pascalCase } = require('pascal-case');
const PATH = path.resolve('node_modules/@tabler/icons/icons');
const jsxOutDir = './jsx';
const componentTemplate = (name, svg) =>
`
export default {
name: '${name}',
props: {
size: {
type: String,
default: '24',
}
},
functional: true,
render(h, ctx) {
const size = parseInt(ctx.props.size) + 'px';
const attrs = ctx.data.attrs || {};
attrs.width = attrs.width || size;
attrs.height = attrs.height || size;
ctx.data.attrs = attrs;
return ${svg.replace(/<svg([^>]+)>/, '<svg$1 {...ctx.data}>')}
}
}
`.trim();
const pluginTemplate = `
export * from './icons';
import * as components from './icons';
export default {
install(Vue) {
Object
.entries(components)
.forEach(([name, component]) => Vue.component(name, component));
}
}
`.trim();
const typingTemplate = `
import { DefineComponent } from 'vue';
export type TablerIconProps = {
size: string;
}
export type TablerIconComponent = DefineComponent<TablerIconProps, {}, any>;
`.trim();
const aliases = {
'2fa.svg': 'two-factor-auth.svg',
'3d-cube-sphere.svg': 'threed-cube-sphere.svg',
}
fs.readdir(PATH, (err, items) => {
let index = [];
let typings = [];
items
.filter(name => name.endsWith('.svg'))
.forEach(name => {
let content = fs.readFileSync(`${PATH}/${name}`, 'utf-8').replace(/\n/gm, ' ');
if (name in aliases) {
name = aliases[name];
}
let nameCamel = pascalCase(name.replace('.svg', '')).replace(/_(\d)/g, '$1') + 'Icon';
let component = componentTemplate(nameCamel, content);
let filePath = path.resolve(`${jsxOutDir}/${nameCamel}.js`);
index.push(`export { default as ${nameCamel} } from './icons/${nameCamel}.js';`);
typings.push(`export const ${nameCamel}: TablerIconComponent;`);
fs.ensureDir(path.dirname(filePath)).then(() => {
fs.writeFileSync(filePath, component, 'utf-8');
});
});
index.push('');
typings.push('');
fs.writeFileSync('./icons.js', index.join('\n'), 'utf-8');
fs.writeFileSync('./index.js', [pluginTemplate, ''].join('\n'), 'utf-8');
fs.writeFileSync('./index.d.ts', typingTemplate + '\n\n' + typings.join('\n'), 'utf-8');
});