From 69287ab1bd0c4960384144f90fea8ebded3b0cc5 Mon Sep 17 00:00:00 2001 From: SrIzan10 <66965250+SrIzan10@users.noreply.github.com> Date: Thu, 22 Sep 2022 09:08:49 +0200 Subject: [PATCH] fix: git not installed errors during init (#79) Co-authored-by: EvolutionX --- src/commands/init.ts | 22 +++++++++++----------- src/utilities/install.ts | 22 ++++++++++++++++------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/commands/init.ts b/src/commands/init.ts index acbaf3e..d214e43 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -1,4 +1,4 @@ -import { greenBright, redBright } from 'colorette'; +import { greenBright, redBright, underline } from 'colorette'; import { execa } from 'execa'; import { findUp } from 'find-up'; import ora from 'ora'; @@ -98,19 +98,19 @@ async function git(data: Data) { spinner: 'aesthetic', }).start(); - const exe = await execa('git', ['init', data.name]); - - await wait(300); - - if (!exe || exe?.failed) { + try { + await execa('git', ['init', data.name]); + await wait(300); + spin.succeed('Git initialized!'); + } catch (error) { spin.fail( - `${redBright('Failed')} to initialize git!` + - '\nMaybe you should run git init?' + `${redBright( + 'Failed' + )} to initialize git!\nTry to install it at ${underline( + 'https://git-scm.com' + )}\nSkipping for now.` ); - process.exit(1); } - - spin.succeed('Git initialized!'); } /** Wait for a specified number of milliseconds, then return a promise that resolves to undefined. */ diff --git a/src/utilities/install.ts b/src/utilities/install.ts index 436779d..f674a6a 100644 --- a/src/utilities/install.ts +++ b/src/utilities/install.ts @@ -36,6 +36,7 @@ export async function installDeps(choice: PackageManagerChoice, name: string) { const result = await execa(choice, ['install'], { cwd: process.cwd() + '/' + name, }).catch(() => null); + if (!result || result?.failed) { spin.fail(`${redBright('Failed')} to install dependencies!`); process.exit(1); @@ -48,12 +49,21 @@ export async function installDeps(choice: PackageManagerChoice, name: string) { * @param name - The name of the project */ export async function cloneRepo(lang: string, name: string) { - await execa('git', [ - 'clone', - `https://github.com/sern-handler/templates.git`, - ]); - copyRecursiveSync(`templates/templates/${lang}`, name); - fs.rmSync(`templates/`, { recursive: true, force: true }); + try { + await execa('git', [ + 'clone', + `https://github.com/sern-handler/templates.git`, + ]); + copyRecursiveSync(`templates/templates/${lang}`, name); + fs.rmSync(`templates/`, { recursive: true, force: true }); + } catch (error) { + console.log( + `${redBright( + '✖ Failed' + )} to clone github templates repo. Install git and try again!` + ); + process.exit(1); + } } /**