-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (92 loc) · 3.7 KB
/
index.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
#!/usr/bin/env node
let shell = require('shelljs')
let colors = require('colors')
let fs = require('fs')
let appName = process.argv[2]
let appDirectory = `${process.cwd()}/${appName}`
let templates = require('./templates/templates.js')
const createDirectory = () => {
if (appName) {
if (!fs.existsSync(appName)) {
fs.mkdirSync(appName);
}
}
else {
console.log("\nNo app name was provided.".red)
console.log("\nProvide an app name in the following format: ")
console.log("\ncreate-smart-contract ", "app-name\n".cyan)
process.exit()
}
}
const cdIntoNewApp = () => {
return new Promise(resolve => {
shell.cd(appDirectory)
// process.chdir(appDirectory)
resolve()
})
}
const createSmartContract = () => {
return new Promise(resolve => {
if (appName) {
shell.exec(`truffle init ${appName}`, () => {
console.log("Init truffle")
shell.exec('npm init --yes')
resolve(true)
})
} else {
console.log("\nNo app name was provided.".red)
console.log("\nProvide an app name in the following format: ")
console.log("\ncreate-react-redux-router-app ", "app-name\n".cyan)
resolve(false)
}
})
}
const installPackages = () => {
return new Promise(resolve => {
console.log("\nInstalling hardhat, @openzeppelin/contracts, @truffle/hdwallet-provider, chai, solidity-docgen, truffle, @nomiclabs/hardhat-ethers, @nomiclabs/hardhat-truffle5, @nomiclabs/hardhat-waffle, @nomiclabs/hardhat-web3, eth-gas-reporter, ethereum-waffle, ethers, mocha, prettier, prettier-plugin-solidity, solc-0.7, solidity-coverage, truffle-assertions and web3\n".cyan)
shell.exec(`npm install --save-dev hardhat @openzeppelin/contracts @truffle/hdwallet-provider chai solidity-docgen truffle @nomiclabs/hardhat-ethers @nomiclabs/hardhat-truffle5 @nomiclabs/hardhat-waffle @nomiclabs/hardhat-web3 eth-gas-reporter ethereum-waffle ethers mocha prettier prettier-plugin-solidity solidity-coverage truffle-assertions web3 solc-0.7@npm:solc@^0.7.0`, () => {
console.log("\nFinished installing packages\n".green)
resolve()
})
})
}
const updateTemplates = () => {
return new Promise(resolve => {
let promises = []
Object.keys(templates).forEach((fileName, i) => {
switch (fileName) {
case 'package':
promises[i] = new Promise(res => {
let package = JSON.parse(templates[fileName])
package['name'] = appName
let data = JSON.stringify(package);
fs.writeFile(`${appDirectory}/${fileName}`, data, function (err) {
if (err) { return console.log(err) }
res()
})
})
break
default:
promises[i] = new Promise(res => {
fs.writeFile(`${appDirectory}/${fileName}`, templates[fileName], function (err) {
if (err) { return console.log(err) }
res()
})
})
}
})
Promise.all(promises).then(() => { resolve() })
})
}
const run = async () => {
let success = await createSmartContract()
if (!success) {
console.log('Something went wrong while trying to create a new React app using create-react-app'.red)
return false;
}
await cdIntoNewApp()
await updateTemplates()
await installPackages()
console.log("All done")
}
run()