Skip to content

Commit

Permalink
✨ feat: presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Rettend committed Jan 24, 2024
1 parent 446f0ba commit f81edc4
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/json.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [main]
paths:
- src/config.ts
- src/emojis.jsonc
- src/presets/default.jsonc

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
44 changes: 40 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ eemoji run

## 🦾 Config

The default configuration is here: [emojis.jsonc](./src/emojis.jsonc) and the [Emojis](#-emojis) section.
The default configuration is here: [default.jsonc](./src/presets/default.jsonc) and the [Emojis](#-emojis) section.

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

Expand Down Expand Up @@ -339,17 +339,53 @@ If it continues to fail to work, try this as well and restart everything:

### Adding a new emoji

To add a new emoji to the default config or modify it, please open an issue first.
To add a new emoji to a preset (even the default config) or modify it, open an issue first.

Then, if it's okay:

- modify the [emojis.jsonc](./src/emojis.jsonc) file, add the emoji and a description
- modify the [default.jsonc](./src/presets/default.jsonc) file, add the emoji and a description
- The Action will take care of the rest (copying it to the json, updating the readme emoji table and the json schema)
- open a PR

### Creating a new emoji preset

SOON™️
Add a new `jsonc` file to the **presets** folder and name it as you wish.
Describe the emoji using comments.

Example:

```json
{
"fix": "🔧", // general fix
"feat": "" // introduced a new feature
}
```

Locate the [presets.ts](./src/presets.ts) file and add your preset like this:

```ts
export const [
presetDefault,
presetMinimal,
/* ... */

// add your presetSomething here
] = createPresets([
'default.jsonc',
'minimal.jsonc',
/* ... */

// make sure the path is relative to the root of the project
]) as [
Preset,
Preset,
/* ... */

// and make TypeScript happy
]
```

Then, open a PR.

### Development

Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
- [x] make the consola start and success logs sane in the cleanup command
- [x] investigate speed, prepare script for init
- [x] move bin scripts to `scripts` folder and also build them from typescript
- [x] Generate the readme emoji table from `emojis.jsonc` (jsonc for description)
- [ ] overwrite the default emojis in ts config too (import the default emojis, import other presets)
- [x] Generate the readme emoji table from `default.jsonc` (jsonc for description)
- [x] overwrite the default emojis in ts config too (import the default emojis, import other presets)
- [ ] try astro and create a small website explaining which emoji to use and when, + stuff
4 changes: 1 addition & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import antfu from '@antfu/eslint-config'

export default antfu({
ignores: ['src/**/*.jsonc'],
})
export default antfu()
11 changes: 5 additions & 6 deletions scripts/readme-table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'node:fs'
import process from 'node:process'
import path from 'node:path'
import type {
CommentArray,
CommentDescriptor,
Expand All @@ -9,17 +8,17 @@ import type {
} from 'comment-json'
import { parse } from 'comment-json'
import { markdownTable } from 'markdown-table'
import emojis from '../src/emojis.json'
import emojis from '../src/presets/default.json'
import { r } from '../src/utils/utils'

const cwd = process.env.INIT_CWD || process.cwd()
const eemojiPath = path.join(cwd, 'src/emojis.jsonc')
const readmePath = path.join(cwd, 'README.md')
const eemojiPath = r('src/presets/default.jsonc')
const readmePath = r('README.md')

const emojiFile = fs.readFileSync(eemojiPath, 'utf8')
const emojiJsonc = parse(emojiFile)

if (!emojiJsonc) {
console.error('Failed to parse emojis.jsonc')
console.error('Failed to parse default.jsonc')
process.exit(1)
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import fs from 'node:fs'
import * as jsonc from 'jsonc-parser'
import schema from './../json/eemoji-config-schema.json'

const emojis = jsonc.parse(fs.readFileSync('./src/emojis.jsonc', 'utf8'))
const emojis = jsonc.parse(fs.readFileSync('./src/presets/default.jsonc', 'utf8'))

schema.properties.emojis.default = emojis
fs.writeFileSync('./json/eemoji-config-schema.json', `${JSON.stringify(schema, null, 2)}\n`)

fs.writeFileSync('./src/emojis.json', `${JSON.stringify(emojis, null, 2)}\n`)
fs.writeFileSync('./src/presets/default.json', `${JSON.stringify(emojis, null, 2)}\n`)
5 changes: 2 additions & 3 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as fs from 'node:fs'
import * as path from 'node:path'
import { defineCommand } from 'citty'
import { consola } from 'consola'
import { name } from '../../package.json'
import type { ConfigType, JsFiles, JsonFiles } from '../config'
import { ConfigObject, configTypes } from '../config'
import { r } from '../utils/utils'

const C = new ConfigObject()

Expand Down Expand Up @@ -68,8 +68,7 @@ export default defineCommand({
})

function createConfigFile(filename: JsonFiles | JsFiles, content: string): void {
const filePath = path.join(C.cwd, filename)
fs.writeFileSync(filePath, `${content}\n`)
fs.writeFileSync(r(filename), `${content}\n`)
}

async function checkGitHook(clean: boolean | undefined) {
Expand Down
56 changes: 36 additions & 20 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { merge, mergeWith } from 'lodash-es'
import { isArray, merge } from 'lodash-es'
import { name } from '../package.json'
import emojis from './emojis.json'
import { r } from './utils/utils'
import emojis from './presets/default.json'

globalThis.__eemoji_pkg__ = globalThis.__eemoji_pkg__ || {
entryDir: dirname(fileURLToPath(import.meta.url)),
Expand All @@ -15,15 +15,22 @@ 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>
} | Record<string, string | Record<string, string>>
}

type EmojiType = StringOrOptionalProp<typeof emojis> & { breaking?: string }
type EmojiType = StringOrOptionalProp<typeof emojis> & { breaking: string }
export type EmojiConfig = Record<string, string | Record<string, string>>

export interface Config {
type DefineConfig = Partial<{
format: string
strict: boolean
emojis: EmojiType
}
emojis: EmojiType | EmojiConfig | EmojiConfig[]
}>

export type Config = Required<{
[K in keyof DefineConfig]: DefineConfig[K] extends infer U
? U extends unknown[] ? never : U
: never
}>

export const configTypes = [
'ts',
Expand Down Expand Up @@ -87,25 +94,34 @@ export default defineDefaultConfig({
],
}

cwd = process.env.INIT_CWD || process.cwd()
vscodeSettingsFile = path.join(this.cwd, '.vscode/settings.json')
gitCommitFile = path.join(this.cwd, '.git/COMMIT_EDITMSG')
hooksDir = path.join(this.cwd, '.git/hooks')
hookFile = path.join(this.cwd, '.git/hooks/prepare-commit-msg')
vscodeSettingsFile = r('.vscode/settings.json')
gitCommitFile = r('.git/COMMIT_EDITMSG')
hooksDir = r('.git/hooks')
hookFile = r('.git/hooks/prepare-commit-msg')
entryFile = path.join(globalThis.__eemoji_pkg__.entryDir, 'hook.sh')
}

export function defineConfig(config: Partial<Config>): Config {
export function defineConfig(config: DefineConfig): Config {
const defaultConfig = new ConfigObject().defaultConfig
const emojis = mergeEmojis(config.emojis) || defaultConfig.emojis

return mergeWith({}, defaultConfig, config, (_objValue, srcValue, key) => {
if (key === 'emojis')
return srcValue
})
return {
...defaultConfig,
...config,
emojis,
}
}

export function defineDefaultConfig(config: Partial<Config>): Config {
export function defineDefaultConfig(config: DefineConfig): Config {
const defaultConfig = new ConfigObject().defaultConfig
const emojis = mergeEmojis(config.emojis)

return merge({}, defaultConfig, config, { emojis })
}

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

return merge({}, defaultConfig, config)
return emojis || {}
}
2 changes: 1 addition & 1 deletion src/eemoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function eemojify(text: string, config: Config, DEBUG?: number): string {
}

function getEmoji(type: string, text: string, config: Config, DEBUG?: number): string | undefined {
if (text.includes('!') && config.emojis.breaking)
if (text.includes('!') && config.emojis.breaking && typeof config.emojis.breaking === 'string')
return config.emojis.breaking

type = type.toLowerCase().replace(/\(.*\)/, '').trim()
Expand Down
57 changes: 0 additions & 57 deletions src/emojis.jsonc

This file was deleted.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { runMain as _runMain } from 'citty'
import { main } from './main'

export * from './presets'
export { defineConfig, defineDefaultConfig, type Config } from './config'

export const runMain = () => _runMain(main)
28 changes: 28 additions & 0 deletions src/presets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as fs from 'node:fs'
import * as jsonc from 'jsonc-parser'
import type { EmojiConfig } from './config'
import { r } from './utils/utils'

type Preset = () => EmojiConfig
type PathLiteral = `${string}.jsonc`

function createPresets(pathes: PathLiteral[]) {
return pathes.map((path) => {
const config: EmojiConfig = jsonc.parse(fs.readFileSync(r(`src/presets/${path}`), 'utf-8'))
return () => ({ ...config })
})
}

export const [
presetDefault,
presetMinimal,
presetAoc,
] = createPresets([
'default.jsonc',
'minimal.jsonc',
'aoc.jsonc',
]) as [
Preset,
Preset,
Preset,
]
4 changes: 4 additions & 0 deletions src/presets/aoc.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"add": "🎁,🎅,🎄,❄️,☃️", // finally, part 1 is done, it wasn't that hard
"progress": "🔹" // *cries in part 2*
}
File renamed without changes.
Loading

0 comments on commit f81edc4

Please sign in to comment.