Skip to content

Commit baf8ef8

Browse files
committed
linted code
1 parent e463fca commit baf8ef8

11 files changed

+108
-90
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ npm-debug.log*
55
yarn-debug.log*
66
yarn-error.log*
77
lerna-debug.log*
8-
8+
my-dapp-[0-9]*
99
# Diagnostic reports (https://nodejs.org/api/report.html)
1010
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
1111

helpers/cleanUpFiles.ts

+17
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,21 @@ export const cleanUpFiles = () => {
1212
recursive: true,
1313
force: true,
1414
});
15+
fs.rmSync(path.join(process.cwd(), "types"), {
16+
recursive: true,
17+
force: true,
18+
});
19+
fs.rmSync(path.join(process.cwd(), ".eslintignore"), {
20+
force: true,
21+
});
22+
fs.rmSync(path.join(process.cwd(), ".eslintrc"), {
23+
force: true,
24+
});
25+
fs.rmSync(path.join(process.cwd(), "contributing.md"), {
26+
force: true,
27+
});
28+
fs.rmSync(path.join(process.cwd(), "yarn.lock"), {
29+
recursive: true,
30+
force: true,
31+
});
1532
};

helpers/cloneRepo.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import path from "path";
44
import fse from "fs-extra";
55
import chalk from "chalk";
66
import cliProgress from "cli-progress";
7-
import { createEnv } from "./createEnv.js";
87
import { setUpHardhat } from "./setupHardhat.js";
8+
import { dappInfo } from "../interfaces/dappInfo.js";
99

1010
export const cloneRepo = (
11-
projectPath,
12-
dappInfo
11+
projectPath : string,
12+
dappInfo : dappInfo
1313
) => {
1414
try {
1515
process.chdir(projectPath);
@@ -31,7 +31,7 @@ export const cloneRepo = (
3131

3232

3333

34-
let template = path.join(
34+
const template = path.join(
3535
process.cwd(),
3636
"templates",
3737
(dappInfo.chain == "ethereum" || dappInfo.chain == "polygon" || dappInfo.chain == "arbitrum"|| dappInfo.chain == "optimism") ? "ethereum" : "solana",
@@ -45,8 +45,9 @@ export const cloneRepo = (
4545

4646
bar1.stop();
4747

48-
if (dappInfo.wantsBackend) {
49-
switch (dappInfo.type) {
48+
if (dappInfo.useBackend) {
49+
console.log(chalk.yellow(`Copying ${dappInfo.backendProvider} files...`));
50+
switch (dappInfo.backendProvider) {
5051
case "hardhat":
5152
setUpHardhat(dappInfo)
5253
break;
@@ -56,10 +57,8 @@ export const cloneRepo = (
5657
case "Anchor":
5758
break;
5859
}
59-
60-
setUpHardhat(dappInfo)
6160
}
6261
} catch (e) {
63-
selfDestroy(e);
62+
selfDestroy();
6463
}
6564
};

helpers/createEnv.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import fs from "fs";
2+
import { APIKeys } from "../interfaces/dappInfo";
3+
import path from "path";
24

3-
export const createEnv = (keys, path) => {
4-
let env = {};
5+
export const createEnv = (apiKeys: APIKeys, projectPath = "./") => {
56

6-
if (keys.alchemyKey) {
7-
env["NEXT_PUBLIC_ALCHEMY_API_KEY"] = "demo";
8-
}
9-
if (Object.keys(env).length) {
10-
let writeStream = fs.createWriteStream(path.join(path,".env"));
11-
for (const [key, value] of Object.entries(env)) {
7+
console.log(apiKeys)
8+
9+
if (Object.keys(apiKeys).length) {
10+
const writeStream = fs.createWriteStream(path.join(projectPath, ".env"));
11+
for (const [key, value] of Object.entries(apiKeys)) {
1212
writeStream.write(`NEXT_PUBLIC_${key.toUpperCase()}= ${value}`);
1313
}
1414
writeStream.end();

helpers/createPackage.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ import fs from "fs";
22
import { execSync } from "child_process";
33
import chalk from "chalk";
44
import cliProgress from "cli-progress";
5+
import { selfDestroy } from "./selfDestroy";
56

6-
export const createPackageJson = async ( projectName: string, {isEVM, useBackend, backendProvider = ""}) => {
7+
interface packageData{
8+
isEVM: boolean,
9+
useBackend: boolean,
10+
backendProvider?: string
11+
}
12+
13+
export const createPackageJson = async ( projectName: string, {isEVM, useBackend, backendProvider = ""} : packageData) => {
714
try {
815
console.log(chalk.yellow("Generating package.json"));
916
const bar1 = new cliProgress.SingleBar(
@@ -12,7 +19,7 @@ export const createPackageJson = async ( projectName: string, {isEVM, useBackend
1219
);
1320
bar1.start(200, 0);
1421

15-
let packageJson = {
22+
const packageJson = {
1623
name: projectName,
1724
version: "0.1.0",
1825
private: true,
@@ -77,5 +84,7 @@ export const createPackageJson = async ( projectName: string, {isEVM, useBackend
7784
console.log(chalk.yellow("Installing dependencies..."));
7885
execSync("npm install");
7986
console.log(chalk.green("Dependencies installed"));
80-
} catch (e) {}
87+
} catch (e) {
88+
selfDestroy()
89+
}
8190
};

helpers/mkdir.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "fs"
33
export const mkdir = (projectPath: string) => {
44
try {
55
fs.mkdirSync(projectPath)
6-
} catch (e: any) {
6+
} catch (e) {
77
console.log(e)
88
}
99
}

helpers/selfDestroy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import fs from "fs";
22

33
let root = ""
44

5-
export const setRoot = (path) => {
5+
export const setRoot = (path: string) => {
66
root = path;
77
}
88

9-
export const selfDestroy = (e) => {
9+
export const selfDestroy = () => {
1010
// console.log("CURRENT WORKING DIRECTORY::", process.cwd())
1111
// console.log("ROOT::", root)
1212

helpers/setupHardhat.ts

+19-31
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,25 @@ import cliProgress from "cli-progress";
33
import path from "path";
44
import { createEnv } from "./createEnv.js";
55
import fse from "fs-extra";
6+
import { dappInfo } from "../interfaces/dappInfo.js";
67

7-
export const setUpHardhat = (dappInfo) => {
8-
console.log(chalk.yellow(`Copying ${dappInfo.type} files...`));
9-
const bar2 = new cliProgress.SingleBar(
10-
{},
11-
cliProgress.Presets.shades_classic
12-
);
13-
bar2.start(100, 0);
14-
bar2.update(50);
8+
export const setUpHardhat = (dappInfo: dappInfo) => {
9+
const bar2 = new cliProgress.SingleBar(
10+
{},
11+
cliProgress.Presets.shades_classic
12+
);
13+
bar2.start(100, 0);
14+
bar2.update(50);
1515

16-
switch (dappInfo.type) {
17-
case "hardhat":
18-
let hardhatTemplate = path.join(
19-
process.cwd(),
20-
"templates",
21-
"hardhat"
22-
);
23-
fse.mkdirSync(path.join(process.cwd(), "backend"));
24-
fse.copySync(hardhatTemplate, path.join(process.cwd(), "backend"));
25-
createEnv(dappInfo.APIKey, path.join(process.cwd(), "backend"))
26-
break;
27-
case "foundry":
28-
break;
29-
case "anchor":
30-
break;
31-
}
16+
const hardhatTemplate = path.join(process.cwd(), "templates", "hardhat");
17+
fse.mkdirSync(path.join(process.cwd(), "backend"));
18+
fse.copySync(hardhatTemplate, path.join(process.cwd(), "backend"));
3219

33-
bar2.update(100);
34-
bar2.stop();
35-
console.log(
36-
chalk.green("Smart Contract Development Environment copied ✅")
37-
);
38-
39-
}
20+
if (dappInfo.apiKeys) {
21+
createEnv(dappInfo.apiKeys, path.join(process.cwd(), "backend"));
22+
}
23+
24+
bar2.update(100);
25+
bar2.stop();
26+
console.log(chalk.green("Smart Contract Development Environment copied ✅"));
27+
};

index.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env node
2-
/* eslint-disable import/no-extraneous-dependencies */
32

43
import * as Commander from "commander";
54
import prompts from "prompts";
65
import path from "path";
76
import { createPackageJson } from "./helpers/createPackage.js";
8-
import { existsSync, fstat } from "fs";
7+
import { existsSync } from "fs";
98
import { mkdir } from "./helpers/mkdir.js";
109
import { cleanUpFiles } from "./helpers/cleanUpFiles.js";
1110
import { cloneRepo } from "./helpers/cloneRepo.js";
1211
import { selfDestroy, setRoot } from "./helpers/selfDestroy.js";
1312
import chalk from "chalk";
1413
import { createEnv } from "./helpers/createEnv.js";
14+
import { dappInfo } from "./interfaces/dappInfo.js"
1515

1616
console.log(`MMMMMMMMMMMMMMMMMK:..:KMMMMMMMMMMMMMMMMM
1717
MMMMMMMMMMMMMMMWO, ,OWMMMMMMMMMMMMMMM
@@ -34,20 +34,25 @@ l. 'kWNx. .l
3434
console.log("\n");
3535
console.log("🔵 Welcome to the create-web3-dapp wizard 🔵");
3636
console.log("\n");
37+
3738
let projectPath = "";
3839

40+
// Gets project name
3941
const program = new Commander.Command("create-web3-dapp")
4042
.version("0.1.0")
4143
.arguments("[project-directory]")
4244
.usage("<project-directory>")
43-
.action((name) => {
45+
.action((name: string) => {
4446
projectPath = name;
4547
})
4648
.option("--ts, --typescript", "Initialize as a TypeScript project")
4749
.parse(process.argv);
4850

51+
// Starts creation process
4952
async function run() {
5053
try {
54+
55+
// Checks if project name is provided
5156
if (typeof projectPath === "string") {
5257
projectPath = projectPath.trim();
5358
}
@@ -60,38 +65,39 @@ async function run() {
6065
}).then((data) => data.projectPath);
6166
}
6267

68+
//Reformat project's name
6369
projectPath = projectPath.trim().replace(/[\W_]+/g, "-");
64-
console.log(projectPath);
6570

66-
let resolvedProjectPath = path.resolve(projectPath);
67-
let dirExists = existsSync(resolvedProjectPath);
71+
let resolvedProjectPath: string = path.resolve(projectPath);
72+
let dirExists: boolean = existsSync(resolvedProjectPath);
6873
setRoot(resolvedProjectPath);
6974

75+
// Check if project
7076
while (dirExists) {
7177
projectPath = await prompts({
7278
type: "text",
7379
name: "projectPath",
7480
message:
7581
"A directory with this name already exists, please use a different name",
7682
initial: "my-dapp",
77-
}).then((data) => data.projectPath);
78-
console.log(projectPath);
83+
}).then((data) => data.projectPath.trim().replace(/[\W_]+/g, "-"));
7984
resolvedProjectPath = path.resolve(projectPath);
8085
dirExists = existsSync(resolvedProjectPath);
8186
console.log(dirExists);
8287
}
8388

8489
const projectName = path.basename(resolvedProjectPath);
8590

86-
let dappInfo: dappInfo = {
91+
const dappInfo: dappInfo = {
8792
chain: "",
8893
isEVM: true,
8994
isTestnet: false,
9095
useBackend: false,
9196
wantsTemplateFiles: false,
97+
apiKeys: {}
9298
};
9399

94-
const chain = await prompts({
100+
const chain: string = await prompts({
95101
type: "select",
96102
name: "chain",
97103
message: "For which VM are you building for?",
@@ -106,6 +112,7 @@ async function run() {
106112
}).then((data) => data.chain);
107113

108114
dappInfo.chain = chain;
115+
109116
dappInfo.isEVM =
110117
chain == "ethereum" ||
111118
chain == "polygon" ||
@@ -115,7 +122,7 @@ async function run() {
115122
: false;
116123

117124
if (dappInfo.chain === "ethereum" || dappInfo.chain === "polygon") {
118-
const isTestnet = await prompts({
125+
const isTestnet: boolean = await prompts({
119126
type: "toggle",
120127
name: "testnet",
121128
message: "Do you want to use a testnet?",
@@ -137,7 +144,7 @@ async function run() {
137144
}
138145
//TODO: Split in components selection
139146

140-
const wantsTemplateFiles = await prompts({
147+
const wantsTemplateFiles: boolean = await prompts({
141148
type: "toggle",
142149
name: "templateFiles",
143150
message: "Do you want to import the template files?",
@@ -148,7 +155,7 @@ async function run() {
148155

149156
dappInfo.wantsTemplateFiles = wantsTemplateFiles;
150157

151-
const useBackend = await prompts({
158+
const useBackend: boolean = await prompts({
152159
type: "toggle",
153160
name: "useBackend",
154161
message:
@@ -161,6 +168,7 @@ async function run() {
161168
dappInfo.useBackend = useBackend;
162169

163170
if (useBackend) {
171+
// set provider
164172
if (dappInfo.chain === "solana") {
165173
await prompts({
166174
type: "select",
@@ -183,27 +191,26 @@ async function run() {
183191
}
184192
}
185193

186-
let alchemyAPIKey = await prompts({
194+
const alchemyAPIKey: string = await prompts({
187195
type: "text",
188196
name: "apiKey",
189197
message: "Insert your Alchemy API Key (if none, 'demo' will be used",
190198
initial: "demo",
191199
}).then((data) => data.apiKey);
192200

193-
mkdir(resolvedProjectPath);
201+
dappInfo.apiKeys["alchemy_api_key"]= alchemyAPIKey
194202

203+
mkdir(resolvedProjectPath);
195204
cloneRepo(resolvedProjectPath, dappInfo);
196-
197205
createPackageJson(projectName, dappInfo);
198-
199-
createEnv(alchemyAPIKey, process.cwd());
206+
createEnv(dappInfo.apiKeys, process.cwd());
200207
cleanUpFiles();
201208

202209
console.log(
203210
chalk.green("Visit https://docs.alchemy.com/for the complete tutorial")
204211
);
205212
} catch (e) {
206-
selfDestroy(e);
213+
selfDestroy();
207214
}
208215
}
209216

0 commit comments

Comments
 (0)