1
1
#!/usr/bin/env node
2
- /* eslint-disable import/no-extraneous-dependencies */
3
2
4
3
import * as Commander from "commander" ;
5
4
import prompts from "prompts" ;
6
5
import path from "path" ;
7
6
import { createPackageJson } from "./helpers/createPackage.js" ;
8
- import { existsSync , fstat } from "fs" ;
7
+ import { existsSync } from "fs" ;
9
8
import { mkdir } from "./helpers/mkdir.js" ;
10
9
import { cleanUpFiles } from "./helpers/cleanUpFiles.js" ;
11
10
import { cloneRepo } from "./helpers/cloneRepo.js" ;
12
11
import { selfDestroy , setRoot } from "./helpers/selfDestroy.js" ;
13
12
import chalk from "chalk" ;
14
13
import { createEnv } from "./helpers/createEnv.js" ;
14
+ import { dappInfo } from "./interfaces/dappInfo.js"
15
15
16
16
console . log ( `MMMMMMMMMMMMMMMMMK:..:KMMMMMMMMMMMMMMMMM
17
17
MMMMMMMMMMMMMMMWO, ,OWMMMMMMMMMMMMMMM
@@ -34,20 +34,25 @@ l. 'kWNx. .l
34
34
console . log ( "\n" ) ;
35
35
console . log ( "🔵 Welcome to the create-web3-dapp wizard 🔵" ) ;
36
36
console . log ( "\n" ) ;
37
+
37
38
let projectPath = "" ;
38
39
40
+ // Gets project name
39
41
const program = new Commander . Command ( "create-web3-dapp" )
40
42
. version ( "0.1.0" )
41
43
. arguments ( "[project-directory]" )
42
44
. usage ( "<project-directory>" )
43
- . action ( ( name ) => {
45
+ . action ( ( name : string ) => {
44
46
projectPath = name ;
45
47
} )
46
48
. option ( "--ts, --typescript" , "Initialize as a TypeScript project" )
47
49
. parse ( process . argv ) ;
48
50
51
+ // Starts creation process
49
52
async function run ( ) {
50
53
try {
54
+
55
+ // Checks if project name is provided
51
56
if ( typeof projectPath === "string" ) {
52
57
projectPath = projectPath . trim ( ) ;
53
58
}
@@ -60,38 +65,39 @@ async function run() {
60
65
} ) . then ( ( data ) => data . projectPath ) ;
61
66
}
62
67
68
+ //Reformat project's name
63
69
projectPath = projectPath . trim ( ) . replace ( / [ \W _ ] + / g, "-" ) ;
64
- console . log ( projectPath ) ;
65
70
66
- let resolvedProjectPath = path . resolve ( projectPath ) ;
67
- let dirExists = existsSync ( resolvedProjectPath ) ;
71
+ let resolvedProjectPath : string = path . resolve ( projectPath ) ;
72
+ let dirExists : boolean = existsSync ( resolvedProjectPath ) ;
68
73
setRoot ( resolvedProjectPath ) ;
69
74
75
+ // Check if project
70
76
while ( dirExists ) {
71
77
projectPath = await prompts ( {
72
78
type : "text" ,
73
79
name : "projectPath" ,
74
80
message :
75
81
"A directory with this name already exists, please use a different name" ,
76
82
initial : "my-dapp" ,
77
- } ) . then ( ( data ) => data . projectPath ) ;
78
- console . log ( projectPath ) ;
83
+ } ) . then ( ( data ) => data . projectPath . trim ( ) . replace ( / [ \W _ ] + / g, "-" ) ) ;
79
84
resolvedProjectPath = path . resolve ( projectPath ) ;
80
85
dirExists = existsSync ( resolvedProjectPath ) ;
81
86
console . log ( dirExists ) ;
82
87
}
83
88
84
89
const projectName = path . basename ( resolvedProjectPath ) ;
85
90
86
- let dappInfo : dappInfo = {
91
+ const dappInfo : dappInfo = {
87
92
chain : "" ,
88
93
isEVM : true ,
89
94
isTestnet : false ,
90
95
useBackend : false ,
91
96
wantsTemplateFiles : false ,
97
+ apiKeys : { }
92
98
} ;
93
99
94
- const chain = await prompts ( {
100
+ const chain : string = await prompts ( {
95
101
type : "select" ,
96
102
name : "chain" ,
97
103
message : "For which VM are you building for?" ,
@@ -106,6 +112,7 @@ async function run() {
106
112
} ) . then ( ( data ) => data . chain ) ;
107
113
108
114
dappInfo . chain = chain ;
115
+
109
116
dappInfo . isEVM =
110
117
chain == "ethereum" ||
111
118
chain == "polygon" ||
@@ -115,7 +122,7 @@ async function run() {
115
122
: false ;
116
123
117
124
if ( dappInfo . chain === "ethereum" || dappInfo . chain === "polygon" ) {
118
- const isTestnet = await prompts ( {
125
+ const isTestnet : boolean = await prompts ( {
119
126
type : "toggle" ,
120
127
name : "testnet" ,
121
128
message : "Do you want to use a testnet?" ,
@@ -137,7 +144,7 @@ async function run() {
137
144
}
138
145
//TODO: Split in components selection
139
146
140
- const wantsTemplateFiles = await prompts ( {
147
+ const wantsTemplateFiles : boolean = await prompts ( {
141
148
type : "toggle" ,
142
149
name : "templateFiles" ,
143
150
message : "Do you want to import the template files?" ,
@@ -148,7 +155,7 @@ async function run() {
148
155
149
156
dappInfo . wantsTemplateFiles = wantsTemplateFiles ;
150
157
151
- const useBackend = await prompts ( {
158
+ const useBackend : boolean = await prompts ( {
152
159
type : "toggle" ,
153
160
name : "useBackend" ,
154
161
message :
@@ -161,6 +168,7 @@ async function run() {
161
168
dappInfo . useBackend = useBackend ;
162
169
163
170
if ( useBackend ) {
171
+ // set provider
164
172
if ( dappInfo . chain === "solana" ) {
165
173
await prompts ( {
166
174
type : "select" ,
@@ -183,27 +191,26 @@ async function run() {
183
191
}
184
192
}
185
193
186
- let alchemyAPIKey = await prompts ( {
194
+ const alchemyAPIKey : string = await prompts ( {
187
195
type : "text" ,
188
196
name : "apiKey" ,
189
197
message : "Insert your Alchemy API Key (if none, 'demo' will be used" ,
190
198
initial : "demo" ,
191
199
} ) . then ( ( data ) => data . apiKey ) ;
192
200
193
- mkdir ( resolvedProjectPath ) ;
201
+ dappInfo . apiKeys [ "alchemy_api_key" ] = alchemyAPIKey
194
202
203
+ mkdir ( resolvedProjectPath ) ;
195
204
cloneRepo ( resolvedProjectPath , dappInfo ) ;
196
-
197
205
createPackageJson ( projectName , dappInfo ) ;
198
-
199
- createEnv ( alchemyAPIKey , process . cwd ( ) ) ;
206
+ createEnv ( dappInfo . apiKeys , process . cwd ( ) ) ;
200
207
cleanUpFiles ( ) ;
201
208
202
209
console . log (
203
210
chalk . green ( "Visit https://docs.alchemy.com/for the complete tutorial" )
204
211
) ;
205
212
} catch ( e ) {
206
- selfDestroy ( e ) ;
213
+ selfDestroy ( ) ;
207
214
}
208
215
}
209
216
0 commit comments