From a212f0525aebd508da0698ffca68b1a684a05b72 Mon Sep 17 00:00:00 2001 From: Jack Hamer <47187316+JackHamer09@users.noreply.github.com> Date: Thu, 12 Oct 2023 13:01:45 +0300 Subject: [PATCH] fix: create project cross-platform compatibility, properly remove git folder (#67) --- src/commands/create/create.ts | 54 ++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/src/commands/create/create.ts b/src/commands/create/create.ts index 019ecb23..4baeb4b5 100644 --- a/src/commands/create/create.ts +++ b/src/commands/create/create.ts @@ -1,10 +1,14 @@ +import chalk from "chalk"; import { Option } from "commander"; +import fs from "fs"; import inquirer from "inquirer"; import path from "path"; import Program from "./command.js"; import { zeekOption } from "../../common/options.js"; import { track } from "../../utils/analytics.js"; +import { fileOrDirExists } from "../../utils/files.js"; +import { cloneRepo } from "../../utils/git.js"; import { optionNameToParam, executeCommand } from "../../utils/helpers.js"; import Logger from "../../utils/logger.js"; import zeek from "../../utils/zeek.js"; @@ -41,6 +45,11 @@ export const handler = async (folderName: string, options: CreateOptions) => { }; Logger.debug(`Initial create project options: ${JSON.stringify(options, null, 2)}`); + const folderLocation = path.join(process.cwd(), options.folderName!); + if (fileOrDirExists(folderLocation)) { + throw new Error(`Folder at ${folderLocation} already exists. Try a different project name or remove the folder.`); + } + const answers: CreateOptions = await inquirer.prompt( [ { @@ -63,24 +72,43 @@ export const handler = async (folderName: string, options: CreateOptions) => { const template = templates.find((e) => e.value === options.template)!; - Logger.info(`\nCreating new project from "${template.name}" template at "${path.join(options.folderName!, "/")}"`); - await executeCommand(`git clone ${template.git} ${options.folderName}`); - await executeCommand(`cd ${options.folderName} && rm -rf -r .git`); // removes .git folder so new repo can be initialized - - Logger.info("\nInstalling dependencies with yarn..."); - await executeCommand(`cd ${options.folderName} && yarn`); + Logger.info(`\nCreating new project from "${template.name}" template at "${folderLocation}"`); + await cloneRepo(template.git, folderLocation); + try { + fs.rmdirSync(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."); + } - Logger.info(`\nAll ready 🎉🎉 + const isYarnInstalled = await executeCommand("yarn --version", { silent: true }) + .then(() => true) + .catch(() => false); + + if (isYarnInstalled) { + Logger.info("\nInstalling dependencies with yarn..."); + await executeCommand("yarn", { cwd: folderLocation }); + } else { + Logger.warn("\nYarn is not installed. Install by running: `npm install -g yarn`"); + Logger.warn( + `\nAfter installing Yarn, make sure to install dependencies by running: \`cd ${options.folderName} && yarn\`` + ); + } -Run cd ${options.folderName} to enter your project folder. + Logger.info(`\n${chalk.green("🎉 All set up! 🎉")} + +${chalk.magentaBright("Navigate to your project:")} cd ${options.folderName} -Contracts are stored in the /contracts folder. -Deployment scripts go in the /deploy folder. +${chalk.magentaBright("Directory Overview:")} +- Contracts: ${path.join(folderName, "/contracts")} +- Deployment Scripts: ${path.join(folderName, "/deploy")} -- "yarn hardhat compile" to compile your contracts. -- "yarn hardhat deploy-zksync" to deploy your contract (this command accepts a --script option). +${chalk.magentaBright("Commands:")} +- Compile your contracts: \`yarn hardhat compile\` +- Deploy your contract: \`yarn hardhat deploy-zksync\` + - Note: You can use the \`--script\` option with this command. -Read the ${path.join(options.folderName!, "README.md")} file to learn more. +${chalk.magentaBright("Further Reading:")} +Check out the README file for more details: ${path.join(folderLocation, "README.md")} `); track("create", { template: options.template, zeek: options.zeek });