-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·95 lines (79 loc) · 2.47 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
#!/usr/bin/env node
'use strict';
const chalk = require('chalk');
const config = require('./lib/config');
const IOTA = require('iota.lib.js');
const prompt = require('./lib/prompt');
const setupCommands = require('./lib/commands/index');
const vorpal = require('vorpal')();
const setDelimiter = prompt.setDelimiter;
const setupPrompt = prompt.setupPrompt;
const data = {
accountData: undefined,
currentNodeInfo: undefined,
depth: 9,
maxNeighbors: 9,
milestoneLag: 15,
minNeighbors: 4,
minWeightMagnitude: 3,
seed: ''
};
const iotajs = new IOTA({
host: 'http://iotanode.party',
port: 14265
});
let refreshAccountDataTimer;
const refreshAccountData = () => {
if (refreshAccountDataTimer) {
clearTimeout(refreshAccountDataTimer);
}
if (data.seed) {
iotajs.api.getAccountData(data.seed, (err, accountData) => {
if (err) {
// on fail, retry fast
refreshAccountDataTimer = setTimeout(refreshAccountData, 10 * 1000);
return;
}
if (!data.accountData) {
vorpal.log(chalk.green('Account data retrieved.'));
}
data.accountData = accountData;
setDelimiter();
// on success, refresh slowly.
refreshAccountDataTimer = setTimeout(refreshAccountData, 2 * 60 * 1000);
});
}
};
const refreshServerInfo = () => {
iotajs.api.getNodeInfo((err, nodeInfo) => {
if (err) {
data.currentNodeInfo = undefined;
} else {
data.currentNodeInfo = nodeInfo;
// Also, see if we should store this node info in the config file
config.get('nodes', []).then(nodes => {
const node = `${iotajs.host}:${iotajs.port}`.replace('http://', '');
if (nodes.indexOf(node) === -1) {
nodes.push(node);
nodes = nodes.sort();
config.set('nodes', nodes);
}
});
}
setDelimiter();
});
};
setupPrompt(data, iotajs, vorpal);
setupCommands(data, iotajs, refreshAccountData, refreshServerInfo, vorpal);
// Give the local connection a little time to connect, then get new data periodically.
// TODO make this more deterministic. timeouts = ugly
setTimeout(refreshServerInfo, 100);
setInterval(refreshServerInfo, 15 * 1000);
const version = require('./package.json').version;
vorpal.log(chalk.green(`Running IOTA CLI v${version}\n`));
// Give the iotajs connection time to settle before processing command line params
// TODO make this more deterministic. timeouts = ugly
setTimeout(() => {
vorpal.parse(process.argv);
}, 100);
vorpal.show();