Skip to content

Commit

Permalink
add style condition
Browse files Browse the repository at this point in the history
  • Loading branch information
李俊 authored and 李俊 committed Nov 7, 2019
1 parent 2a8b791 commit f2ec4d0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 95 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- uses: actions/setup-node@v1
with:
node-version: '10.x'
always-auth: true
- run: yarn install
- name: Figma Action
uses: primer/[email protected]
Expand All @@ -27,7 +28,7 @@ jobs:
FIGMA_TOKEN: ${{ secrets.FIGMA_TOKEN }}
- run: yarn generate
- run: yarn build
- run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}" > ~/.npmrc
# - run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}" > ~/.npmrc
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
18 changes: 10 additions & 8 deletions bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const path = require('path')
const fs = require('fs')
const format = require('prettier-eslint')
const upperCamelCase = require('uppercamelcase')
const processSvg = require('./processSvg')
const style = process.env.npm_package_config_style || 'stroke'
const { parseName } = require('./utils')
const defaultStyle = process.env.npm_package_config_style || 'stroke'
const { getAttrs, getElementCode } = require('./template')
const icons = require('../src/data.json')

Expand Down Expand Up @@ -47,7 +47,7 @@ const generateIndex = () => {
const attrsToString = (attrs) => {
return Object.keys(attrs).map((key) => {
// should distinguish fill or stroke
if (key === 'width' || key === 'height' || key === style) {
if (key === 'width' || key === 'height' || key === 'fill' || key === 'stroke') {
return key + '={' + attrs[key] + '}';
}
if (key === 'otherProps') {
Expand All @@ -59,12 +59,14 @@ const attrsToString = (attrs) => {

// generate icon code separately
const generateIconCode = async ({name}) => {
const location = path.join(rootDir, 'src/svg', `${name}.svg`)
const destination = path.join(rootDir, 'src/icons', `${name}.js`)
const ComponentName = (name === 'github') ? 'GitHub' : upperCamelCase(name)
const names = parseName(name, defaultStyle)
console.log(names)
const location = path.join(rootDir, 'src/svg', `${names.name}.svg`)
const destination = path.join(rootDir, 'src/icons', `${names.name}.js`)
const code = fs.readFileSync(location)
const svgCode = await processSvg(code)
const element = getElementCode(ComponentName, attrsToString(getAttrs(style)), svgCode)
const ComponentName = names.componentName
const element = getElementCode(ComponentName, attrsToString(getAttrs(names.style)), svgCode)
const component = format({
text: element,
eslintConfig: {
Expand All @@ -80,7 +82,7 @@ const generateIconCode = async ({name}) => {
fs.writeFileSync(destination, component, 'utf-8');

console.log('Successfully built', ComponentName);
return {ComponentName, name}
return {ComponentName, name: names.name}
}

// append export code to index.js
Expand Down
85 changes: 0 additions & 85 deletions bin/export.js

This file was deleted.

2 changes: 1 addition & 1 deletion bin/processSvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function processSvg(svg) {
// remove semicolon inserted by prettier
// because prettier thinks it's formatting JSX not HTML
.then(svg => svg.replace(/;/g, ''))
.then(svg = svg.replace(/([a-z]+)-([a-z]+)=/g, (_, a, b) => `${a}${CamelCase(b)}=`))
.then(svg => svg.replace(/([a-z]+)-([a-z]+)=/g, (_, a, b) => `${a}${CamelCase(b)}=`))
.then(removeSVGElement)
return optimized;
}
Expand Down
15 changes: 15 additions & 0 deletions bin/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const upperCamelCase = require('uppercamelcase')

const parseName = (name, defaultStyle) => {
const nameSlices = name.split('-')
const style = nameSlices[nameSlices.length - 1]
return {
name,
componentName: upperCamelCase(name),
style: style==='fill' || style==='stroke' ? style : defaultStyle
}
}

module.exports = {
parseName
};
1 change: 1 addition & 0 deletions src/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit f2ec4d0

Please sign in to comment.