Skip to content

Commit

Permalink
🔧 fix: correctly bundle presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Rettend committed Jan 25, 2024
1 parent 101e790 commit 7fb531b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/json.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Simple workflow for deploying static content to GitHub Pages
name: Deploy JSON Schema to Pages
name: Deploy JSON and Update

on:
# Runs on pushes targeting the default branch, when certain files are modified
push:
branches: [main]
paths:
- src/config.ts
- src/presets.ts
- src/presets/default.jsonc

# Allows you to run this workflow manually from the Actions tab
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This is a tiny CLI tool that automatically adds emojis to your commit messages b
## 😎 Emojis

Hi, read the `README.md` first (starting with [Install](#-install)). This emoji table is for quick reference.
Hi, read the `README.md` first (starting with [Install](#-install)). This emoji table is for quick reference. Btw it's generated from the [default.jsonc](./src/presets/default.jsonc) file.

<details open>
<summary><b>Close/open gigantic table</b></summary>
Expand Down Expand Up @@ -373,7 +373,7 @@ Then, if it's okay:

### Creating a new emoji preset

Add a new `jsonc` file to the **presets** folder and name it as you wish.
Add a new `jsonc` (note the `c` at the end) file to the **presets** folder and name it as you wish.
Describe the emoji using comments.

Example:
Expand All @@ -395,11 +395,11 @@ export const [

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

// make sure the path is relative to the root of the project
// add your file name here (without the .jsonc extension)
]) as [
Preset,
Preset,
Expand All @@ -409,7 +409,7 @@ export const [
]
```

Then, open a PR.
Then open a PR, the Action will take care of the rest.

### Development

Expand Down
6 changes: 0 additions & 6 deletions bin/eemoji.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/usr/bin/env node

import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { runMain } from './../dist/index.mjs'

globalThis.__eemoji_pkg__ = {
entryDir: dirname(fileURLToPath(import.meta.url)),
}

runMain()
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@
"dist"
],
"scripts": {
"build": "unbuild",
"stub": "unbuild --stub",
"build": "unbuild && pnpm run gen:presets",
"stub": "unbuild --stub && pnpm run gen:presets",
"dev": "node ./bin/eemoji.mjs",
"test": "vitest",
"test:ui": "vitest --ui --coverage --api 9527",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"typecheck": "tsc --noEmit",
"gen:presets": "tsx scripts/presets",
"gen:schema": "tsx scripts/schema",
"gen:table": "tsx scripts/readme-table",
"release": "bumpp && unbuild && pnpm publish",
"release": "bumpp && pnpm run build && pnpm publish",
"postinstall": "node ./bin/eemoji.mjs init -c none",
"preuninstall": "node ./bin/eemoji.mjs cleanup",
"prepare": "simple-git-hooks"
Expand Down
32 changes: 32 additions & 0 deletions scripts/presets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'node:fs'
import path from 'node:path'
import * as jsonc from 'jsonc-parser'
import { r } from '../src/utils/utils'

const presetsDir = r('src/presets')
const outputDir = r('dist/presets')

if (!fs.existsSync(outputDir))
fs.mkdirSync(outputDir, { recursive: true })

fs.readdir(presetsDir, (err, files) => {
if (err)
throw err

files.forEach((file) => {
if (path.extname(file) === '.jsonc') {
fs.readFile(path.join(presetsDir, file), 'utf8', (err, data) => {
if (err)
throw err

const json = jsonc.parse(data)
const outputFilename = path.join(outputDir, `${path.basename(file, '.jsonc')}.json`)

fs.writeFile(outputFilename, JSON.stringify(json, null, 2), (err) => {
if (err)
throw err
})
})
}
})
})
2 changes: 0 additions & 2 deletions scripts/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ 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/presets/default.json', `${JSON.stringify(emojis, null, 2)}\n`)
15 changes: 10 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import { isArray, merge } from 'lodash-es'
import { name } from '../package.json'
import { r } from './utils/utils'
import emojis from './presets/default.json'

globalThis.__eemoji_pkg__ = globalThis.__eemoji_pkg__ || {
entryDir: dirname(fileURLToPath(import.meta.url)),
}
const entryDir = path.dirname(fileURLToPath(
new URL(
import.meta.url.endsWith('.ts')
? '../bin/eemoji.mjs'
: '../../bin/eemoji.mjs',
import.meta.url,
),
))

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

Expand Down Expand Up @@ -98,7 +102,8 @@ export default defineDefaultConfig({
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')
entryDir = entryDir
entryFile = path.join(this.entryDir, 'hook.sh')
}

export function defineConfig(config: DefineConfig): Config {
Expand Down
17 changes: 8 additions & 9 deletions src/presets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as fs from 'node:fs'
import * as jsonc from 'jsonc-parser'
import type { EmojiConfig } from './config'
import { r } from './utils/utils'
import { dirname, join } from 'node:path'
import { ConfigObject, type EmojiConfig } from './config'

const C = new ConfigObject()
type Preset = () => EmojiConfig
type PathLiteral = `${string}.jsonc`

function createPresets(pathes: PathLiteral[]) {
function createPresets(pathes: string[]): Preset[] {
return pathes.map((path) => {
const config: EmojiConfig = jsonc.parse(fs.readFileSync(r(`src/presets/${path}`), 'utf-8'))
const config: EmojiConfig = JSON.parse(fs.readFileSync(join(dirname(C.entryDir), 'dist', 'presets', `${path}.json`), 'utf8'))
return () => ({ ...config })
})
}
Expand All @@ -18,9 +17,9 @@ export const [
presetMinimal,
presetAoc,
] = createPresets([
'default.jsonc',
'minimal.jsonc',
'aoc.jsonc',
'default',
'minimal',
'aoc',
]) as [
Preset,
Preset,
Expand Down

0 comments on commit 7fb531b

Please sign in to comment.