|
1 | 1 | const fs = require("fs");
|
2 | 2 | const path = require("path");
|
3 |
| -var size = new Array(); |
| 3 | +const exec = require('child_process').execSync; |
| 4 | +const chalk = require('chalk'); |
| 5 | +const { table } = require("table"); |
4 | 6 |
|
5 |
| -function readFiles() { |
| 7 | +async function readFiles() { |
6 | 8 | if (fs.existsSync("./build/contracts/")) {
|
7 |
| - let files = fs.readdirSync("./build/contracts/"); |
8 |
| - return files; |
| 9 | + return fs.readdirSync("./build/contracts/"); |
9 | 10 | } else {
|
10 |
| - console.log("Directory doesn't exists"); |
| 11 | + console.log('Compiling contracts. This may take a while, please wait.'); |
| 12 | + exec('truffle compile'); |
| 13 | + return fs.readdirSync("./build/contracts/"); |
11 | 14 | }
|
12 | 15 | }
|
13 | 16 |
|
14 | 17 | async function printSize() {
|
15 |
| - let files = readFiles(); |
| 18 | + let files = await readFiles(); |
| 19 | + console.log(`NOTE- Maximum size of contracts allowed to deloyed on the Ethereum mainnet is 24 KB(EIP170)`); |
| 20 | + console.log(`---- Size of the contracts ----`); |
| 21 | + let dataTable = [['Contracts', 'Size in KB']]; |
16 | 22 | files.forEach(item => {
|
17 | 23 | let content = JSON.parse(fs.readFileSync(`./build/contracts/${item}`).toString()).deployedBytecode;
|
18 | 24 | let sizeInKB = content.toString().length / 2 / 1024;
|
19 |
| - size.push(sizeInKB); |
| 25 | + if (sizeInKB > 24) |
| 26 | + dataTable.push([chalk.red(path.basename(item, ".json")),chalk.red(sizeInKB)]); |
| 27 | + else if (sizeInKB > 20) |
| 28 | + dataTable.push([chalk.yellow(path.basename(item, ".json")),chalk.yellow(sizeInKB)]); |
| 29 | + else |
| 30 | + dataTable.push([chalk.green(path.basename(item, ".json")),chalk.green(sizeInKB)]); |
20 | 31 | });
|
21 |
| - console.log(`NOTE- Maximum size of contracts allowed to deloyed on the Ethereum mainnet is 24 KB(EIP170)`); |
22 |
| - console.log(`---- Size of the contracts ----`); |
23 |
| - for (let i = 0; i < files.length; i++) { |
24 |
| - console.log(`${path.basename(files[i], ".json")} - ${size[i]} KB`); |
25 |
| - } |
| 32 | + console.log(table(dataTable)); |
26 | 33 | }
|
27 | 34 |
|
28 | 35 | printSize();
|
0 commit comments