Skip to content

Commit

Permalink
feat: add git-repo-info (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyankatsu0105 committed Aug 1, 2020
1 parent 2a9e20d commit c80b86b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,46 @@ Make `.czferc.js`

```js
module.exports = {
questions(inquirer) {
questions({inquirer, gitInfo}) {
return [
{...},
{...},
]
},
commitMessage(answers) {
commitMessage({answers, gitInfo}) {
return ...
}
}
```

- questions
- params
- ([inquirer](https://github.com/SBoudrias/Inquirer.js))
- [inquirer](https://github.com/SBoudrias/Inquirer.js)
- [gitInfo](https://github.com/rwjblue/git-repo-info)
- return
- [Question Object](https://github.com/SBoudrias/Inquirer.js#question)
- commitMessage
- params
- [answers](https://github.com/SBoudrias/Inquirer.js#answers)
- [gitInfo](https://github.com/rwjblue/git-repo-info)
- return
- string

#### Tips: Configuration settings with types

If you love to develop with types, you can use that with `JSDoc`.
You should install `@types/inquirer`.
If you love to develop with types, you can use that with `JSDocs`.

```shell
npm install -D @types/inquirer
npm install -D @types/inquirer git-repo-info
```

```js
module.exports = {
/**
* @param {import('inquirer').Inquirer} inquirer
* @param {{inquirer: import('inquirer').Inquirer, gitInfo: import('git-repo-info').GitRepoInfo}}
* @returns {import('inquirer').QuestionCollection}
*/
questions(inquirer) {
questions({inquirer, gitInfo}) {
return [
{
type: "list",
Expand All @@ -94,10 +95,10 @@ module.exports = {
/**
* @typedef {{questionType1: string; questionType2: string}} Answers
*
* @param {Answers} answers
* @param {{answers: Answers, gitInfo: import('git-repo-info').GitRepoInfo}}
* @returns {string}
*/
commitMessage(answers) {
commitMessage({answers, gitInfo}) {
return `${answers.questionType1}${answers.questionType2}`
}
}
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"cosmiconfig": "^6.0.0",
"git-repo-info": "^2.1.1",
"inquirer": "^7.3.3",
"tslib": "2.0.0"
},
Expand Down
20 changes: 12 additions & 8 deletions src/engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { QuestionCollection } from "inquirer";
import { initialize } from "./util";
import * as inquirer from "inquirer";
import getRepoInfo from "git-repo-info";

type CZ = {
prompt: <T>(questions: QuestionCollection<T>) => Promise<T>;
Expand All @@ -10,22 +11,25 @@ type Commit = (commitMessage: string) => void;

export const engine = () => {
const { config } = initialize();
const gitInfo = getRepoInfo();

const prompter = (cz: CZ, commit: Commit) => {
if (config.questions === undefined)
throw new Error("Could not find questions.");

return cz.prompt(config.questions(inquirer)).then((answers) => {
if (config.commitMessage === undefined)
throw new Error("Could not find commitMessage.");
return cz
.prompt(config.questions({ inquirer, gitInfo }))
.then((answers) => {
if (config.commitMessage === undefined)
throw new Error("Could not find commitMessage.");

const commitMessage = config.commitMessage(answers);
const commitMessage = config.commitMessage({ answers, gitInfo });

if (typeof commitMessage !== "string")
throw new Error("commitMessage should return string.");
if (typeof commitMessage !== "string")
throw new Error("commitMessage should return string.");

commit(commitMessage);
});
commit(commitMessage);
});
};

return {
Expand Down
17 changes: 15 additions & 2 deletions src/util/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import * as Cosmiconfig from "cosmiconfig";
import * as Const from "./const";
import { QuestionCollection, Answers, Inquirer } from "inquirer";
import { GitRepoInfo } from "git-repo-info";

const explorer = Cosmiconfig.cosmiconfigSync(Const.MODULE_NAME);

type Config = {
questions: (inquirer: Inquirer) => QuestionCollection;
commitMessage: (answers: Answers) => string;
questions: ({
inquirer,
gitInfo,
}: {
inquirer: Inquirer;
gitInfo: GitRepoInfo;
}) => QuestionCollection;
commitMessage: ({
answers,
gitInfo,
}: {
answers: Answers;
gitInfo: GitRepoInfo;
}) => string;
};

type Initialize = {
Expand Down

0 comments on commit c80b86b

Please sign in to comment.