Skip to content

Commit

Permalink
✨ feat: strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Rettend committed Jan 23, 2024
1 parent d7c7653 commit c5a6b90
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ The default configuration is here: [emojis.json](./src/emojis.json) and the [Emo

This is used if no config file is found in the project.

Apart from emojis, the config also specifies the format of the commit message, see [Format](#format).
Apart from emojis, the config also specifies other things, like:

- `format`: the format of the commit message, see [Format](#format).
- `strict`: enforce formatting, and only allow commits with emojis (default: `true`)

`eemoji` can be configured two different ways: [json](#json-config) and [typescript](#ts-config) config files.

Expand Down
11 changes: 7 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# 🎯 TODO

## Backlog

- [ ] 2 more options: (any combination of these should work)
- [ ] `conventional (boolean) [true]` - only allow conventional commits, this package should work without conventional commits as well
- [ ] `overwrite (boolean) [false]` - overwrite the default emojis with the ones specified in the config (import the default emojis, import other presets)

## v2

- [x] Add e2e tests with github actions (for all operating systems)
- [x] Upgrade to unbuild `3.0.0` (currently errors), fix `masquerading as CJS` error [(see here)](https://arethetypeswrong.github.io/?p=eemoji)
- [x] Upgrade antfu/eslint-config to `2.0.0` and make it work
- [ ] 2 more options: (any combination of these should work)
- [ ] `strict (boolean) [false]` - do not allow any commits without emojis
- [ ] `conventional (boolean) [true]` - only allow conventional commits, this package should work without conventional commits as well
- [ ] if emoji prop is specified, overwrite, else use default + you can import the default emojis to append them
- [x] `strict (boolean) [false]` - do not allow any commits without emojis
- [x] add `-v` alias for `--version`
- [x] make the consola start and success logs sane in the cleanup command
- [x] investigate speed, prepare script for init
Expand Down
4 changes: 4 additions & 0 deletions json/eemoji-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
"type": "string",
"default": "{emoji} {type}: {subject}"
},
"strict": {
"type": "boolean",
"default": true
},
"emojis": {
"type": "object",
"additionalProperties": {
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type EmojiType = StringOrOptionalProp<typeof emojis> & { breaking?: string }

export interface Config {
format: string
strict: boolean
emojis: EmojiType
}

Expand All @@ -37,6 +38,7 @@ export type JsonFiles = typeof ConfigObject.prototype.jsonFiles[number]
export class ConfigObject {
defaultConfig = {
format: '{emoji} {type}: {subject}',
strict: true,
emojis,
} satisfies Config

Expand Down
17 changes: 12 additions & 5 deletions src/utils/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,24 @@ export function eemojify(text: string, config: Config, DEBUG?: number): string {
consola.log(`subject: "${subject}"`)
}

if (!type || !subject)
throw new Error('Invalid commit message.')
if (!type || !subject) {
if (config.strict)
throw new Error(`Invalid commit message. Expected format: "${config.format}"`)
else
return text
}

let emoji = getEmoji(type, text, config, DEBUG)
consola.log('emoji')

if (DEBUG)
consola.log(`emoji: "${emoji}"`)

if (!emoji)
throw new Error(`Emoji for type "${type}" not found.`)
if (!emoji) {
if (config.strict)
throw new Error(`Emoji for type "${type}" not found.`)
else
return text
}

if (emoji.includes(','))
emoji = emoji.trim().split(',')[Math.floor(Math.random() * emoji.split(',').length)] ?? emoji
Expand Down

0 comments on commit c5a6b90

Please sign in to comment.