DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

ι³₯山明
ι³₯山明

Posted on • Updated on

Create a commit message in an interactive format. with Deno

https://github.com/bird-studio/hoipoi_capsule

Hoipoi capsule

What is this?

Create a commit message in an interactive format.

Try

# Go to git directory
brew install deno

deno run --allow-net --allow-write --allow-run "https://deno.land/x/hoipoi_capsule/demo/fill_in_commit_message/conventionalcommits_style.ts?source"
cat .git/COMMIT_EDITMSG
Enter fullscreen mode Exit fullscreen mode

Use

Run with Git hook.

It works without gh.gh is used to obtain github issues.Please complete the gh
setup in advance.

brew install deno gh
Enter fullscreen mode Exit fullscreen mode

Prepare Git hook.

mkdir .githooks

cat <<EOF > .githooks/prepare-commit-msg
#!/bin/sh

exec < /dev/tty deno run --allow-net --allow-write --allow-run "https://deno.land/x/hoipoi_capsule/demo/fill_in_commit_message/conventionalcommits_style.ts?source"

EOF

git config --local core.hooksPath .githooks
chmod +x .githooks/prepare-commit-msg
Enter fullscreen mode Exit fullscreen mode

Execution.

touch myFile.txt
git add myFile.txt
git commit
Enter fullscreen mode Exit fullscreen mode

Customize

Here is a reference.

import * as hoipoiCapsule from "https://deno.land/x/hoipoi_capsule/mod.ts";

const commitMessageTemplate = `{{type}}({{scope}}): {{summary}}

{{body}}

BREAKING CHANGE: {{breakingChange}}`;

hoipoiCapsule.useCase.fillInCommitMessage.run({
  commitMessageTemplate,
  questionList: [
    {
      /**
       * The answer applies to the {{type}} part of commitMessageTemplate.
       */
      target: "type",
      /**
       * Pre-prepared questions.
       */
      q: hoipoiCapsule.preset.fillInCommitMessage.conventionalcommits.qMap.type,

      /**
       * Thus, you can also create your own questions.
       */
      //   q: typeQ,

      /**
       * Modify the commit message.
       * Use this function when a message is unanswered, for example.
       */
      fixCommitMessage: (p) => {
        if (p.answerMap["type"] === "???") {
          return p.commitMessage.replace(/\r?\n{2,}/, "\n").trim();
        }
        return p.commitMessage;
      },
    },
  ],
});

/**
 * Please check here.
 * https://github.com/c4spar/deno-cliffy
 */
const typeQ = () =>
  hoipoiCapsule.userInterface.prompt.Select.prompt({
    message: "Select type.",
    search: true,
    options: [
      {
        name:
          "Build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)",
        value: "Build",
      },
      {
        name:
          "CI: Changes to our CI configuration files and scripts (examples: CircleCi, SauceLabs)",
        value: "CI",
      },
      { name: "Docs: Documentation only changes", value: "Docs" },
      { name: "Feat: A new feature", value: "Feat" },
      { name: "Fix: A bug fix", value: "Fix:" },
      { name: "Perf: A code change that improves performance", value: "Perf" },
      {
        name:
          "Refactor: A code change that neither fixes a bug nor adds a feature",
        value: "Refactor",
      },
      {
        name: "Test: Adding missing tests or correcting existing tests",
        value: "Test",
      },
    ],
  });
Enter fullscreen mode Exit fullscreen mode

Convenient Use

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!