๐ npm install solidity-code-metrics
The number-crunching enginge behind ๐tintinweb.solidity-metrics.
To use the CLI from the compiled sources you can do:
node ./src/cli.js <path to solidity file(s)>It is however more useful to install solidity-metrics globally and call it from any directory:
npm install -g solidity-code-metrics
solidity-code-metrics myfile1.sol myfile2.solBy default, the cli outputs to the console, you can however store the output in a file rather easily (both markdown and html are supported):
solidity-code-metrics myfile.sol > metrics.md
solidity-code-metrics myfile.sol --html > metrics.htmlRequires Node.js 20 or >=22.
const {SolidityMetricsContainer} = require('solidity-code-metrics');
let options = {
basePath:"",
inputFileGlobExclusions:undefined,
inputFileGlob: undefined,
inputFileGlobLimit: undefined,
debug:false,
disableDoppelGanger: false, // true skips matching contracts against known libraries (faster)
repoInfo: {
branch: undefined,
commit: undefined,
remote: undefined
}
}
let metrics = new SolidityMetricsContainer("metricsContainerName", options);
// analyze files
metrics.analyze(path_to_solidity_file);
// ...
metrics.analyze(path_to_solidity_file_N);
// analyze a source string instead of reading the file (e.g. an unsaved editor buffer).
// totals() and details() use the string; generateReportMarkdown() and getDotGraphs()
// still read the path from disk for their surya sections.
metrics.analyze("contracts/Token.sol", { content: solidity_source_string });
// output
console.log(metrics.totals());
// per-file metrics: [{ name, metrics: { sloc, nsloc, complexity, summary, num, capabilities, other } }]
// other.doppelganger holds pending Promises unless disableDoppelGanger is set
console.log(metrics.details());
metrics.generateReportMarkdown().then(text => console.log(text));
// or let text = await metrics.generateReportMarkdown();
// paths relative to basePath; both accept `\` and `/` separators
metrics.relativePath("/project/contracts/Token.sol"); // "contracts/Token.sol" (basePath "/project/")
metrics.toRelativeGlob("C:\\project\\contracts"); // "contracts" (basePath "C:\\project/"), always `/`-separatednpm ci
npm test # vitest: library tests and CLI end-to-end tests
