1
+ import chalk from "chalk" ;
1
2
import { Option } from "commander" ;
3
+ import fs from "fs" ;
2
4
import inquirer from "inquirer" ;
3
5
import path from "path" ;
4
6
5
7
import Program from "./command.js" ;
6
8
import { zeekOption } from "../../common/options.js" ;
7
9
import { track } from "../../utils/analytics.js" ;
10
+ import { fileOrDirExists } from "../../utils/files.js" ;
11
+ import { cloneRepo } from "../../utils/git.js" ;
8
12
import { optionNameToParam , executeCommand } from "../../utils/helpers.js" ;
9
13
import Logger from "../../utils/logger.js" ;
10
14
import zeek from "../../utils/zeek.js" ;
@@ -41,6 +45,11 @@ export const handler = async (folderName: string, options: CreateOptions) => {
41
45
} ;
42
46
Logger . debug ( `Initial create project options: ${ JSON . stringify ( options , null , 2 ) } ` ) ;
43
47
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
+
44
53
const answers : CreateOptions = await inquirer . prompt (
45
54
[
46
55
{
@@ -63,24 +72,43 @@ export const handler = async (folderName: string, options: CreateOptions) => {
63
72
64
73
const template = templates . find ( ( e ) => e . value === options . template ) ! ;
65
74
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
+ }
72
82
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
+ }
74
96
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 }
76
100
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" ) }
79
104
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.
82
109
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" ) }
84
112
` ) ;
85
113
86
114
track ( "create" , { template : options . template , zeek : options . zeek } ) ;
0 commit comments