Skip to content

Commit 91578b8

Browse files
committed
fix: add check to git repos and add handling errors
#28
1 parent 3caa468 commit 91578b8

10 files changed

Lines changed: 114 additions & 19 deletions

File tree

src/constants/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ export enum OPTIONS_WEEK_DAY_CODE_MAP {
3131
Fr = 5,
3232
Sa = 6,
3333
}
34+
35+
export enum CommandResultCode {
36+
Error = 1,
37+
Success = 0,
38+
}
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import {runCommand} from '../runCommand';
2+
import {CommandResultCode} from 'src/constants';
23

34
/**
45
* Get git repository name
56
*/
67
const getRepositoryName = async (): Promise<string> => {
78
const pathToRepo = await runCommand(`git rev-parse --show-toplevel`);
8-
const repoName = await runCommand(`basename ${pathToRepo}`);
99

10-
return repoName.trim();
10+
if (pathToRepo.code === CommandResultCode.Success) {
11+
const repoName = await runCommand(`basename ${pathToRepo.result}`);
12+
13+
if (repoName.code === CommandResultCode.Success) {
14+
return repoName.result.trim();
15+
}
16+
}
17+
18+
/**
19+
* TODO [MoonW1nd]: add debug loging
20+
*/
21+
return 'Not detected repository name';
1122
};
1223

1324
export default getRepositoryName;

src/helpers/git/getUserName.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import {runCommand} from '../runCommand';
2+
import {CommandResultCode} from 'src/constants';
23

34
/**
45
* Get git repository user name from config
56
*/
67
const getUserName = async (): Promise<string> => {
78
const author = await runCommand(`git config --get user.name`);
89

9-
return author.trim();
10+
if (author.code === CommandResultCode.Success) {
11+
return author.result.trim();
12+
}
13+
14+
return 'Not detected user name';
1015
};
1116

1217
export default getUserName;

src/helpers/runCommand.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
import {spawn, exec, SpawnOptionsWithoutStdio} from 'child_process';
2+
import {RunCommandResult} from '@project-types/common';
3+
import {CommandResultCode} from 'src/constants';
24

35
/**
46
* Run command with text output in terminal
57
*/
6-
export const runCommand = (command: string, options?: SpawnOptionsWithoutStdio): Promise<string> => {
8+
export const runCommand = (command: string, options?: SpawnOptionsWithoutStdio): Promise<RunCommandResult> => {
79
const [commandName, ...args] = command.split(' ');
810

911
return new Promise((resolve, reject) => {
1012
const spawnCommand = spawn(commandName, args, options);
1113
let result = '';
14+
let errorMessage = '';
1215

1316
spawnCommand.stdout.on('data', (data) => {
1417
result += data.toString();
1518
});
1619

20+
spawnCommand.stderr.on('data', (data) => {
21+
errorMessage += data.toString();
22+
});
23+
1724
spawnCommand.on('error', (error) => {
1825
console.log('error', error);
19-
reject(error);
26+
reject({code: CommandResultCode.Error, error: error.message});
2027
});
2128

2229
spawnCommand.on('close', (code) => {
23-
resolve(result);
30+
if (code) {
31+
resolve({code: CommandResultCode.Error, error: errorMessage});
32+
} else {
33+
resolve({code: CommandResultCode.Success, result});
34+
}
2435

2536
return code;
2637
});

src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@
44
*/
55
import 'module-alias/register';
66

7+
import {yellow} from 'chalk';
8+
import render from 'src/services/renderData/helpers/render';
79
import clear from 'clear';
810

911
import resolveGitData from './services/resolveGitData';
10-
import {resolveOptions} from 'src/services';
12+
import {resolveGitRepos, resolveOptions} from 'src/services';
1113
import renderData from './services/renderData';
1214

1315
const getLog = async (): Promise<void> => {
14-
const options = await resolveOptions();
15-
const {clearConsole} = options;
16+
const isGitRepo = await resolveGitRepos();
1617

17-
if (clearConsole) {
18-
clear();
19-
}
18+
if (isGitRepo) {
19+
const options = await resolveOptions();
20+
const {clearConsole} = options;
21+
22+
if (clearConsole) {
23+
clear();
24+
}
2025

21-
const gitData = await resolveGitData(options);
26+
const gitData = await resolveGitData(options);
2227

23-
renderData(gitData, options);
28+
renderData(gitData, options);
29+
} else {
30+
render('Not found git repository', {chalk: yellow});
31+
}
2432
};
2533

2634
getLog();

src/services/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export {default as resolveGitData} from './resolveGitData';
22
export {default as resolveOptions} from './resolveOptions';
3+
export {default as resolveGitRepos} from './resolveGitRepos';

src/services/resolveGitData/helpers/ensureBranchesCollection.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Url} from '@project-types/aliases';
22
import {BranchCollection, BranchRefType} from '@project-types/entities';
33
import {runCommand} from 'src/helpers';
4+
import {CommandResultCode} from 'src/constants';
45

56
interface ConstructRepositoryUrlParams {
67
branchName: string;
@@ -33,10 +34,18 @@ const ensureBranchesCollection = async (
3334
repositoryUrl: Url,
3435
): Promise<BranchCollection> => {
3536
const branchNames = Object.keys(branchCollection);
36-
const remoteNames = await Promise.all(
37+
const remoteNamesResults = await Promise.all(
3738
branchNames.map((branchName) => runCommand(`git config --get branch.${branchName}.merge`)),
3839
);
3940

41+
const remoteNames = remoteNamesResults.map((remoteNameData) => {
42+
if (remoteNameData.code === CommandResultCode.Success) {
43+
return remoteNameData.result;
44+
}
45+
46+
return 'Not detected remoteName';
47+
});
48+
4049
branchNames.map((branchName, i) => {
4150
const {refType} = branchCollection[branchName];
4251
const remoteBranchName = refType === 'tag' ? branchName : remoteNames[i].replace(/^refs\/heads\//gi, '').trim();

src/services/resolveGitData/helpers/ensureCommits.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import {Commit} from '@project-types/entities';
1+
import {Commit, BranchInfo} from '@project-types/entities';
22
import {GitLogCommit} from '@project-types/gitlog';
33
import {getBranchInfoByCommitHash, runCommand, getDateRangeGitLogOptions, getCommitReflog} from 'src/helpers';
44
import {Options} from '@project-types/options';
5+
import {CommandResultCode} from 'src/constants';
56

67
const ensureCommits = async (commits: GitLogCommit[], options: Options): Promise<Commit[]> => {
78
const gitLogDataWithSource = await runCommand(
@@ -13,12 +14,16 @@ const ensureCommits = async (commits: GitLogCommit[], options: Options): Promise
1314
*/
1415
// const reflogData = await runCommand(`git log -g --all --pretty=oneline ${getDateRangeGitLogOptions(options)}`);
1516

16-
const branchNames = commits.map((commit) => getBranchInfoByCommitHash(commit.hash, gitLogDataWithSource));
17+
let branchNames: BranchInfo[] = [];
18+
19+
if (gitLogDataWithSource.code === CommandResultCode.Success) {
20+
branchNames = commits.map((commit) => getBranchInfoByCommitHash(commit.hash, gitLogDataWithSource.result));
21+
}
1722

1823
return commits.map((commit, i) => ({
1924
...commit,
2025
reflog: getCommitReflog(commit.abbrevHash, ''),
21-
branchInfo: branchNames[i],
26+
branchInfo: branchNames[i] || {name: 'Not detected branch name'},
2227
}));
2328
};
2429

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {runCommand} from 'src/helpers';
2+
import {red} from 'chalk';
3+
import {CommandResultCode} from 'src/constants';
4+
import render from 'src/services/renderData/helpers/render';
5+
6+
const isNotGitRepositoryError = (message: string): boolean => /not a git repository/.test(message);
7+
8+
const resolveGitRepos = async (): Promise<boolean> => {
9+
const gitRepoResult = await runCommand('git rev-parse --git-dir');
10+
11+
if (gitRepoResult.code === CommandResultCode.Success) {
12+
return true;
13+
} else {
14+
if (!isNotGitRepositoryError(gitRepoResult.error)) {
15+
/**
16+
* TODO [MoonW1nd]: сreate serveis for print errors ans warns
17+
*/
18+
render(
19+
`[Error]: Somthing went wrong, please contact with develpers: https://github.com/MoonW1nd/codestory/issues`,
20+
{chalk: red},
21+
);
22+
}
23+
24+
return false;
25+
}
26+
};
27+
28+
export default resolveGitRepos;

src/types/common.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
import {OPTIONS_WEEK_DAY_CODE_MAP} from 'src/constants';
1+
import {OPTIONS_WEEK_DAY_CODE_MAP, CommandResultCode} from 'src/constants';
22

33
export type WeekDayCode = '0' | '1' | '2' | '3' | '4' | '5' | '6';
44

55
export type OptionsWeekDay = keyof typeof OPTIONS_WEEK_DAY_CODE_MAP;
66

77
export type WorkingDayMap = Record<WeekDayCode, boolean | null>;
8+
9+
type SuccessResult = {
10+
code: CommandResultCode.Success;
11+
result: string;
12+
};
13+
14+
type ErrorResult = {
15+
code: CommandResultCode.Error;
16+
error: string;
17+
};
18+
19+
export type RunCommandResult = SuccessResult | ErrorResult;

0 commit comments

Comments
 (0)