Skip to content

Commit

Permalink
🚸 Handles execa errors in a generic way
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Zarco committed Mar 20, 2024
1 parent 7fd39df commit 29b67f3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 46 deletions.
19 changes: 4 additions & 15 deletions src/commands/commit/withClient/index.js
Expand Up @@ -5,18 +5,9 @@ import chalk from 'chalk'
import isHookCreated from '@utils/isHookCreated'
import configurationVault from '@utils/configurationVault'
import { type Answers } from '../prompts'
import isGitRepository from '@utils/isGitRepository'

const withClient = async (answers: Answers): Promise<void> => {
try {
if(!isGitRepository()) {
return console.log(
chalk.red(
"\nError: Seems that you're trying to commit outside a git repository \n" +
'Please navigate to a git repository to use `gitmoji` \n'
)
)
}
const scope = answers.scope ? `(${answers.scope}): ` : ''
const title = `${answers.gitmoji} ${scope}${answers.title}`
const isAutoAddEnabled = configurationVault.getAutoAdd()
Expand Down Expand Up @@ -50,12 +41,10 @@ const withClient = async (answers: Answers): Promise<void> => {
} catch (error) {
console.error(
chalk.red(
'\n',
'Oops! An error occurred. There is likely additional logging output above.\n',
'You can run the same commit with this command:\n'
),
'\t',
error.escapedCommand
error,
'\n\n',
'Oops! An error occurred. There is likely additional logging output above.\n'
)
)
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/utils/isGitRepository.js

This file was deleted.

14 changes: 5 additions & 9 deletions src/utils/isHookCreated.js
Expand Up @@ -5,17 +5,13 @@ import HOOK from '@commands/hook/hook'
import getAbsoluteHooksPath from './getAbsoluteHooksPath'

const isHookCreated = async (): Promise<?boolean> => {
try {
const hookFile = await getAbsoluteHooksPath(HOOK.FILENAME)
const hookFile = await getAbsoluteHooksPath(HOOK.FILENAME)

if (fs.existsSync(hookFile)) {
return fs.readFileSync(hookFile, { encoding: 'utf-8' }) === HOOK.CONTENTS
}

return false
} catch (error) {
console.error(error)
if (fs.existsSync(hookFile)) {
return fs.readFileSync(hookFile, { encoding: 'utf-8' }) === HOOK.CONTENTS
}

return false
}

export default isHookCreated
10 changes: 1 addition & 9 deletions test/commands/commit.spec.js
Expand Up @@ -142,15 +142,7 @@ describe('commit command', () => {
})

it('should print the error to the console', () => {
expect(consoleError).toHaveBeenCalledWith(
chalk.red(
'\n',
'Oops! An error occurred. There is likely additional logging output above.\n',
'You can run the same commit with this command:\n'
),
'\t',
undefined
)
expect(consoleError).toHaveBeenCalled()
})
})
})
Expand Down

0 comments on commit 29b67f3

Please sign in to comment.