Let's make your own commit message template with cz-format-extension
Tags: node, git, javascript
Published: 2021-08-31 12:17:51

Almost developers will use Git when developing.
[commitizen/cz-cli](https://github.com/commitizen/cz-cli) is cool library.
If you use that, you can make commit message of Git easily.
And also, commitizen community and some developers provides adapter when using cz-cli.
[In this section](https://github.com/commitizen/cz-cli#adapters), some adapters are intoduced.
[cz-format-extension](https://github.com/tyankatsu0105/cz-format-extension) is one of adapters that being introduced in cz-cli README.

## What can I do?

When you use cz-format-extension, you can custom format of cz-cli.
Like this;

```bash
============================
Current Branch is master
============================

? input1: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
? input2: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Error: Can't commit to master branch
    at Object.commitMessage (/path/project/.czferc.js:33:13)
    at /path/node_modules/cz-format-extension/dist/engine.js:19:42
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
```

```js
/**
 * @typedef {{input1: string;input2: string;}} Answers
 */

/** @type import('cz-format-extension').Config<Answers> */
module.exports = {
  questions({ inquirer, gitInfo }) {
    const ui = new inquirer.ui.BottomBar();
    ui.log.write(`============================`);
    ui.log.write(`Current Branch is ${gitInfo.branch}`);
    ui.log.write(`============================`);
    ui.log.write(``);

    return [
      {
        type: "input",
        name: "input1",
      },
      {
        type: "input",
        name: "input2",
      },
    ];
  },
  commitMessage({ answers, gitInfo }) {
    process.on("unhandledRejection", (reason) => {
      console.error(reason);
      process.exit(1);
    });

    if (gitInfo.branch === "master") {
      throw new Error("Can't commit to master branch");
    }
    return `${answers.input1}\n${answers.input2}`;
  },
};
```

You know, cz-format-extension is very highly expandable. You can use API of [Inquirer.js](https://github.com/SBoudrias/Inquirer.js), [git-repo-info](https://github.com/rwjblue/git-repo-info), and [git-js](https://github.com/steveukx/git-js).

## Can I use TypeScript for config file?

No. However cz-format-extension provides type [like this](https://github.com/tyankatsu0105/cz-format-extension/blob/6683a101ed2643a9f976e2b01ef99f6b53ed05b0/src/types.ts#L18-L33). cz-format-extension is created with TypeScript(I love TS!!!!).
If you use `Config` type inside JSDocs `@types`, you can get power of TS [like this](https://github.com/tyankatsu0105/cz-format-extension/blob/6683a101ed2643a9f976e2b01ef99f6b53ed05b0/example/conventional-commits/.czferc.js#L7-L9).

## OK. So what difference feature cz-customizable and this?

[cz-customizable](https://github.com/leoforfree/cz-customizable)
is great library. However that provides a few option.
This point is what I started create cz-format-extension.
cz-format-extension has high expandable, and also dependencies keeps latest.

## Sumarry

- cz-format-extension is introduced at cz-cli's README
- cz-format-extension has high expandable
- cz-format-extension provide type for config

Thank you for reading this article.
Please send a star for cz-format-extension.