Skip to content

Commit

Permalink
Merge pull request #1 from Rettend/v1-alpha
Browse files Browse the repository at this point in the history
V1 alpha
  • Loading branch information
Rettend authored Nov 11, 2023
2 parents c6e74f6 + c75b05f commit 4749a32
Showing 13 changed files with 490 additions and 252 deletions.
18 changes: 0 additions & 18 deletions .vscode/settings.json

This file was deleted.

135 changes: 94 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

This tiny CLI tool automatically adds an emoji to your commit messages based on conventional commit types.

It runs with the `prepare-commit-msg` git hook (every time your make a commit) and modifies your commit messages.

## 😎 Emojis

Hi, read the `README.md` first (starting with [Install](#-install)). This emoji table is for quick reference.
@@ -14,16 +16,16 @@ Hi, read the `README.md` first (starting with [Install](#-install)). This emoji
| `fix` | `.` | 🔧 | general fix |
| `fix` | `typo` | ✏️ | fixed typo in code, docs, ui, etc. |
| `fix` | `bug` | 🐛 | fixed a bug |
| `fix` | `lint` | 🧼 | fixed linting errors |
| `lint` | | 🧼 | |
| `chore` | `.` | 🗑️ | did some chores |
| `chore` | `release` | 🔖 | new release commit (pairs well with [bumpp](https://github.com/antfu/bumpp) and [changelogithub](https://github.com/antfu/changelogithub)) |
| `chore` | `cleanup` | 🧹 | cleaned up code, removed logs and debug stuff, making things ready for production |
| `chore` | `license` | 📜 | changed the license |
| `chore` | `lint` | 🧼 | fixed linting errors |
| `chore` | `deps` | 📦 | updated dependencies |
| `chore` | `readme` | 📕,📗,📘,📙 | update the README |
| `release` | | 🔖 | |
| `cleanup` | | 🧹 | |
| `lint` | | 🧼 | |
| `deps` | `.` | 📦 | added/removed/changed dependencies |
| `deps` | `up` | ⬆️ | updated dependencies |
| `deps` | `down` | ⬇️ | downgraded dependencies |
@@ -51,66 +53,104 @@ Hi, read the `README.md` first (starting with [Install](#-install)). This emoji

## 🚀 Install

The package ensures that the JSON schema is added to `settings.json`, and creates a `prepare-commit-msg` git hook.
There are two different ways to install `eemoji`:

> Recommended: local install as a dev dependency
- [Locally](#local): add it to a node project as a dev dependency. This way you can share it with other contributors.
- [Globally](#global): install it globally to use it everywhere, <u>not just in node projects</u>. Note that you will need to initialize it in other repository for it to work there.

### Locally
Thanks to the `postinstall` script, after installing `eemoji` locally/globally, it will automatically create the git hook for the current repo. This means it will just work, but it's especially useful when other users are going to contribute to your repository.

This way you can add `eemoji` to your `package.json` dependencies, and share it with other contributors (they will need to run `eemoji` once as well).
### Local

```bash
npm i -D eemoji
```

### Globally
### Global

Installing globally allows you to use `eemoji` everywhere, not just in node projects.
```bash
npm i -g eemoji
```

> **Note**
> For `eemoji` to work in a repository you still need to run it once, so that it can install the git hook.
> For `eemoji` to work in a repository you still need to run the init command once, so that it can install the git hook there as well.
## 📖 Usage

After installing it, just create a commit and see an emoji appended to it.

Go to the [Config](#-config) section for [examples](#simple-demonstration) and to see how to customize it.

Use `eemoji <command>` if you installed it globally, or `npx eemoji <command>` if you installed it locally.

### Commands

Use the help command to see all available commands and flags.

`eemoji -h` or `eemoji --help`

In addition, check the version with `eemoji --version`.

#### 🚩 Init

Installs the git hook in the current repository.

It will also ask you what type of config file you want to use, see [Config](#-config).

The `postinstall` script will run this command with the `-c none` flag, so no config is assumed.

```bash
npm i -g eemoji
eemoji init
```

## 📖 Usage
**Flags:**

Just run it once in your project to initialize it:
Specify flags to skip the questions.

### Local
- `-c, --config <config>`: specify the config type, `json`, `ts` or `none`

#### 🧹 Cleanup

Removes `eemoji` from the current repository, including the git hook, config file and its vscode settings if present.

```bash
npx eemoji
eemoji cleanup
```

### Global
#### 🥏 Run

Runs the `eemoji` on the current commit message manually.

This is used by the git hook, but you can also test it manually (specify a test file or it will use the current commit message in `.git/COMMIT_EDITMSG`).

```bash
eemoji
eemoji run
```

**Commands:**
**Arguments:**

- `commit_file`: the file to run emoji on, defaults to `.git/COMMIT_EDITMSG`

- `--version`: Show version number
- `-h, --help`: Show help (useless)
**Flags:**

- `-d, --debug`: the debug level, `0` for none, `1` for some, `2` for all

## 🦾 Config

The default configuration is here: [emojis.json](./src/emojis.json), or scrutinize the [Emojis](#-emojis) section for details.
The default configuration is here: [emojis.json](./src/emojis.json) and the [Emojis](#-emojis) section.

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

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

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

> Recommended: use `eemoji.config.ts` in project root
### TS Config

This way your emojis will be merged with the default ones.

Use the `init` command and select the `ts` config type.

`eemoji` will look for these config files:

- `eemoji.config.ts`
@@ -124,6 +164,20 @@ This way your emojis will be merged with the default ones.

</details>

#### Format

The format specifies how the commit message will be formatted (`{emoji} {type}: {subject}` is the default format btw, this property is optional here).

- `{type}`: this determines the emoji
- `{subject}`: rest of the commit message
- `{emoji}`: the place of the emoji to be inserted

Some other formats I could think of:

- `{emoji} {type} - {subject}`
- `{emoji} {type} {subject}`
- `{type}: {emoji} {subject}`

#### Simple demonstration

```ts
@@ -148,24 +202,14 @@ Commit message:
- before: `fix: navbar issue`
- after: `🔧 fix: navbar issue`

#### Format

The format specifies how the commit message will be formatted (`{emoji} {type}: {subject}` is the default format btw, this property is optional).

- `{type}`: this determines the emoji
- `{subject}`: rest of the commit message
- `{emoji}`: the place of the emoji to be inserted

Some other formats I could think of:

- `{emoji} {type} - {subject}`
- `{emoji} {type} {subject}`
- `{type}: {emoji} {subject}`

#### Nested emojis

You can also nest emojis to create subtypes. After finding the type, `eemoji` will look for subtypes in the commit message.

Either using conventional commit scopes or just including the subtype in the commit message will work.

You can specify multiple emojis by separating them with commas and a **random** one will be chosen.

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

@@ -189,10 +233,6 @@ export default defineConfig({
})
```

You can specify multiple emojis by separating them with commas and a **random** one will be chosen.

Either using conventional commit scopes or just including the subtype in the commit message will work.

**Examples:**

Commit message:
@@ -219,6 +259,8 @@ Commit message:

Same deal, but you overwrite the whole config.

Use the `init` command and select the `json` config type to generate a config file.

> **Warning**
> You must specify the `format` property
@@ -238,6 +280,17 @@ You also get types thanks to the JSON schema.

</details>

## ⚠️ Known issues

There is a [known issue](https://github.com/desktop/desktop/issues/12562#issuecomment-1444580040) with GitHub Desktop.

You need to follow these steps to make it work:

- Add `C:\Program Files\Git\bin` to your system `PATH` environment variable next to `C:\Program Files\Git\cmd`
- Move these to the top of your `PATH`!
- Run this command in your repository: `git config stash.usebuiltin false`
- Restart GitHub Desktop

## 📜 License

[MIT](./LICENSE)
12 changes: 12 additions & 0 deletions bin/hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
LOCAL_PATH=$(npm root)
NODE_PATH=$(npm root -g)

if [ -f "$LOCAL_PATH/eemoji/bin/eemoji.mjs" ]; then
node "$LOCAL_PATH/eemoji/bin/eemoji.mjs" run $1
elif [ -f "$NODE_PATH/eemoji/bin/eemoji.mjs" ]; then
node "$NODE_PATH/eemoji/bin/eemoji.mjs" run $1
else
echo "eemoji is not properly installed locally or globally"
exit 1
fi
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eemoji",
"version": "0.2.4",
"version": "1.0.0",
"packageManager": "pnpm@8.10.0",
"description": "(๑•̀ㅂ•́)و✧ Add emojis to git commits",
"author": "Rettend",
@@ -27,20 +27,22 @@
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"bin": {
"eemoji": "./bin/eemoji.mjs"
"eemoji": "./bin/eemoji.mjs",
"eemoji-hook": "./bin/hook.sh"
},
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "unbuild && node ./dist/index.mjs",
"dev": "npx eemoji",
"stub": "unbuild --stub",
"test": "vitest --ui --coverage --api 9527",
"lint": "eslint .",
"lint:fix": "pnpm run lint --fix",
"typecheck": "tsc --noEmit",
"release": "bumpp && unbuild && pnpm publish"
"release": "bumpp && unbuild && pnpm publish",
"postinstall": "node ./bin/eemoji.mjs init -c none"
},
"dependencies": {
"citty": "^0.1.4",
68 changes: 68 additions & 0 deletions src/commands/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as fs from 'node:fs'
import path from 'node:path'
import { defineCommand } from 'citty'
import { consola } from 'consola'
import { ConfigObject } from '../config'
import { name } from '../../package.json'

const C = new ConfigObject()

export default defineCommand({
meta: {
name: 'cleanup',
description: 'Cleanup git hook and settings.json',
},
async run() {
consola.start('Removing git hook...')

if (fs.existsSync(C.hookFile)) {
fs.unlinkSync(C.hookFile)
consola.success(`Removed ${C.hookFile}`)
}
else {
consola.info(`No ${C.hookFile} found`)
}

consola.start('Removing VSCode settings...')

if (fs.existsSync(C.vscodeSettingsFile)) {
const settings = JSON.parse(fs.readFileSync(C.vscodeSettingsFile, 'utf-8'))
if (settings['json.schemas']) {
settings['json.schemas'] = settings['json.schemas'].filter((schema: any) => {
return schema.url !== `https://rettend.github.io/${name}/${name}-config-schema.json`
})
if (settings['json.schemas'].length === 0)
delete settings['json.schemas']
}
if (Object.keys(settings).length === 0) {
fs.unlinkSync(C.vscodeSettingsFile)
fs.rmdirSync(path.dirname(C.vscodeSettingsFile))
consola.success(`Removed ${C.vscodeSettingsFile}`)
}
else {
fs.writeFileSync(C.vscodeSettingsFile, JSON.stringify(settings, null, 2))
consola.info(`Updated ${C.vscodeSettingsFile}`)
}
}
else {
consola.info(`No ${C.vscodeSettingsFile} found`)
}

consola.start('Removing config files...')
let removedFiles = false

;[...C.jsFiles, ...C.jsonFiles.filter(file => file !== 'package.json')].forEach((file: string) => {
if (fs.existsSync(file)) {
fs.unlinkSync(file)
consola.success(`Removed ${file}`)
removedFiles = true
}
})

if (!removedFiles)
consola.info('No config files found')

else
consola.success('Cleanup complete!')
},
})
9 changes: 9 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CommandDef } from 'citty'

const _rDefault = (r: any) => (r.default || r) as Promise<CommandDef>

export const commands = {
init: () => import('./init').then(_rDefault),
run: () => import('./run').then(_rDefault),
cleanup: () => import('./cleanup').then(_rDefault),
} as const
Loading

0 comments on commit 4749a32

Please sign in to comment.