-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
49 lines (42 loc) · 1.94 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
const path = require('path')
const unicons = require('@iconscout/unicons/json/line.json')
const {
pascalCase
} = require('pascal-case')
const fs = require('fs-extra')
const cheerio = require('cheerio')
const handleComponentName = name => name.replace(/\-(\d+)/, '$1')
const icons = unicons.map(item => ({
name: item.name,
pascalCasedComponentName: 'Uil' + pascalCase(`${handleComponentName(item.name)}`),
kebabCasedComponentName: `Uil-${handleComponentName(item.name)}`,
iconSvg: item.svg
}))
Promise.all(icons.map(icon => {
const svgFile = fs.readFileSync(path.resolve(`node_modules/@iconscout/unicons/${icon.iconSvg}`), 'utf-8')
let data = svgFile.replace(/<svg[^>]+>/gi, '').replace(/<\/svg>/gi, '')
/* const $ = cheerio.load(data, {
xmlMode: true
})
const svgPath = $('path').attr('d') */
const svgPath = data.replace('<?xml version="1.0" encoding="utf-8"?>','')
let component = `
<script>
export let size = "24";
let customClass = "";
export { customClass as class };
</script>
<svg xmlns="http://www.w3.org/2000/svg" width={size} height={size} fill="currentColor" viewBox="0 0 24 24" class="unicons ${icon.name} {customClass}">${svgPath}</svg>
`
const filepath = `./src/icons/${icon.pascalCasedComponentName}.svelte`
return fs.ensureDir(path.dirname(filepath))
.then(() => fs.writeFile(filepath, component, 'utf8'))
})).then(async () => {
const main = icons
.map(icon => `export { default as ${icon.pascalCasedComponentName} } from './icons/${icon.pascalCasedComponentName}.svelte'`)
.join('\n\n')
const types = '/// <reference types="svelte" />\nimport {SvelteComponentTyped} from "svelte/internal"\n' +
icons.map(icon => `export class ${icon.pascalCasedComponentName} extends SvelteComponentTyped<{size?: string, strokeWidth?: number, class?: string}> {}`).join("\n")
await fs.outputFile("index.d.ts", types, 'utf8');
return await fs.outputFile('./src/index.js', main, 'utf8')
})