From c5a6b90654be3e3d11e22fc8fdc3b4c9d1bc86c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hegyi=20=C3=81ron=20Ferenc?= Date: Tue, 23 Jan 2024 16:14:05 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20strict=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- TODO.md | 11 +++++++---- json/eemoji-config-schema.json | 4 ++++ src/config.ts | 2 ++ src/utils/emoji.ts | 17 ++++++++++++----- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 57ef70b..ede772a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/TODO.md b/TODO.md index 38e6d53..e2ffd7b 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/json/eemoji-config-schema.json b/json/eemoji-config-schema.json index da90447..e712590 100644 --- a/json/eemoji-config-schema.json +++ b/json/eemoji-config-schema.json @@ -10,6 +10,10 @@ "type": "string", "default": "{emoji} {type}: {subject}" }, + "strict": { + "type": "boolean", + "default": true + }, "emojis": { "type": "object", "additionalProperties": { diff --git a/src/config.ts b/src/config.ts index 343389d..1fab3ac 100644 --- a/src/config.ts +++ b/src/config.ts @@ -21,6 +21,7 @@ type EmojiType = StringOrOptionalProp & { breaking?: string } export interface Config { format: string + strict: boolean emojis: EmojiType } @@ -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 diff --git a/src/utils/emoji.ts b/src/utils/emoji.ts index b231958..b859b57 100644 --- a/src/utils/emoji.ts +++ b/src/utils/emoji.ts @@ -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