Skip to content

Commit

Permalink
✨ feat: support nesting and scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rettend committed Oct 29, 2023
1 parent ddec110 commit e93d201
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"url": "https://rettend.github.io/eemoji/eemoji-config-schema.json"
}
]
}
}

Check failure on line 18 in .vscode/settings.json

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ v0.2.0:

- [ ] Add tests with `vitest`
- [x] Support JSON config files (host json schema on github)
- [ ] Support nesting (conventional commits scopes)
- [x] Support nesting (conventional commits scopes)
- fix: typo ✏️, bug 🐛
- chore: release 🔖, cleanup 🧹, license 📜
- breaking change!: 💥
x breaking change!: 💥
- [x] Add many more emojis
- [ ] Write nice README
6 changes: 5 additions & 1 deletion eemoji.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ export default defineConfig({
format: '{emoji} {type}: {subject}',
emojis: {
fix: '🐛',
chore: '🧹',
chore: {
'.': '🧹',
'release': '🔖',
},
cleanup: '🤢',
docs: '✏️',
breaking: '🚨',
},
})
3 changes: 3 additions & 0 deletions eemojirc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

Check failure on line 2 in eemojirc.json

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
}
14 changes: 12 additions & 2 deletions json/eemoji-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@
"emojis": {
"type": "object",
"additionalProperties": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"additionalProperties": {
"type": "string"
}
}
]
}
}
}
}
}

Check failure on line 25 in json/eemoji-config-schema.json

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import emojis from './emojis.json' assert { type: 'json' }
export interface Config {
format: string
emojis: {
[key: string]: string
[key: string]: string | Record<string, string>
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/emojis.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
{
"fix": "🔧",
"fix": {
".": "🔧",
"typo": "✏️",
"bug": "🐛"
},
"chore": {
".": "🗑️",
"release": "🔖",
"cleanup": "🧹",
"license": "📜",
"deps": "📦"
},
"feat": "",
"docs": "📝",
"test": "🧪",
"refactor": "♻️",
"chore": "🗑️",
"init": "🎉",
"deps": "📦",
"config": "⚙️",
Expand All @@ -14,6 +24,7 @@
"ui": "🪟",
"perf": "",
"text": "💬",
"breaking": "💥",
"revert": "",
"cleanup": "🧹",
"ci": "🦾",
Expand All @@ -23,4 +34,4 @@
"remove": "",
"up": "⬆️",
"down": "⬇️"
}
}

Check failure on line 37 in src/emojis.json

View workflow job for this annotation

GitHub Actions / lint

Newline required at end of file but not found
47 changes: 45 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const main = defineCommand({
const result = await explorer.search()
let config: Config

consola.log(`file: ${result?.filepath}`)
consola.log(`config: ${JSON.stringify(result?.config, null, 2)}`)

if (result)
config = result.config
else
Expand All @@ -53,12 +56,26 @@ export const main = defineCommand({
type = type?.trim()
subject = subject?.trim()

consola.log(`type: ${type}`)
consola.log(`subject: ${subject}`)

if (!type || !subject) {
consola.warn(`Invalid commit message: ${firstLine}`)
throw new Error('Invalid commit message.')
}

const emoji = config.emojis[type.toLowerCase()]
let emoji: string | Record<string, string> | undefined

// if there is an exclamatory mark, then it's a breaking change
if (firstLine.includes('!') && config.emojis.breaking)
emoji = config.emojis.breaking
else
emoji = config.emojis[type.toLowerCase().replace(/\(.*\)/, '')]

// if the emoji is an object, then it's a nested emoji
if (typeof emoji === 'object')
emoji = getNestedEmoji(firstLine, emoji)

if (!emoji)
throw new Error(`Emoji for type "${type}" not found.`)

Expand All @@ -75,7 +92,31 @@ export const main = defineCommand({
},
})

runMain(main)
// get the first nested emoji that is found in the commit message
// example of a config with nested emojis:
// "fix": {
// ".": "🔧",
// "typo": "✏️",
// "bug": "🐛"
// },
// "feat": "✨",
// "docs": "📝",
// "test": "🧪",
// "refactor": "♻️",
// 1. we iterate over the entries of the object and check if the commit message contains any of the keys (without the dot)
// 2. if it does, then we return the value of that key
// 3. if it doesn't, we return the dot key

function getNestedEmoji(text: string, emoji: Record<string, string>) {
const entries = Object.entries(emoji).filter(([key]) => key !== '.')

for (const [key, value] of entries) {
if (text.includes(key))
return value
}

return emoji['.']
}

function checkGitHook() {
const hookContent = '#!/bin/sh\n'
Expand Down Expand Up @@ -125,3 +166,5 @@ function checkJsonSchema() {
else consola.error(err)
}
}

runMain(main)

0 comments on commit e93d201

Please sign in to comment.