forked from hashgraph/hedera-local-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·80 lines (73 loc) · 2.43 KB
/
cli.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
#!/usr/bin/env node
const [, , ...commands] = process.argv;
const shell = require('shelljs');
const CliHelper = require('./src/cliHelper');
const HederaUtils = require('./src/hederaUtils');
(async function () {
if (!commands.length) {
console.log(`
Local Hedera Plugin - Runs consensus and mirror nodes on localhost:
- consensus node url - 127.0.0.1:50211
- node id - 0.0.3
- mirror node url - http://127.0.0.1:5551
Available commands:
start - Starts the local hedera network.
stop - Stops the local hedera network and delete all the existing data.
restart - Restart the local hedera network.
generate-accounts <n> - Generates N accounts, default 10.
`);
process.exit();
}
switch (commands[0]) {
case 'start': {
await start(commands);
break;
}
case 'stop': {
await stop(commands);
break;
}
case 'restart': {
await stop(commands);
await start(commands);
break;
}
case 'generate-accounts': {
await HederaUtils.generateAccounts(commands[1] || 10);
break;
}
default: {
console.log(`Undefined command. Check available commands at "npx hedera-local"`);
}
}
async function start(commands) {
console.log('Starting the docker containers...');
shell.cd(__dirname);
const output = shell.exec('docker-compose up -d 2>/dev/null');
if (output.code == 1) {
const yaml = require('js-yaml');
const fs = require('fs');
const containersNames = Object.values(yaml.load(fs.readFileSync('docker-compose.yml')).services)
.map(e => e.container_name)
.join(' ');
shell.exec(`docker stop ${containersNames} 2>/dev/null 1>&2`);
shell.exec(`docker rm -f -v ${containersNames} 2>/dev/null 1>&2`);
await stop();
shell.exec('docker-compose up -d 2>/dev/null');
}
await CliHelper.waitForFiringUp(5600);
console.log('Starting the network...');
console.log('Generating accounts...');
await HederaUtils.generateAccounts(CliHelper.getArgValue(commands, 'accounts', 10), true);
}
async function stop() {
console.log('Stopping the network...');
shell.cd(__dirname);
console.log('Stopping the docker containers...');
shell.exec('docker-compose down -v 2>/dev/null');
console.log('Cleaning the volumes and temp files...');
shell.exec('rm -rf network-logs/* >/dev/null 2>&1');
shell.exec('docker network prune -f 2>/dev/null');
}
process.exit();
})();