Skip to content

Commit b036d3c

Browse files
authored
fix: handle failing template installation (#78)
1 parent c303548 commit b036d3c

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

src/commands/create/groups/scripting.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const templates: Template[] = [
2525
value: "node_ethers5",
2626
framework: "Node.js",
2727
ethereumFramework: "Ethers v5",
28+
path: "templates/nodejs/ethers5",
2829
git: "https://github.com/matter-labs/zksync-scripting-templates",
2930
},
3031
];

src/commands/create/utils.ts

+48-28
Original file line numberDiff line numberDiff line change
@@ -101,55 +101,75 @@ export const setupTemplate = async (
101101
Logger.info(`\nSetting up template in ${chalk.magentaBright(folderLocation)}...`);
102102
if (!template.path) {
103103
const spinner = ora("Cloning template...").start();
104-
await cloneRepo(template.git, folderLocation, { silent: true });
105104
try {
106-
fs.rmSync(path.join(folderLocation, ".git"), { recursive: true });
107-
} catch {
108-
Logger.warn("Failed to remove .git folder. Make sure to remove it manually before pushing to a new repo.");
109-
}
110-
try {
111-
const githubFolderLocation = path.join(folderLocation, ".github");
112-
if (fileOrDirExists(githubFolderLocation)) {
113-
fs.rmSync(githubFolderLocation, { recursive: true });
105+
await cloneRepo(template.git, folderLocation, { silent: true });
106+
try {
107+
fs.rmSync(path.join(folderLocation, ".git"), { recursive: true });
108+
} catch {
109+
Logger.warn("Failed to remove .git folder. Make sure to remove it manually before pushing to a new repo.");
114110
}
115-
} catch {
116-
Logger.warn("Failed to remove .github folder. Make sure to remove it manually before pushing to a new repo.");
111+
try {
112+
const githubFolderLocation = path.join(folderLocation, ".github");
113+
if (fileOrDirExists(githubFolderLocation)) {
114+
fs.rmSync(githubFolderLocation, { recursive: true });
115+
}
116+
} catch {
117+
Logger.warn("Failed to remove .github folder. Make sure to remove it manually before pushing to a new repo.");
118+
}
119+
spinner.succeed("Cloned template");
120+
} catch (error) {
121+
spinner.fail("Failed to clone template");
122+
throw error;
117123
}
118-
spinner.succeed("Cloned template");
119124
} else {
120125
// We need to firstly clone the repo to a temp folder
121126
// then copy required folder to the main folder
122127
// then remove the temp folder
123128
const cloneTempPath = path.join(folderLocation, "___temp");
124129
const spinner = ora("Cloning template...").start();
125-
await cloneRepo(template.git, path.join(folderLocation, "___temp"), { silent: true });
126-
127-
const templatePath = path.join(cloneTempPath, template.path);
128-
if (fileOrDirExists(templatePath)) {
129-
try {
130-
// Copy the template to the folder location
131-
copyRecursiveSync(templatePath, folderLocation);
132-
// Remove the temp folder after copying
133-
fs.rmSync(cloneTempPath, { recursive: true, force: true });
134-
} catch (err) {
135-
throw new Error("An error occurred while copying the template");
130+
try {
131+
await cloneRepo(template.git, path.join(folderLocation, "___temp"), { silent: true });
132+
133+
const templatePath = path.join(cloneTempPath, template.path);
134+
if (fileOrDirExists(templatePath)) {
135+
try {
136+
// Copy the template to the folder location
137+
copyRecursiveSync(templatePath, folderLocation);
138+
// Remove the temp folder after copying
139+
fs.rmSync(cloneTempPath, { recursive: true, force: true });
140+
} catch (err) {
141+
throw new Error("An error occurred while copying the template");
142+
}
143+
} else {
144+
throw new Error(`The specified template path does not exist: ${templatePath}`);
136145
}
137-
} else {
138-
throw new Error(`The specified template path does not exist: ${templatePath}`);
146+
spinner.succeed("Cloned template");
147+
} catch (error) {
148+
spinner.fail("Failed to clone template");
149+
throw error;
139150
}
140-
spinner.succeed("Cloned template");
141151
}
142152
if (Object.keys(env).length > 0) {
143153
const spinner = ora("Setting up environment variables...").start();
144-
setupEnv(folderLocation, env);
154+
try {
155+
setupEnv(folderLocation, env);
156+
} catch (error) {
157+
spinner.fail("Failed to set up environment variables");
158+
throw error;
159+
}
145160
spinner.succeed("Environment variables set up");
146161
}
147162

148163
const spinner = ora(
149164
`Installing dependencies with ${chalk.bold(packageManager)}... This may take a couple minutes.`
150165
).start();
151166
if (await packageManagers[packageManager].isInstalled()) {
152-
await executeCommand(packageManagers[packageManager].install(), { cwd: folderLocation, silent: true });
167+
try {
168+
await executeCommand(packageManagers[packageManager].install(), { cwd: folderLocation, silent: true });
169+
} catch (error) {
170+
spinner.fail("Failed to install dependencies");
171+
throw error;
172+
}
153173
spinner.succeed("Dependencies installed");
154174
} else {
155175
spinner.fail(

0 commit comments

Comments
 (0)