Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle failing template installation #78

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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