aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src/collect_contract_data.ts
blob: a0ce2640fc81f7f28230fcd8f620842ad5c080ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 useful 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;
        });
        if (_.isUndefined(artifact.networks[networkId])) {
            throw new Error(`No ${baseName} artifacts found for networkId ${networkId}`);
        }
        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;
};