Skip to content

Commit a212f05

Browse files
authored
fix: create project cross-platform compatibility, properly remove git folder (#67)
1 parent c83d1b5 commit a212f05

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/commands/create/create.ts

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import chalk from "chalk";
12
import { Option } from "commander";
3+
import fs from "fs";
24
import inquirer from "inquirer";
35
import path from "path";
46

57
import Program from "./command.js";
68
import { zeekOption } from "../../common/options.js";
79
import { track } from "../../utils/analytics.js";
10+
import { fileOrDirExists } from "../../utils/files.js";
11+
import { cloneRepo } from "../../utils/git.js";
812
import { optionNameToParam, executeCommand } from "../../utils/helpers.js";
913
import Logger from "../../utils/logger.js";
1014
import zeek from "../../utils/zeek.js";
@@ -41,6 +45,11 @@ export const handler = async (folderName: string, options: CreateOptions) => {
4145
};
4246
Logger.debug(`Initial create project options: ${JSON.stringify(options, null, 2)}`);
4347

48+
const folderLocation = path.join(process.cwd(), options.folderName!);
49+
if (fileOrDirExists(folderLocation)) {
50+
throw new Error(`Folder at ${folderLocation} already exists. Try a different project name or remove the folder.`);
51+
}
52+
4453
const answers: CreateOptions = await inquirer.prompt(
4554
[
4655
{
@@ -63,24 +72,43 @@ export const handler = async (folderName: string, options: CreateOptions) => {
6372

6473
const template = templates.find((e) => e.value === options.template)!;
6574

66-
Logger.info(`\nCreating new project from "${template.name}" template at "${path.join(options.folderName!, "/")}"`);
67-
await executeCommand(`git clone ${template.git} ${options.folderName}`);
68-
await executeCommand(`cd ${options.folderName} && rm -rf -r .git`); // removes .git folder so new repo can be initialized
69-
70-
Logger.info("\nInstalling dependencies with yarn...");
71-
await executeCommand(`cd ${options.folderName} && yarn`);
75+
Logger.info(`\nCreating new project from "${template.name}" template at "${folderLocation}"`);
76+
await cloneRepo(template.git, folderLocation);
77+
try {
78+
fs.rmdirSync(path.join(folderLocation, ".git"), { recursive: true });
79+
} catch {
80+
Logger.warn("Failed to remove .git folder. Make sure to remove it manually before pushing to a new repo.");
81+
}
7282

73-
Logger.info(`\nAll ready 🎉🎉
83+
const isYarnInstalled = await executeCommand("yarn --version", { silent: true })
84+
.then(() => true)
85+
.catch(() => false);
86+
87+
if (isYarnInstalled) {
88+
Logger.info("\nInstalling dependencies with yarn...");
89+
await executeCommand("yarn", { cwd: folderLocation });
90+
} else {
91+
Logger.warn("\nYarn is not installed. Install by running: `npm install -g yarn`");
92+
Logger.warn(
93+
`\nAfter installing Yarn, make sure to install dependencies by running: \`cd ${options.folderName} && yarn\``
94+
);
95+
}
7496

75-
Run cd ${options.folderName} to enter your project folder.
97+
Logger.info(`\n${chalk.green("🎉 All set up! 🎉")}
98+
99+
${chalk.magentaBright("Navigate to your project:")} cd ${options.folderName}
76100
77-
Contracts are stored in the /contracts folder.
78-
Deployment scripts go in the /deploy folder.
101+
${chalk.magentaBright("Directory Overview:")}
102+
- Contracts: ${path.join(folderName, "/contracts")}
103+
- Deployment Scripts: ${path.join(folderName, "/deploy")}
79104
80-
- "yarn hardhat compile" to compile your contracts.
81-
- "yarn hardhat deploy-zksync" to deploy your contract (this command accepts a --script option).
105+
${chalk.magentaBright("Commands:")}
106+
- Compile your contracts: \`yarn hardhat compile\`
107+
- Deploy your contract: \`yarn hardhat deploy-zksync\`
108+
- Note: You can use the \`--script\` option with this command.
82109
83-
Read the ${path.join(options.folderName!, "README.md")} file to learn more.
110+
${chalk.magentaBright("Further Reading:")}
111+
Check out the README file for more details: ${path.join(folderLocation, "README.md")}
84112
`);
85113

86114
track("create", { template: options.template, zeek: options.zeek });

0 commit comments

Comments
 (0)