DEV Community

tyankatsu
tyankatsu

Posted on

Let's make your own commit message template with cz-format-extension

Almost developers will use Git when developing.
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, some adapters are intoduced.
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;

============================
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)
Enter fullscreen mode Exit fullscreen mode
/**
 * @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}`;
  },
};
Enter fullscreen mode Exit fullscreen mode

You know, cz-format-extension is very highly expandable. You can use API of Inquirer.js, git-repo-info, and git-js.

Can I use TypeScript for config file?

No. However cz-format-extension provides type like this. 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.

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

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.

Discussion (0)