-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
executable file
·65 lines (58 loc) · 1.82 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
#!/usr/bin/env node
const fs = require("fs");
const fsp = require("fs").promises;
const path = require("path");
const util = require("util");
const globby = require('globby');
const pascalCase = require("pascalcase");
const getComponentName = name => {
return pascalCase(name)
};
const rootDir = path.join(__dirname);
const svgDir = path.join(rootDir, "./svg");
const iconsDir = path.join(rootDir, "./src/icons");
if (!fs.existsSync(iconsDir)) {
fs.mkdirSync(iconsDir, { recursive: true });
}
// RUN
(async () => {
let icons = {};
let indexContent = "";
try {
const files = await globby(`${svgDir}/*.svg`);
await Promise.all(files.map(async function (file) {
let content = await fsp.readFile(file, 'utf-8')
const svgInner = content.match(/<p(ath|olygon) [^>]+>/)[0];
let name = path.basename(file, '.svg');
icons[name] = svgInner;
}));
await Promise.all(Object.keys(icons).map(async function (name) {
const str = icons[name];
const componentName = getComponentName(name);
indexContent += `export { default as ${componentName}Icon } from './icons/${componentName}.svelte';\n`;
const componentContent = `<script>
export let color = 'currentColor';
export let size = 24;
let className = '';
export { className as class };
</script>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
class={className}
fill={color}
width="{size}"
height="{size}">
${str}
</svg>`;
await fsp.writeFile(path.join(iconsDir, componentName + ".svelte"), componentContent);
}));
} catch (error) {
console.error(error);
}
try {
await fsp.writeFile(path.join(rootDir, "src/index.js"), indexContent);
} catch (error) {
console.error(error);
}
})();