Skip to content

Commit

Permalink
feat(bezier-icons): add changeset generating job to workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
yangwooseong committed Oct 15, 2023
1 parent 18e9213 commit bc9cb03
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/generate-icon-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ jobs:
run: echo "pull_request_number=$(gh pr view --json number -q .number || echo "")" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Add pr description
if: ${{ steps.generate-svg-files.outputs.icon_not_changed == 0}}
run: |
node packages/bezier-icons/scripts/add-pr-description.js ${{ secrets.GITHUB_TOKEN }} ${{ steps.pr.outputs.pull_request_number }}
- name: Add changeset
if: ${{ steps.generate-svg-files.outputs.icon_not_changed == 0}}
run: |
node packages/bezier-icons/scripts/generate-changeset.js
git add .
git commit -m "chore(bezier-icons): add changeset"
- name: Delete icons.json files
run: |
git rm packages/bezier-icons/icons.json
Expand Down
12 changes: 12 additions & 0 deletions packages/bezier-icons/scripts/generate-changeset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { exec } = require('child_process')
const fs = require('fs')
const path = require('path')

const { getChangeLog } = require('./utils/getChangeLog')

const changesetPath = path.resolve(__dirname, `../../../.changeset/icon-update-${new Date().valueOf()}.md`)

exec('git log -1 --name-status --pretty="format:"', async (_undefined, stdout) => {
fs.writeFileSync(changesetPath, getChangeLog(stdout), 'utf-8')
})

60 changes: 60 additions & 0 deletions packages/bezier-icons/scripts/utils/getChangeLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const statusByKey = {
M: '**CHANGE**',
A: '**ADD**',
D: '**DELETE**',
}

const getIconName = (path) => path.split('/').at(-1)

const getIconChangeLog = (iconsByStatus) => {
let res = ''

for (const [key, icons] of Object.entries(iconsByStatus)) {
res += statusByKey[key]
res += '\n\n'
res += icons.map(icon => `- ${icon}`).join('\n')
res += '\n\n'
}

return res
}

const getIconNamesByStatus = (gitLog) => gitLog
.trim()
.split('\n')
.map(line => line.split('\t'))
.filter(line => line[1].endsWith('.svg'))
.reduce((acc, cur) => {
const [key, file] = cur
const icon = getIconName(file)

if (!icon) { return acc }

if (!acc[key]) {
acc[key] = [icon]
} else {
acc[key].push(icon)
}
return acc
}, {})

const getChangeLog = (gitLog) => {
const iconsByStatus = getIconNamesByStatus(gitLog)

let description =
`---
"@channel.io/bezier-icons": minor
---
Update icons
`

description += getIconChangeLog(iconsByStatus)

return description.trim()
}

module.exports = {
getChangeLog,
}

0 comments on commit bc9cb03

Please sign in to comment.