-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.js
More file actions
61 lines (52 loc) · 1.31 KB
/
interface.js
File metadata and controls
61 lines (52 loc) · 1.31 KB
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
const { spawn } = require('child_process');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const WALLET_MNEMONIC = process.env.WALLET_MNEMONIC || '../libra-cli-rest/wallet.mnemonic';
let cli = null;
let lines = '';
setInterval(() => {
cli.stdin.write(`a w ${WALLET_MNEMONIC}\n`);
}, 1000 * 60);
async function init() {
const LIBRA_DIR = process.env.LIBRA_DIR || '../libra';
cli = spawn(process.env.START_SCRIPT || LIBRA_DIR + '/scripts/cli/start_cli_testnet.sh', [], {
cwd: LIBRA_DIR,
stdio: ['pipe', 'pipe', process.stderr]
});
cli.stdout.on('data', data => {
const line = data.toString();
if (line.includes('mnemonic')) {
return;
}
console.log(line);
lines = lines + line;
});
cli.stdin.write(`a r ${WALLET_MNEMONIC}\n`);
}
init();
const runCmd = async (cmd, maxDelay, stopWord) => {
lines = '';
cli.stdin.write(cmd + '\n');
if (stopWord) {
let remainingDelay = maxDelay;
while (remainingDelay > 0) {
await sleep(1);
if (lines.includes(stopWord)) {
break;
}
remainingDelay--;
}
} else {
await sleep(maxDelay);
}
return lines;
};
const lastWord = str => {
const splitted = str.trimEnd().split(' ');
return splitted[splitted.length - 1];
};
module.exports = {
runCmd,
lastWord
};