forked from muon-protocol/muon-node-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devnet-run.js
124 lines (117 loc) · 3.26 KB
/
devnet-run.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
const dotenv = require('dotenv')
dotenv.config()
const fs = require('fs')
const emoji = require('node-emoji')
const { spawn } = require('child_process')
const parseArgv = require('./src/utils/parseArgv')
function runMuonNode(node_n) {
try {
for (let i = 1; i <= node_n; i++) {
const result = spawn('./node_modules/.bin/env-cmd', [
'-f',
`./dev-chain/dev-node-${i}.env`,
'ts-node',
'./src/index.ts'
])
result.stdout.on('data', (data) => {
console.log(data.toString())
})
result.stderr.on('data', (data) => {
console.error(`stderr: ${data}`)
})
}
} catch (error) {
console.log('Error happend in run nodes:', error)
}
}
async function runNodes() {
let params = parseArgv()
let node_n = params['n'] ? Number(params['n']) : 2
let port = params['p'] ? Number(params['p']) : 8080
const dir = './dev-chain'
if (params['setup']) {
const configFiles = fs
.readdirSync('./config')
.filter((item) => item.startsWith('dev-node'))
if (configFiles.length > 0) {
configFiles.forEach((item) => {
// delete dev-node directory recursively
fs.rm(`./config/${item}`, { recursive: true, force: true }, (err) => {
if (err) {
throw err
}
})
})
}
// delete directory recursively
fs.rm(dir, { recursive: true, force: true }, (err) => {
if (err) {
throw err
}
fs.mkdirSync(dir, {
recursive: true
})
})
// console.log('Setting Up Envs ...')
const result = spawn('node', [
'devnet-generate-envs.js',
`-n=${node_n}`,
`-p=${port}`
])
result.stdout.on('data', (data) => {
console.log(data.toString())
})
result.on('exit', () => {
runMuonNode(node_n)
})
// await exec(`node devnet-generate-envs.js -n=${node_n} -p=${port}`)
} else {
if (fs.existsSync(`./dev-chain/dev-node-${node_n}.env`)) {
runMuonNode(node_n)
for (let index = 1; index <= node_n; index++) {
let data = fs.readFileSync(`./dev-chain/dev-node-${index}.env`, 'utf8')
let lines = data.split('\n')
let address = lines
.find((item) => item.trim().startsWith('SIGN_WALLET_ADDRESS'))
.split('=')
console.log(
emoji.get('o'),
`Node-${index} Ethereum Address: `,
`${address[1]}\n`
)
}
} else {
// console.log('Generating Envs...')
const configFiles = fs
.readdirSync('./config')
.filter((item) => item.startsWith('dev-node'))
if (configFiles.length > 0) {
configFiles.forEach((item) => {
// delete dev-node directory recursively
fs.rm(`./config/${item}`, { recursive: true, force: true }, (err) => {
if (err) {
throw err
}
})
})
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {
recursive: true
})
}
const result = spawn('node', [
'devnet-generate-envs.js',
`-n=${node_n}`,
`-p=${port}`
])
result.stdout.on('data', (data) => {
console.log(data.toString())
})
result.on('exit', () => {
runMuonNode(node_n)
})
}
}
}
runNodes()