From 1e582eda1ec9a67a24c24b1a511a1f394c58d617 Mon Sep 17 00:00:00 2001 From: Miguel Zarco Date: Tue, 12 Mar 2024 14:22:45 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=9A=B8=20Warn=20user=20if=20they=20ar?= =?UTF-8?q?e=20not=20in=20a=20git=20repository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The change introduces a check to bail out if the user runs the tool outside a git repository. It will also warn the user. This handles this common pitfall gracefully instead of logging the stdout errors that happen in these cases. --- src/commands/commit/withClient/index.js | 9 +++++++++ src/utils/isGitRepository.js | 13 +++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/utils/isGitRepository.js diff --git a/src/commands/commit/withClient/index.js b/src/commands/commit/withClient/index.js index c8648838ab..88bfe7852f 100644 --- a/src/commands/commit/withClient/index.js +++ b/src/commands/commit/withClient/index.js @@ -5,9 +5,18 @@ 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 => { 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() diff --git a/src/utils/isGitRepository.js b/src/utils/isGitRepository.js new file mode 100644 index 0000000000..97841db4ec --- /dev/null +++ b/src/utils/isGitRepository.js @@ -0,0 +1,13 @@ +// @flow +import { execa } from 'execa' + +const isGitRepository = async (): Promise => { + try { + await execa('git', ['rev-parse', '--is-inside-work-tree']) + return true + } catch (_) { + return false + } +} + +export default isGitRepository From c8f8af3c85b47c5e7df283b2df1783f50410d5f5 Mon Sep 17 00:00:00 2001 From: Miguel Zarco Date: Wed, 20 Mar 2024 10:33:42 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=9A=B8=20Handles=20execa=20errors=20i?= =?UTF-8?q?n=20a=20generic=20way?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/commit/withClient/index.js | 19 ++++--------------- src/utils/isGitRepository.js | 13 ------------- src/utils/isHookCreated.js | 14 +++++--------- test/commands/commit.spec.js | 10 +--------- 4 files changed, 10 insertions(+), 46 deletions(-) delete mode 100644 src/utils/isGitRepository.js diff --git a/src/commands/commit/withClient/index.js b/src/commands/commit/withClient/index.js index 88bfe7852f..e8ab7525d0 100644 --- a/src/commands/commit/withClient/index.js +++ b/src/commands/commit/withClient/index.js @@ -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 => { 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() @@ -50,12 +41,10 @@ const withClient = async (answers: Answers): Promise => { } 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' + ) ) } } diff --git a/src/utils/isGitRepository.js b/src/utils/isGitRepository.js deleted file mode 100644 index 97841db4ec..0000000000 --- a/src/utils/isGitRepository.js +++ /dev/null @@ -1,13 +0,0 @@ -// @flow -import { execa } from 'execa' - -const isGitRepository = async (): Promise => { - try { - await execa('git', ['rev-parse', '--is-inside-work-tree']) - return true - } catch (_) { - return false - } -} - -export default isGitRepository diff --git a/src/utils/isHookCreated.js b/src/utils/isHookCreated.js index b79e1b2dda..879cab0d8e 100644 --- a/src/utils/isHookCreated.js +++ b/src/utils/isHookCreated.js @@ -5,17 +5,13 @@ import HOOK from '@commands/hook/hook' import getAbsoluteHooksPath from './getAbsoluteHooksPath' const isHookCreated = async (): Promise => { - 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 diff --git a/test/commands/commit.spec.js b/test/commands/commit.spec.js index 8fcd58d577..04f1c09fbb 100644 --- a/test/commands/commit.spec.js +++ b/test/commands/commit.spec.js @@ -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() }) }) })