debt-collector
Version:
a nodejs tool to identify, track and mesure technical debt
50 lines (49 loc) • 1.9 kB
JavaScript
import { useEffect, useState } from 'react';
import { getIsHistoryDirty, getCurrentBranch, checkoutTo, getRevList, walkCommits, execWalkCommand, } from './gitUtils.js';
const useGitUtils = (config) => {
const [isGitReady, setIsGitReady] = useState(false);
const [currentBranch, setCurrentBranch] = useState(null);
const [isHistoryDirty, setIsHistoryDirty] = useState(null);
const [revList, setRevList] = useState(null);
const [gitErrors, setGitErrors] = useState([]);
useEffect(() => {
if (config) {
;
(async () => {
try {
const currentBranchRes = await getCurrentBranch();
const isHistoryDirtyRes = await getIsHistoryDirty();
setCurrentBranch(currentBranchRes);
setIsHistoryDirty(isHistoryDirtyRes);
if (config.walkConfig?.gitCommand && !!config.walkConfig?.parser) {
const { gitCommand, parser, limit } = config.walkConfig;
const revListRes = await getRevList(gitCommand, parser, limit);
setRevList(revListRes);
}
setIsGitReady(true);
}
catch (e) {
setGitErrors((prevErrors) => [
...prevErrors,
{
errorType: 'GIT_ERROR',
message: e instanceof Error ? e.message : 'Unknown error',
},
]);
setIsGitReady(true);
}
})();
}
}, [config]);
return {
isGitReady,
currentBranch,
isHistoryDirty,
checkoutTo,
revList,
gitErrors,
walkCommits,
execWalkCommand,
};
};
export default useGitUtils;