diff --git a/README.md b/README.md index 3fb7a8d..3d115d9 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Options: Commands: generate|gen [options] Generate deployment and abi files from Truffle generated JSON files removeSourceEntries|removeSrc [options] remove source entries from all deployment files + check [options] Basic integrity check of files generated. Checks references between Abi files and deployment files More info: https://github.com/Augmint/abiniser @@ -125,6 +126,20 @@ Each file contains the contract's Abi in [Solidity ABI JSON format](https://soli ## Maintenance features +### Checks on generated files + +``` +$ abiniser check -h +Usage: check [options] + +Basic integrity check of files generated. Checks references between Abi files and deployment files + +Options: + -a, --abi-output-dir [value] Sets abi output directory to work on. (default: "./abiniser/abis") + -d, --deployments-output-dir [value] Sets deployments output directory to work on. (default: "./abiniser/deployments") + -h, --help output usage information +``` + ### Remove all source entries from deploy files already generated by abiniser ``` diff --git a/abiniser.js b/abiniser.js index 0a86510..876f967 100644 --- a/abiniser.js +++ b/abiniser.js @@ -5,6 +5,7 @@ const pjson = require('./package.json'); const program = require('commander'); const generate = require('./lib/generate.js'); const removeSourceEntries = require('./lib/removeSourceEntries.js'); +const checkIntegrity = require('./lib/checkIntegrity.js'); function help() { console.log( @@ -46,6 +47,17 @@ program ) .action(options => removeSourceEntries(options)); +program + .command('check') + .description('Basic integrity check of files generated. Checks references between Abi files and deployment files') + .option('-a, --abi-output-dir [value]', 'Sets abi output directory to work on.', './abiniser/abis') + .option( + '-d, --deployments-output-dir [value]', + 'Sets deployments output directory to work on.', + './abiniser/deployments' + ) + .action(options => checkIntegrity(options)); + program.on('--help', () => help()); program.on('command:*', () => { diff --git a/lib/checkIntegrity.js b/lib/checkIntegrity.js new file mode 100644 index 0000000..58c0faa --- /dev/null +++ b/lib/checkIntegrity.js @@ -0,0 +1,68 @@ +'use strict'; + +const filesLib = require('./files'); + +async function checkIntegrity(options) { + try { + let config; + if (filesLib.fileExists(options.configFile)) { + config = filesLib.readJsonFile(options.configFile); + } + + const abiOutputDir = options.abiOutputDir || config.abiOutputDir; + const deploymentsOutputDir = options.deploymentsOutputDir || config.deploymentsOutputDir; + + if (!filesLib.directoryExists(abiOutputDir)) { + console.error(`Can't find abi output directory ${abiOutputDir}`); + process.exit(1); + } + + if (!filesLib.directoryExists(deploymentsOutputDir)) { + console.error(`Can't find deployments output directory ${deploymentsOutputDir}`); + process.exit(1); + } + + const abiFiles = await filesLib.fileWalker(abiOutputDir); + + const abis = []; + abiFiles.forEach(abiFile => { + const abiFileContent = filesLib.readJsonFile(abiFile); + abis.push({ + abiHash: abiFileContent.abiHash, + contractName: abiFileContent.contractName, + file: abiFile, + referenceCount: 0 + }); + }); + + const deploymentFiles = await filesLib.fileWalker(deploymentsOutputDir); + + const deploymentsWithoutAbiFile = []; + deploymentFiles.forEach(file => { + const content = filesLib.readJsonFile(file); + + for (const abiHash in content.deployedAbis) { + const abi = abis.find(abi => abi.abiHash === abiHash); + if (abi) { + abi.referenceCount++; + } else { + deploymentsWithoutAbiFile.push({ abiHash, fileName: file }); + } + } + }); + + console.log( + '***** ABI files with references from deployment files:\n', + abis.filter(abi => abi.referenceCount !== 0) + ); + console.log( + '***** ABI files WITHOUT reference from deployment files:\n', + abis.filter(abi => abi.referenceCount === 0) + ); + console.log('***** Deployments without ABI file:\n', deploymentsWithoutAbiFile); + } catch (error) { + console.error('Error:\n', error); + } +} + +module.exports = checkIntegrity;