Open matt-degraffenreid opened 1 year ago
Opencommit only works with git hooks.
Refactor the code to add some dynamic logic to the tool
Identify hooks' path in the repo you are working in. This can be accomplished with the following command:
git config core.hooksPath
This needed to be translated into a util function for git so I wrote the following function:
export const getCoreHooksPath = async(): Promise<string> => { const { stdout } = await execa('git', [ 'config', 'core.hooksPath']); return stdout; }
Note: the function throws an error when there is no set path that needs to be caught when used.
Determine the path by invoking getCoreHooksPath catch the error and give the DEFAULT_SYMLINK_URL if core.hooksPath is not set.
getCoreHooksPath
DEFAULT_SYMLINK_URL
core.hooksPath
const getHooksPath = async (): Promise<string> => { try { const hooksPath = await getCoreHooksPath(); return `${hooksPath}/${HOOK_NAME}`; } catch (error) { return DEFAULT_SYMLINK_URL; } };
Refactor our two helper functions to utilize the getHooksPath function.
getHooksPath
export const isHookCalled = async (): Promise<boolean> => { const hooksPath = await getHooksPath(); return process.argv[1].endsWith(hooksPath); }; const isHookExists = async (): Promise<boolean> => { const hooksPath = await getHooksPath(); return existsSync(hooksPath); };
Replace the variable calls isHookCalled and isHookExists with the function call await isHookCalled() and await isHookExists() respectively.
isHookCalled
isHookExists
await isHookCalled()
await isHookExists()
When core.hooksPath is not set
When core.hooksPath is set to .husky
Stale issue message
Problem:
Opencommit only works with git hooks.
Solution:
Refactor the code to add some dynamic logic to the tool
Explanation:
Identify hooks' path in the repo you are working in. This can be accomplished with the following command:
git config core.hooksPath
This needed to be translated into a util function for git so I wrote the following function:
Note: the function throws an error when there is no set path that needs to be caught when used.
Determine the path by invoking
getCoreHooksPath
catch the error and give theDEFAULT_SYMLINK_URL
ifcore.hooksPath
is not set.Refactor our two helper functions to utilize the
getHooksPath
function.Replace the variable calls
isHookCalled
andisHookExists
with the function callawait isHookCalled()
andawait isHookExists()
respectively.Scenarios Tested
When core.hooksPath is not set
When core.hooksPath is set to .husky