-
Notifications
You must be signed in to change notification settings - Fork 0
/
contractSizeReport.js
68 lines (58 loc) · 1.73 KB
/
contractSizeReport.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
'use strict';
const fs = require('fs');
const util = require('util');
const got = require('got');
const yargs = require('yargs');
let path = 'artifacts/contracts';
const readdir = util.promisify(fs.readdir);
async function getFiles(dir, files_, names_){
var files = await readdir(dir);
for (var i in files){
var fullDir = dir + '/' + files[i];
if (fs.statSync(fullDir).isDirectory()){
await getFiles(fullDir, files_, names_);
} else {
files_.push(fullDir);
names_.push(files[i]);
}
}
return (files_, names_);
}
async function generateCodeSizeReport() {
let result = {};
let fileDirs = [];
let fileNames = [];
await getFiles(path, fileDirs, fileNames);
for (let i = 0; i < fileDirs.length; i++) {
let fileDir = fileDirs[i];
let rawData = fs.readFileSync(fileDir);
let contractData = JSON.parse(rawData);
if (contractData.deployedBytecode != undefined) {
let codeSize = contractData.deployedBytecode.length / 2 - 1;
if (codeSize > 0) {
result[fileNames[i]] = codeSize;
}
}
}
return result;
}
async function writeReport(report) {
let jsonContent = JSON.stringify(report, null, '\t');
let reportDir = `report/`;
if (!fs.existsSync(reportDir)) {
fs.mkdirSync(reportDir, {recursive: true});
}
let reportFile = `${reportDir}contractSize.json`;
fs.writeFile(reportFile, jsonContent, 'utf8', function (err) {
if (err) {
console.log('An error occured while writing JSON Object to File.');
return console.log(err);
}
});
console.table(report);
}
async function printContractSize() {
let contractSizeReport = await generateCodeSizeReport();
await writeReport(contractSizeReport);
}
printContractSize();