From 7fb531ba2f9af52834a2b31c9db814184c41f270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hegyi=20=C3=81ron=20Ferenc?= Date: Thu, 25 Jan 2024 17:28:50 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix:=20correctly=20bundle=20pres?= =?UTF-8?q?ets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/json.yml | 3 ++- README.md | 12 ++++++------ bin/eemoji.mjs | 6 ------ package.json | 7 ++++--- scripts/presets.ts | 32 ++++++++++++++++++++++++++++++++ scripts/schema.ts | 2 -- src/config.ts | 15 ++++++++++----- src/presets.ts | 17 ++++++++--------- 8 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 scripts/presets.ts diff --git a/.github/workflows/json.yml b/.github/workflows/json.yml index 4b78406..4e57664 100644 --- a/.github/workflows/json.yml +++ b/.github/workflows/json.yml @@ -1,5 +1,5 @@ # 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 @@ -7,6 +7,7 @@ on: branches: [main] paths: - src/config.ts + - src/presets.ts - src/presets/default.jsonc # Allows you to run this workflow manually from the Actions tab diff --git a/README.md b/README.md index dbcead9..91380e1 100644 --- a/README.md +++ b/README.md @@ -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.
Close/open gigantic table @@ -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: @@ -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, @@ -409,7 +409,7 @@ export const [ ] ``` -Then, open a PR. +Then open a PR, the Action will take care of the rest. ### Development diff --git a/bin/eemoji.mjs b/bin/eemoji.mjs index c3defc9..145e825 100644 --- a/bin/eemoji.mjs +++ b/bin/eemoji.mjs @@ -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() diff --git a/package.json b/package.json index 7286330..1489494 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/scripts/presets.ts b/scripts/presets.ts new file mode 100644 index 0000000..ef46c37 --- /dev/null +++ b/scripts/presets.ts @@ -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 + }) + }) + } + }) +}) diff --git a/scripts/schema.ts b/scripts/schema.ts index a0b79b9..5aec024 100644 --- a/scripts/schema.ts +++ b/scripts/schema.ts @@ -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`) diff --git a/src/config.ts b/src/config.ts index 71dacf1..0f687f2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 extends `${infer First}|${infer _}` ? First : T @@ -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 { diff --git a/src/presets.ts b/src/presets.ts index ac9652a..e87b442 100644 --- a/src/presets.ts +++ b/src/presets.ts @@ -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 }) }) } @@ -18,9 +17,9 @@ export const [ presetMinimal, presetAoc, ] = createPresets([ - 'default.jsonc', - 'minimal.jsonc', - 'aoc.jsonc', + 'default', + 'minimal', + 'aoc', ]) as [ Preset, Preset,