Skip to content

Commit

Permalink
fix: handle failing template installation (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 authored Nov 16, 2023
1 parent c303548 commit b036d3c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/commands/create/groups/scripting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
];
Expand Down
76 changes: 48 additions & 28 deletions src/commands/create/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,55 +101,75 @@ 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");
}

const spinner = ora(
`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(
Expand Down

0 comments on commit b036d3c

Please sign in to comment.