-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
125 lines (104 loc) · 2.89 KB
/
generator.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const path = require('path')
const camelize = require('camelize')
const { promisify } = require('util')
const { readFileSync } = require('fs')
const iconSets = require('./config').iconSets
var filndir = require('filendir')
const cheerio = require('cheerio')
const glob = promisify(require('glob'))
const dist = path.resolve('dist')
const _ = require('underscore')
var rimraf = require('rimraf')
var attrs = ['xlink:href', 'clip-path', 'fill-opacity', 'fill']
const template = require('./template')
var cleanAtrributes = function ($el, $) {
_.each(attrs, function (attr) {
$el.removeAttr(attr)
})
if ($el.children().length === 0) {
return false
} //
$el.children().each(function (index, el) {
cleanAtrributes($(el), $)
})
}
//
function writeIcon (iconPath) {
var iconFolder = iconPath.split('/')[3] // Hope this works
var fileName = path.basename(iconPath, '.svg')
var componentName = camelize(`${iconFolder}-${fileName}`)
var destination = path.join(dist, iconFolder, fileName + '.js')
filndir.ws(destination, generateIcon(iconPath, componentName), 'utf-8')
}
function generateIcon (iconPath, componentName) {
var svg = readFileSync(iconPath, 'utf-8')
let $ = cheerio.load(svg, {
xmlMode: true
})
var $svg = $('svg')
cleanAtrributes($svg, $)
var content = $svg.html()
var viewBox = $svg.attr('viewBox')
return template({
componentName,
content,
viewBox
})
}
async function generateIcons (path) {
try {
const icons = await glob(path)
return icons
} catch (error) {
console.log(error)
}
} //
rimraf(dist, () => {
var work = []
var iconCount = 0
for (let set in iconSets) {
work.push(generateIcons(iconSets[set]))
}
var data = []
Promise.all(work)
.then(data => {
for (let set of data) {
let iconFolder = set[0].split('/')[3]
const imports =
set
.map(iconPath => {
let fileName = path.basename(iconPath, '.svg')
let componentName = camelize(`${iconFolder}-${fileName}`)
return `var ${componentName} = require('./${fileName}').default`
})
.join('\n') + '\n'
const exports =
set
.map(iconPath => {
let fileName = path.basename(iconPath, '.svg')
let componentName = camelize(`${iconFolder}-${fileName}`)
return `${componentName},`
})
.join('\n') + '\n'
const indexJS = `
${imports}
module.exports = {
${exports}
}
`
var destination = path.join(dist, iconFolder, 'index.js')
filndir.ws(destination, indexJS, 'utf-8')
for (let icon of set) {
iconCount++
writeIcon(icon)
} //
filndir.ws(
'./docs/iconCount.js',
`export default { iconCount: ${iconCount} }\n`
)
}
})
.catch(err => {
console.log('error', err)
})
})