From 6f5514bafcb13b346a5a00646e4446f4ed3c0342 Mon Sep 17 00:00:00 2001 From: Caleb Tucker-Raymond Date: Fri, 7 Oct 2022 09:52:55 -0700 Subject: [PATCH] fix: gives user a specific error message when the repository URL does not end in .git --- src/handlers/systemInstall.ts | 22 +++++++++++++++------- src/util/getGitRepoNameFromUrl.test.ts | 12 +++++++----- src/util/getGitRepoNameFromUrl.ts | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/handlers/systemInstall.ts b/src/handlers/systemInstall.ts index d4fc799..08a9891 100644 --- a/src/handlers/systemInstall.ts +++ b/src/handlers/systemInstall.ts @@ -43,13 +43,21 @@ export async function getSystemRepoInfo( ): Promise<(GitCloneOptions & { name: string }) | void> { // If a repository and checkout were specified, use that to return system information. if (repository && checkout) { - const repoName = getGitRepoNameFromUrl(repository); - if (repoName) { - return { - name: repoName, - repository, - checkout, - }; + try { + const repoName = getGitRepoNameFromUrl(repository); + if (repoName) { + return { + name: repoName, + repository, + checkout, + }; + } + } catch (error: unknown) { + if (error instanceof Error) { + return log('error', error.message, EXIT_ERROR); + } else { + throw error; + } } } diff --git a/src/util/getGitRepoNameFromUrl.test.ts b/src/util/getGitRepoNameFromUrl.test.ts index c33bc7c..82d3581 100644 --- a/src/util/getGitRepoNameFromUrl.test.ts +++ b/src/util/getGitRepoNameFromUrl.test.ts @@ -17,11 +17,13 @@ describe('getGitRepoNameFromUrl', () => { ).toBe('emulsify-drupal'); }); - it('can return void if given an invalid git url', () => { + it('can throw an Error if given an invalid git url', () => { expect.assertions(2); - expect(getGitRepoNameFromUrl('')).toBe(undefined); - expect( - getGitRepoNameFromUrl('https://github.com/emulsify-ds/emulsify-drupal') - ).toBe(undefined); + expect(() => { + getGitRepoNameFromUrl(''); + }).toThrow(Error); + expect(() => { + getGitRepoNameFromUrl('https://github.com/emulsify-ds/emulsify-drupal'); + }).toThrow(Error); }); }); diff --git a/src/util/getGitRepoNameFromUrl.ts b/src/util/getGitRepoNameFromUrl.ts index ff3dc06..222723c 100644 --- a/src/util/getGitRepoNameFromUrl.ts +++ b/src/util/getGitRepoNameFromUrl.ts @@ -14,7 +14,7 @@ export default function getGitRepoNameFromUrl(url: string): string | void { // If no .git extension is provided, then this is an invalid git url. if (!gitName.includes('.git')) { - return undefined; + throw new Error('The repository URL must end in .git.'); } return R.head(gitName.split('.')); }