-
Notifications
You must be signed in to change notification settings - Fork 28
/
gas-estimate.js
executable file
·70 lines (57 loc) · 1.75 KB
/
gas-estimate.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
/**
* This script runs all tests with gas-reporter so you will see
* gas consumptions of all the tests along with gas consumptions
* of contract deployments.
* It works pretty dumb:
* 1. Before starting tests the script updates reporter in truffle.js config:
* reporter: 'spec' => reporter: 'eth-gas-reporter'
* 2. Start of the tests.
* 3. On test end/error/ctrl+c rollback reporter in truffle.js to standard 'spec'
*/
const fs = require('fs');
const { spawn } = require('child_process');
const filename = `${__dirname}/../truffle.js`;
const typeSpec = "reporter: 'spec'";
const typeEthGasReporter = "reporter: 'eth-gas-reporter'";
fs.readFile(filename, 'utf8', (err, data) => {
if (err) return console.log(err);
let text = data.replace(typeSpec, typeEthGasReporter);
fs.writeFile(filename, text, 'utf8', (err) => {
if (err) return console.log(err);
// run tests
const child = spawn('scripts/test.sh');
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
console.log(chunk);
});
child.stderr.on('data', (chunk) => {
console.log(chunk);
});
child.on('close', () => {
console.log(`child process closed`);
rollback();
});
child.on('exit', () => {
console.log(`child process exited`);
rollback();
});
child.on('error', () => {
console.log(`child process errored`);
rollback();
});
process.on('SIGINT', () => {
console.log(`child process iterrupted via CTRL+C`);
rollback();
});
});
});
function rollback() {
fs.readFile(filename, 'utf8', (err, data) => {
if (err) return console.log(err);
let text = data.replace(typeEthGasReporter, typeSpec);
fs.writeFile(filename, text, 'utf8', (err) => {
if (err) return console.log(err);
});
});
}