hardhat-contract-sizer
Advanced tools
Comparing version
{ | ||
"name": "hardhat-contract-sizer", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Output Solidity contract sizes with Hardhat", |
@@ -0,1 +1,3 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const chalk = require('chalk'); | ||
@@ -10,2 +12,6 @@ const Table = require('cli-table3'); | ||
const formatSize = function (size) { | ||
return (size / 1000).toFixed(3); | ||
}; | ||
task( | ||
@@ -26,2 +32,17 @@ 'size-contracts', 'Output the size of compiled contracts' | ||
const outputPath = path.resolve( | ||
hre.config.paths.cache, | ||
'.hardhat_contract_sizer_output.json' | ||
); | ||
const previousSizes = {}; | ||
if (fs.existsSync(outputPath)) { | ||
const previousOutput = await fs.promises.readFile(outputPath); | ||
JSON.parse(previousOutput).forEach(function (el) { | ||
previousSizes[el.fullName] = el.size; | ||
}); | ||
} | ||
await Promise.all(fullNames.map(async function (fullName) { | ||
@@ -37,11 +58,12 @@ if (config.only.length && !config.only.some(m => fullName.match(m))) return; | ||
if (!config.disambiguatePaths) { | ||
fullName = fullName.split(':').pop(); | ||
} | ||
outputData.push({ name: fullName, size }); | ||
outputData.push({ | ||
fullName, | ||
displayName: config.disambiguatePaths ? fullName : fullName.split(':').pop(), | ||
size, | ||
previousSize: previousSizes[fullName] || null, | ||
}); | ||
})); | ||
if (config.alphaSort) { | ||
outputData.sort((a, b) => a.name.toUpperCase() > b.name.toUpperCase() ? 1 : -1); | ||
outputData.sort((a, b) => a.displayName.toUpperCase() > b.displayName.toUpperCase() ? 1 : -1); | ||
} else { | ||
@@ -51,4 +73,6 @@ outputData.sort((a, b) => a.size - b.size); | ||
await fs.promises.writeFile(outputPath, JSON.stringify(outputData), { flag: 'w' }); | ||
const table = new Table({ | ||
head: [chalk.bold('Contract Name'), chalk.bold('Size (KB)')], | ||
head: [chalk.bold('Contract Name'), chalk.bold('Size (KB)'), chalk.bold('Change (KB)')], | ||
style: { head: [], border: [], 'padding-left': 2, 'padding-right': 2 }, | ||
@@ -80,3 +104,3 @@ chars: { | ||
let size = (item.size / 1000).toFixed(3); | ||
let size = formatSize(item.size); | ||
@@ -90,5 +114,18 @@ if (item.size > SIZE_LIMIT) { | ||
let diff; | ||
if (item.size < item.previousSize) { | ||
diff = chalk.green(formatSize(item.previousSize - item.size)); | ||
} else if (item.size > item.previousSize) { | ||
diff = chalk.red(formatSize(item.size - item.previousSize)); | ||
} else { | ||
diff = ''; | ||
} | ||
table.push([ | ||
{ content: item.name }, | ||
{ content: item.displayName }, | ||
{ content: size, hAlign: 'right' }, | ||
{ content: diff, hAlign: 'right' }, | ||
]); | ||
@@ -95,0 +132,0 @@ } |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
9281
12.01%224
14.29%2
100%