Skip to content

Commit

Permalink
πŸ’₯ remove!: support for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rettend committed Jan 26, 2024
1 parent 246777e commit 313a789
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,28 +249,27 @@ This is useful for conventional commit scopes, but you can include the subtype a
Notes:

- the `'.'` is the fallback subtype
- specify multiple aliases for a type by separating them with pipes: `feat|feature`
- specify multiple emojis by separating them with commas and a **random** one will be chosen: `πŸ’Ž,πŸ’²,πŸ’Έ,πŸ’°`

```ts
import { defineConfig } from 'eemoji'

export default defineConfig({
emojis: {
'fix': {
fix: {
'.': 'πŸ”§',
'typo': '✏️',
'bug': 'πŸ›'
},
'chore': {
chore: {
'.': 'πŸ—‘οΈ',
'release': 'πŸ”–',
'cleanup': '🧹',
'license': 'πŸ“œ',
'deps': 'πŸ“¦'
},
'feat|feature': '✨',
'bounty': 'πŸ’Ž,πŸ’²,πŸ’Έ,πŸ’°'
feat: '✨',
bounty: 'πŸ’Ž,πŸ’²,πŸ’Έ,πŸ’°'
}
})
```
Expand Down
3 changes: 2 additions & 1 deletion eemoji.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineDefaultConfig } from 'eemoji'
/* eslint-disable antfu/no-import-dist */
import { defineDefaultConfig } from './dist/index.mjs'

export default defineDefaultConfig({
emojis: {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ export default defineCommand({
if (ctx.args.test) {
let initial = createExampleCommitMessage(config)

if (fs.existsSync(ctx.args.commit_file))
if (fs.existsSync(ctx.args.commit_file)) {
initial = unemojify(fs.readFileSync(ctx.args.commit_file, 'utf-8'), config)
initial = initial.split('\n')[0] ?? ''
}

commitMessage = await consola.prompt('Commit message:', {
placeholder: 'enter a commit message for testing...',
Expand Down
12 changes: 6 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ const entryDir = path.dirname(fileURLToPath(
),
))

type PipePropName<T> = T extends `${infer First}|${infer _}` ? First : T

type StringOrOptionalProp<T> = {
[K in keyof T as K extends 'breaking' ? never :
PipePropName<K>]: T[K] | Record<string, string>
K]: T[K] | Record<string, string>
}

type EmojiType = StringOrOptionalProp<typeof emojis> & { breaking: string }

export type EmojiConfig = Record<string, string | Record<string, string>>

type DefineConfig = Partial<{
Expand Down Expand Up @@ -119,14 +118,15 @@ export function defineConfig(config: DefineConfig): Config {

export function defineDefaultConfig(config: DefineConfig): Config {
const defaultConfig = new ConfigObject().defaultConfig
const emojis = mergeEmojis(config.emojis)
const { emojis, ...rest } = config
const mergedEmojis = mergeEmojis(emojis)

return merge({}, defaultConfig, config, { emojis })
return merge({}, defaultConfig, rest, { emojis: mergedEmojis })
}

function mergeEmojis(emojis: DefineConfig['emojis']): EmojiConfig {
if (isArray(emojis))
return emojis.reduce((acc, cur) => merge(acc, cur), {})
return merge({}, ...emojis)

return emojis || {}
}
4 changes: 2 additions & 2 deletions src/eemoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function getEmoji(type: string, text: string, config: Config, DEBUG?: number): s
return config.emojis.breaking

type = type.toLowerCase().replace(/\(.*\)/, '').trim()
const emojiKey = Object.keys(config.emojis).find(key => key.split('|').includes(type))
const emojiKey = Object.keys(config.emojis).find(key => key.includes(type))

if (!emojiKey)
return undefined
Expand All @@ -81,7 +81,7 @@ function getEmoji(type: string, text: string, config: Config, DEBUG?: number): s
text = text.toLowerCase()

for (const [key, value] of entries) {
if (key.split('|').some(k => text.includes(k.toLowerCase())))
if (text === key.toLowerCase())
return value
}

Expand Down
2 changes: 1 addition & 1 deletion src/presets/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"enhance": "πŸ’Ž",
"test": "πŸ§ͺ",
"refactor": "♻️",
"init|initial": "πŸŽ‰",
"init": "πŸŽ‰",
"perf": "⚑",
"breaking": "πŸ’₯",
"ci": "🦾",
Expand Down
2 changes: 1 addition & 1 deletion src/presets/default.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"enhance": "πŸ’Ž", // made something a little better (omit from release notes)
"test": "πŸ§ͺ", // worked on tests
"refactor": "♻️", // refactored code, achieved the same with less
"init|initial": "πŸŽ‰", // started a new project!
"init": "πŸŽ‰", // started a new project!
"perf": "⚑", // improved performance, achieved the same faster
"breaking": "πŸ’₯", // *special type:* will be used if the commit contains an exclamation mark (`!`), indicates breaking changes
"ci": "🦾", // changed workflow files, CI stuff
Expand Down

0 comments on commit 313a789

Please sign in to comment.