diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-05 11:05:26 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-03-12 10:37:27 +0800 |
commit | 13299158d1e22d1af1cd36434fc403a74743ecb1 (patch) | |
tree | 9b35435c6f8641d2dc3d7bfd530c7c4f040a1f51 /packages/sol-cov/src/collect_contract_data.ts | |
parent | a6571b09d2087ffb9a4860c0db3d7344321fe2c3 (diff) | |
download | dexon-0x-contracts-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.gz dexon-0x-contracts-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.zst dexon-0x-contracts-13299158d1e22d1af1cd36434fc403a74743ecb1.zip |
Add sol-cover implementation
Diffstat (limited to 'packages/sol-cov/src/collect_contract_data.ts')
-rw-r--r-- | packages/sol-cov/src/collect_contract_data.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/packages/sol-cov/src/collect_contract_data.ts b/packages/sol-cov/src/collect_contract_data.ts new file mode 100644 index 000000000..dffccce2f --- /dev/null +++ b/packages/sol-cov/src/collect_contract_data.ts @@ -0,0 +1,40 @@ +import * as fs from 'fs'; +import * as glob from 'glob'; +import * as _ from 'lodash'; +import * as path from 'path'; + +import { ContractData } from './types'; + +export const collectContractsData = (artifactsPath: string, sourcesPath: string, networkId: number) => { + const sourcesGlob = `${sourcesPath}/**/*.sol`; + const sourceFileNames = glob.sync(sourcesGlob, { absolute: true }); + const contractsDataIfExists: Array<ContractData | {}> = _.map(sourceFileNames, sourceFileName => { + const baseName = path.basename(sourceFileName, '.sol'); + const artifactFileName = path.join(artifactsPath, `${baseName}.json`); + if (!fs.existsSync(artifactFileName)) { + // If the contract isn't directly compiled, but is imported as the part of the other contract - we don't have an artifact for it and therefore can't do anything usefull with it + return {}; + } + const artifact = JSON.parse(fs.readFileSync(artifactFileName).toString()); + const sources = _.map(artifact.networks[networkId].sources, source => { + const includedFileName = glob.sync(`${sourcesPath}/**/${source}`, { absolute: true })[0]; + return includedFileName; + }); + const sourceCodes = _.map(sources, source => { + const includedSourceCode = fs.readFileSync(source).toString(); + return includedSourceCode; + }); + const contractData = { + baseName, + sourceCodes, + sources, + sourceMap: artifact.networks[networkId].source_map, + sourceMapRuntime: artifact.networks[networkId].source_map_runtime, + runtimeBytecode: artifact.networks[networkId].runtime_bytecode, + bytecode: artifact.networks[networkId].bytecode, + }; + return contractData; + }); + const contractsData = _.filter(contractsDataIfExists, contractData => !_.isEmpty(contractData)) as ContractData[]; + return contractsData; +}; |