diff --git a/README.md b/README.md index 4c112a95..333335cb 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ only really useful as a `prepare-commit-msg` or `commit-msg` hook. When to use what hook? * `prepare-commit-msg`: use this if you do not use **Devmnojis** `--lint` option and want to use it with something like [commitlint](https://commitlint.js.org/) instead. -* `commit-msg`: use this hook if you also want to use **Devmoji** for linting +* `commit-msg`: use this hook if you also want to use **Devmoji** for linting Configuration using [Husky](https://typicode.github.io/husky/) @@ -168,8 +168,8 @@ Configuration using [Yorkie](https://www.npmjs.com/package/yorkie) // package.json { "gitHooks": { - "prepare-commit-msg": "devmoji -e --lint" - } + "prepare-commit-msg": "devmoji -e --lint" +} } ``` @@ -208,15 +208,15 @@ To get a list of all available **Devmoji**, run with `--list`. (see also ## :gear: Configuration -`devmoji` uses the config file as specified with the `--config` option, or looks -for `devmoji.config.js` in the following paths: +`devmoji` uses the config file as specified with the `--config` option, otherwise it looks +for `devmoji.config.js` or `devmoji.config.ts` in the following paths: - current directory - parent directory that contains a `package.json` file - parent directory that is a `git` repository - home directory -### Example Config File +### Example of a JavaScript Config File ```js module.exports = { @@ -247,6 +247,37 @@ module.exports = { } ``` +### Example of a TypeScript Config File + +```typescript +export default { + // extra types used in commit messages + types: ["lint"], + // custom devmoji + devmoji: [ + // use :boom: instead of :sparkles: for the type 'feat' + { code: "feat", emoji: "boom" }, + // add a custom devmoji + { + code: "fail", + emoji: "poop", + description: "something bad happened", + }, + // add a new devmoji based on an existing gitmoji. description will be taken from the gitmoji + { + code: "css", + gitmoji: "art", + }, + // the emoji from the gitmoji can be overriden as well + { + code: "config", + gitmoji: "wrench", + emoji: "gear", + }, + ], +} +``` + ### Default Devmoji Reference | Emoji | Devmoji Code | Description | diff --git a/__tests__/config.ts b/__tests__/config.ts index edac62e3..d3503dab 100644 --- a/__tests__/config.ts +++ b/__tests__/config.ts @@ -7,6 +7,12 @@ test("load config", async () => { expect(config.pack.get("fuckup")?.emoji).toBe("poop") }) +test("load typescript from config file", async () => { + const config = await Config.load("__tests__/ignore.devmoji.config.ts") + expect(config.pack.get("feat")?.emoji).toBe("poop") + expect(config.pack.get("fuckup")?.emoji).toBe("poop") +}) + test("missing prop code", () => { expect(() => { const config = { devmoji: [{ foo: 1 }] } as unknown as ConfigOptions @@ -56,3 +62,13 @@ test("no default config file", async () => { expect(error).toMatch(/missing.*/) } }) + +test("config file does not exist", async () => { + const configFile = "__tests__/fake.devmoji.config.js" + try { + await Config.load(configFile, "/") + } catch (error) { + // eslint-disable-next-line jest/no-try-expect, jest/no-conditional-expect + expect(error).toMatch(`Config file not found ${configFile}`) + } +}) diff --git a/__tests__/ignore.devmoji.config.ts b/__tests__/ignore.devmoji.config.ts new file mode 100644 index 00000000..a77cb3ac --- /dev/null +++ b/__tests__/ignore.devmoji.config.ts @@ -0,0 +1,10 @@ +export default { + devmoji: [ + { + code: "fuckup", + emoji: "poop", + description: "something got messed up pretty bad", + }, + { code: "feat", emoji: "poop" }, + ], +} diff --git a/src/config-options.ts b/src/config-options.ts index e76d763b..9cfe96b2 100644 --- a/src/config-options.ts +++ b/src/config-options.ts @@ -6,6 +6,7 @@ export type TDevmoji = { } export interface ConfigOptions { + default?: ConfigOptions types: string[] devmoji: TDevmoji[] } diff --git a/src/config.ts b/src/config.ts index e03e3088..e5e44400 100644 --- a/src/config.ts +++ b/src/config.ts @@ -14,6 +14,7 @@ export class Config { constructor(options?: ConfigOptions) { this._load(defaults) if (options) { + if (options.default) options = { ...options.default } if (!options.types) options.types = [] if (!options.devmoji) options.devmoji = [] this.validate(options) @@ -71,9 +72,14 @@ export class Config { ] for (const p of searchPaths) { if (p) { - const file = path.resolve(p, "./devmoji.config.js") - if (fs.existsSync(file)) { - configFile = file + const javascriptConfigFile = path.resolve(p, "./devmoji.config.js") + if (fs.existsSync(javascriptConfigFile)) { + configFile = javascriptConfigFile + break + } + const typescriptConfigFile = path.resolve(p, "./devmoji.config.ts") + if (fs.existsSync(typescriptConfigFile)) { + configFile = typescriptConfigFile break } }