forked from josip0920-zz/create-truffle-dapp-master
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTruffleDapp.js
235 lines (199 loc) · 7 KB
/
createTruffleDapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#! /usr/bin/env node
/* eslint no-console: 0 */
/* We load our modules */
const cmd = require('node-cmd');
const inquirer = require('inquirer');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
/* We get the project name */
const args = process.argv.slice(2);
const projectName = args[0];
const moduleDirectory = __dirname;
const projectDirectory = process.cwd();
/* All the info we need to know */
const questions = [
{
type: 'confirm',
name: 'infura_setup',
message: 'Do you want to set an Infura project ID?',
}, {
type: 'input',
name: 'infura_project_id',
message: 'Please enter your Infura project ID:',
when(answers) {
return answers.infura_setup;
},
}, {
type: 'confirm',
name: 'mnemonic_setup',
message: 'Do you want to set a mnemonic?',
}, {
type: 'input',
name: 'mnemonic',
message: 'Please enter your mnemonic:',
when(answers) {
return answers.mnemonic_setup;
},
}, {
type: 'list',
name: 'solidity_linter',
message: 'Do you want to create a default configuration file for a Solidity linter?',
choices: ['Solium', 'Solhint', 'No'],
},
];
/* Say hello! */
console.log(`\nWelcome to ${chalk.blue('create-truffle-dapp')}!\n`);
/* We ask the questions */
inquirer.prompt(questions).then((answers) => {
console.log(`\nCreating ${chalk.green(projectName)} in ${chalk.green(projectDirectory)}...\n`);
/* Creates the folder for the project */
cmd.get(`mkdir ${projectName}`, () => {
/* Creates the folder for the contracts */
cmd.get(`mkdir ${projectName}/contracts`, () => {
/* We start to create the base contract */
fs.readFile(path.join(moduleDirectory, 'templates/project/contracts/Base.sol'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
const buf = data.replace(/_project_placeholder_/g, projectName);
fs.writeFile(`${projectName}/contracts/${projectName}.sol`, buf, (error) => {
if (error) {
console.log(error);
}
});
});
/* Then the file for the migrations */
fs.readFile(path.join(moduleDirectory, '/templates/project/contracts/Migrations.sol'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
fs.writeFile(`${projectName}/contracts/Migrations.sol`, data, (error) => {
if (error) {
console.log(error);
}
});
});
});
/* If needed, we init a default configuration file for a Soldity linter */
if (answers.solidity_linter === 'Solhint') {
cmd.get(`
cd ${projectName}
solhint init-config
`);
} else if (answers.solidity_linter === 'Solium') {
cmd.get(`
cd ${projectName}
solium --init
`);
}
/* Creates the folder for the migrations files */
cmd.get(`mkdir ${projectName}/migrations`, () => {
/* We copy the step 1 for the migrations */
fs.readFile(path.join(moduleDirectory, '/templates/project/migrations/1_initial_migration.js'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
fs.writeFile(`${projectName}/migrations/1_initial_migration.js`, data, (error) => {
if (error) {
console.log(error);
}
});
});
/* Then we copy the step 2 for the migrations */
fs.readFile(path.join(moduleDirectory, '/templates/project/migrations/2_deploy_contracts.js'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
const buf = data.replace(/_project_placeholder_/g, projectName);
fs.writeFile(`${projectName}/migrations/2_deploy_contracts.js`, buf, (error) => {
if (error) {
console.log(error);
}
});
});
});
/* Creates the folder for our tests */
cmd.get(`mkdir ${projectName}/test`, () => {
/* We copy a simple test and change the placeholder */
fs.readFile(path.join(moduleDirectory, '/templates/project/test/Base.test.js'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
const buf = data.replace(/_project_placeholder_/g, projectName);
fs.writeFile(`${projectName}/test/${projectName}.test.js`, buf, (error) => {
if (error) {
console.log(error);
}
});
});
});
/* Creates the .env file */
fs.readFile(path.join(moduleDirectory, '/templates/project/.env'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
let buf = data;
if (answers.mnemonic_setup) {
buf = buf.replace(/_mnemonic_placeholder_/g, answers.mnemonic);
}
if (answers.infura_setup) {
buf = buf.replace(/_infura_project_id_placeholder_/g, answers.infura_project_id);
}
fs.writeFile(`${projectName}/.env`, buf, (error) => {
if (error) {
console.log(error);
}
});
});
/* Creates the .gitignore */
fs.readFile(path.join(moduleDirectory, '/templates/project/.gitignore'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
fs.writeFile(`${projectName}/.gitignore`, data, (error) => {
if (error) {
console.log(error);
}
});
});
/* Creates the Truffle configuration file */
fs.readFile(path.join(moduleDirectory, '/templates/project/truffle.js'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
fs.writeFile(`${projectName}/truffle.js`, data, (error) => {
if (error) {
console.log(error);
}
});
});
/* Creates the package.json file */
fs.readFile(path.join(moduleDirectory, '/templates/project/package.json'), 'utf8', (err, data) => {
if (err) {
console.log(err);
}
const buf = data.replace(/_project_placeholder_/g, projectName);
fs.writeFile(`${projectName}/package.json`, buf, (error) => {
if (error) {
console.log(error);
}
console.log('Installing packages. This might take a couple of minutes.');
console.log(`Installing ${chalk.blue('dotenv')}, ${chalk.blue('truffle-hdwallet-provider')} and ${chalk.blue('web3')}...\n`);
cmd.get(`
cd ${projectName}
npm install
`, (cmdError, res, stderr) => {
console.log(res);
console.log(`Success! You can go to the ${chalk.green(projectName)} project directory with ${chalk.magenta(`cd ${projectName}`)}!`);
console.log('Inside this directory, you can run commands like:\n');
console.log(`${chalk.magenta('npm install openzeppelin-solidity')}\nTo install a package...\n\n`);
console.log(`${chalk.magenta('truffle test')}\nTo test your dapp...\n\n`);
console.log(`${chalk.magenta('truffle compile')}\nTo compile your dapp...\n\n`);
console.log(`${chalk.magenta('truffle migrate')}\nTo migrate your dapp...\n\n`);
console.log('Happy coding! :)');
});
});
});
});
});