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: create project works on all systems, properly remove git folder #67

Merged
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
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