From b036d3c4a6965654dd138487b0db69c37fb96567 Mon Sep 17 00:00:00 2001 From: Jack Hamer <47187316+JackHamer09@users.noreply.github.com> Date: Thu, 16 Nov 2023 16:50:36 +0300 Subject: [PATCH] fix: handle failing template installation (#78) --- src/commands/create/groups/scripting.ts | 1 + src/commands/create/utils.ts | 76 ++++++++++++++++--------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/commands/create/groups/scripting.ts b/src/commands/create/groups/scripting.ts index fb6bfee1..86ae94df 100644 --- a/src/commands/create/groups/scripting.ts +++ b/src/commands/create/groups/scripting.ts @@ -25,6 +25,7 @@ export const templates: Template[] = [ value: "node_ethers5", framework: "Node.js", ethereumFramework: "Ethers v5", + path: "templates/nodejs/ethers5", git: "https://github.com/matter-labs/zksync-scripting-templates", }, ]; diff --git a/src/commands/create/utils.ts b/src/commands/create/utils.ts index 8b6892e0..ba746bb8 100644 --- a/src/commands/create/utils.ts +++ b/src/commands/create/utils.ts @@ -101,47 +101,62 @@ export const setupTemplate = async ( Logger.info(`\nSetting up template in ${chalk.magentaBright(folderLocation)}...`); if (!template.path) { const spinner = ora("Cloning template...").start(); - await cloneRepo(template.git, folderLocation, { silent: true }); try { - fs.rmSync(path.join(folderLocation, ".git"), { recursive: true }); - } catch { - Logger.warn("Failed to remove .git folder. Make sure to remove it manually before pushing to a new repo."); - } - try { - const githubFolderLocation = path.join(folderLocation, ".github"); - if (fileOrDirExists(githubFolderLocation)) { - fs.rmSync(githubFolderLocation, { recursive: true }); + await cloneRepo(template.git, folderLocation, { silent: true }); + try { + fs.rmSync(path.join(folderLocation, ".git"), { recursive: true }); + } catch { + Logger.warn("Failed to remove .git folder. Make sure to remove it manually before pushing to a new repo."); } - } catch { - Logger.warn("Failed to remove .github folder. Make sure to remove it manually before pushing to a new repo."); + try { + const githubFolderLocation = path.join(folderLocation, ".github"); + if (fileOrDirExists(githubFolderLocation)) { + fs.rmSync(githubFolderLocation, { recursive: true }); + } + } catch { + Logger.warn("Failed to remove .github folder. Make sure to remove it manually before pushing to a new repo."); + } + spinner.succeed("Cloned template"); + } catch (error) { + spinner.fail("Failed to clone template"); + throw error; } - spinner.succeed("Cloned template"); } else { // We need to firstly clone the repo to a temp folder // then copy required folder to the main folder // then remove the temp folder const cloneTempPath = path.join(folderLocation, "___temp"); const spinner = ora("Cloning template...").start(); - await cloneRepo(template.git, path.join(folderLocation, "___temp"), { silent: true }); - - const templatePath = path.join(cloneTempPath, template.path); - if (fileOrDirExists(templatePath)) { - try { - // Copy the template to the folder location - copyRecursiveSync(templatePath, folderLocation); - // Remove the temp folder after copying - fs.rmSync(cloneTempPath, { recursive: true, force: true }); - } catch (err) { - throw new Error("An error occurred while copying the template"); + try { + await cloneRepo(template.git, path.join(folderLocation, "___temp"), { silent: true }); + + const templatePath = path.join(cloneTempPath, template.path); + if (fileOrDirExists(templatePath)) { + try { + // Copy the template to the folder location + copyRecursiveSync(templatePath, folderLocation); + // Remove the temp folder after copying + fs.rmSync(cloneTempPath, { recursive: true, force: true }); + } catch (err) { + throw new Error("An error occurred while copying the template"); + } + } else { + throw new Error(`The specified template path does not exist: ${templatePath}`); } - } else { - throw new Error(`The specified template path does not exist: ${templatePath}`); + spinner.succeed("Cloned template"); + } catch (error) { + spinner.fail("Failed to clone template"); + throw error; } - spinner.succeed("Cloned template"); } if (Object.keys(env).length > 0) { const spinner = ora("Setting up environment variables...").start(); - setupEnv(folderLocation, env); + try { + setupEnv(folderLocation, env); + } catch (error) { + spinner.fail("Failed to set up environment variables"); + throw error; + } spinner.succeed("Environment variables set up"); } @@ -149,7 +164,12 @@ export const setupTemplate = async ( `Installing dependencies with ${chalk.bold(packageManager)}... This may take a couple minutes.` ).start(); if (await packageManagers[packageManager].isInstalled()) { - await executeCommand(packageManagers[packageManager].install(), { cwd: folderLocation, silent: true }); + try { + await executeCommand(packageManagers[packageManager].install(), { cwd: folderLocation, silent: true }); + } catch (error) { + spinner.fail("Failed to install dependencies"); + throw error; + } spinner.succeed("Dependencies installed"); } else { spinner.fail(