Skip to content

Commit

Permalink
fix: create project cross-platform compatibility, properly remove git…
Browse files Browse the repository at this point in the history
… folder (#67)
  • Loading branch information
JackHamer09 authored Oct 12, 2023
1 parent c83d1b5 commit a212f05
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions src/commands/create/create.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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(
[
{
Expand All @@ -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 });
Expand Down

0 comments on commit a212f05

Please sign in to comment.