Skip to content

Commit

Permalink
Use @mdi/js to generate the icons.
Browse files Browse the repository at this point in the history
  • Loading branch information
leMaik committed Aug 22, 2018
1 parent 97993b3 commit 6e33df3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 56 deletions.
43 changes: 9 additions & 34 deletions generate-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,22 @@ const fse = require('fs-extra')
const path = require('path')
const pascalCase = require('pascal-case')
const babel = require('babel-core')
const xml2js = require('xml2js')
const pick = require('lodash.pick')

;(async () => {
const mdiSvgPath = path.join(path.dirname(require.resolve('@mdi/svg/meta.json')), 'svg')
const icons = await Promise.all(require('@mdi/svg/meta.json').map(async (icon) => {
const xml = await new Promise((resolve, reject) => {
xml2js.parseString(fse.readFileSync(path.join(mdiSvgPath, `${icon.name}.svg`), 'utf8'), (err, xml) => {
if (err) {
reject(err)
} else {
resolve(xml)
}
})
})

if (Object.keys(xml.svg).length > 2) throw Error(`The SVG of icon ${icon.name} contains unknown children`)
const svg = xml.svg.path.map((child) => {
return new xml2js.Builder({ headless: true }).buildObject({ path: child })
}).join('').trim()

if (svg.length === 0) {
throw Error(`Unexpected number of paths (${xml.svg.path.length}) for ${icon.name}`)
}
return {
name: pascalCase(icon.name),
svg
}
}))
const iconFiles = fse.readdirSync(mdiSvgPath)
if (iconFiles.length !== icons.length) {
console.warn(`${icons.length} icons specified in meta.json but ${iconFiles.length} svg files found`)
}
const icons = Object.entries(require('@mdi/js'))
.filter(([name]) => name.indexOf('mdi') === 0)
.map(([name, path]) => ({
name: pascalCase(name.substr(3)), // remove mdi prefix
svgPath: path
}))

fse.removeSync(path.join(__dirname, 'package'))
fse.mkdirpSync(path.join(__dirname, 'package'))

for (const { name, svg } of icons) {
const code = `import React from 'react'
import createIcon from './util/createIcon'
export default createIcon(<g>${svg}</g>)
for (const { name, svgPath } of icons) {
const code = `import createIcon from './util/createIcon'
export default createIcon('${svgPath}')
`

// commonjs module syntax
Expand Down
29 changes: 10 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"devDependencies": {
"@kadira/storybook": "^2.21.0",
"@material-ui/core": "^1.0.0-rc.0",
"@mdi/svg": "^2.5.94",
"@mdi/js": "^2.6.95",
"ava": "^0.25.0",
"babel-core": "^6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1",
Expand Down
4 changes: 2 additions & 2 deletions src/util/createIcon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react'
import SvgIcon from '@material-ui/core/SvgIcon'

export default (svg) => {
const Icon = (props) => <SvgIcon {...props}>{svg}</SvgIcon>
export default (path) => {
const Icon = (props) => <SvgIcon {...props}><path d={path} /></SvgIcon>
Icon.muiName = 'SvgIcon'
return Icon
}

0 comments on commit 6e33df3

Please sign in to comment.