-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
build.js
53 lines (44 loc) · 1.59 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
const path = require('path')
const feather = require('feather-icons')
const pascalCase = require('pascal-case')
const fs = require('fs-extra')
const componentTemplate = (name, svg) => `
export default {
name: '${name}',
props: {
size: {
type: String,
default: '24',
validator: (s) => (!isNaN(s) || s.length >= 2 && !isNaN(s.slice(0, s.length -1)) && s.slice(-1) === 'x' )
}
},
functional: true,
render(h, ctx) {
const size = ctx.props.size.slice(-1) === 'x'
? ctx.props.size.slice(0, ctx.props.size.length -1) + 'em'
: 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 handleComponentName = name => name.replace(/\-(\d+)/, '$1')
const icons = Object.keys(feather.icons).map(name => ({
name,
pascalCasedComponentName: pascalCase(`${handleComponentName(name)}-icon`)
}))
Promise.all(icons.map(icon => {
const svg = feather.icons[icon.name].toSvg()
const component = componentTemplate(icon.pascalCasedComponentName, svg)
const filepath = `./src/components/${icon.pascalCasedComponentName}.js`
return fs.ensureDir(path.dirname(filepath))
.then(() => fs.writeFile(filepath, component, 'utf8'))
})).then(() => {
const main = icons
.map(icon => `export { default as ${icon.pascalCasedComponentName} } from '../icons/${icon.pascalCasedComponentName}'`)
.join('\n\n')
return fs.outputFile('./src/index.js', main, 'utf8')
})