Skip to content

Commit

Permalink
feat: improve gitInfo (#72)
Browse files Browse the repository at this point in the history
* feat: improve gitInfo

close #71

* test: add gitInfo spec

* test: add git class

* test: remove unnecessary test case
  • Loading branch information
tyankatsu0105 committed Aug 29, 2020
1 parent 945ae88 commit 3413a25
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ module.exports = {
- questions
- params
- [inquirer](https://github.com/SBoudrias/Inquirer.js)
- [gitInfo](https://github.com/rwjblue/git-repo-info)
- [gitInfo](https://github.com/rwjblue/git-repo-info) + `not_added`, `created`, `deleted`, `modified`, `renamed`, `staged` from [StatusResult](https://github.com/steveukx/git-js/blob/master/typings/response.d.ts) of [simple-git](https://github.com/steveukx/git-js)
- 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)
- [gitInfo](https://github.com/rwjblue/git-repo-info) + `not_added`, `created`, `deleted`, `modified`, `renamed`, `staged` from [StatusResult](https://github.com/steveukx/git-js/blob/master/typings/response.d.ts) of [simple-git](https://github.com/steveukx/git-js)
- return
- string

Expand Down
27 changes: 24 additions & 3 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
"dev": "tsc -w",
"release": "shipjs prepare",
"test": "jest",
"test:watch": "jest --watch",
"test:cli": "sh scripts/test-cli",
"test:watch": "jest --watch",
"typecheck": "tsc --project ./tsconfig.build.json --noEmit"
},
"dependencies": {
"@types/inquirer": "^7.3.0",
"cosmiconfig": "^6.0.0",
"git-repo-info": "^2.1.1",
"inquirer": "^7.3.3",
"simple-git": "^2.20.1",
"tslib": "2.0.0"
},
"devDependencies": {
Expand Down
30 changes: 15 additions & 15 deletions src/engine.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import * as inquirer from "inquirer";
import getRepoInfo from "git-repo-info";
import { initialize } from "./util";
import { initialize, getGitInfo } from "./util";
import { CZ, Commit } from "./types";

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 getGitInfo().then(({ gitInfo }) => {
if (config.questions === undefined)
throw new Error("Could not find questions.");

return cz
.prompt(config.questions({ inquirer, gitInfo }))
.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, gitInfo });
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
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { Inquirer, QuestionCollection } from "inquirer";
import { GitRepoInfo } from "git-repo-info";
import { StatusResult } from "simple-git";

export type CZ = Inquirer;
export type Commit = (commitMessage: string) => void;

type GitInfo = GitRepoInfo &
Pick<
StatusResult,
"not_added" | "created" | "deleted" | "modified" | "renamed" | "staged"
>;

export type Config<T> = {
questions: ({
inquirer,
gitInfo,
}: {
inquirer: Inquirer;
gitInfo: GitRepoInfo;
gitInfo: GitInfo;
}) => QuestionCollection;
commitMessage: ({
answers,
gitInfo,
}: {
answers: T;
gitInfo: GitRepoInfo;
gitInfo: GitInfo;
}) => string;
};
22 changes: 22 additions & 0 deletions src/util/gitInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import gitRepoInfo from "git-repo-info";
import simpleGit from "simple-git";

export const getGitInfo = async (repoPath: string = process.cwd()) => {
const repoInfo = gitRepoInfo(repoPath);
const gitStatus = simpleGit(repoPath);
const status = await gitStatus.status();

const { not_added, created, deleted, modified, renamed, staged } = status;

const gitInfo = {
...repoInfo,
not_added,
created,
deleted,
modified,
renamed,
staged,
};

return { gitInfo };
};
1 change: 1 addition & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./const";
export * from "./initialize";
export * from "./gitInfo";
32 changes: 32 additions & 0 deletions tests/util/gitInfo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getGitInfo } from "../../src/util";

describe("getGitInfo", () => {
it("when run function, should return some property that helpful to use information about Git", async () => {
const { gitInfo } = await getGitInfo();

// By git-repo-info
expect(gitInfo).toHaveProperty("sha");
expect(gitInfo).toHaveProperty("abbreviatedSha");
expect(gitInfo).toHaveProperty("branch");
expect(gitInfo).toHaveProperty("tag");
expect(gitInfo).toHaveProperty("committer");
expect(gitInfo).toHaveProperty("committerDate");
expect(gitInfo).toHaveProperty("author");
expect(gitInfo).toHaveProperty("authorDate");
expect(gitInfo).toHaveProperty("commitMessage");
expect(gitInfo).toHaveProperty("root");
expect(gitInfo).toHaveProperty("commonGitDir");
expect(gitInfo).toHaveProperty("worktreeGitDir");
expect(gitInfo).toHaveProperty("lastTag");
expect(gitInfo).toHaveProperty("commitsSinceLastTag");
expect(gitInfo).toHaveProperty("parents");

// By simple-git
expect(gitInfo).toHaveProperty("not_added");
expect(gitInfo).toHaveProperty("created");
expect(gitInfo).toHaveProperty("deleted");
expect(gitInfo).toHaveProperty("modified");
expect(gitInfo).toHaveProperty("renamed");
expect(gitInfo).toHaveProperty("staged");
});
});

0 comments on commit 3413a25

Please sign in to comment.