aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-doc/src/solidity_doc_generator.ts
blob: c57a4779c3459d2d7bbd79e5427b81abe7586a23 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as _ from 'lodash';

import { MethodAbi } from 'ethereum-types';

import { Compiler, CompilerOptions } from '@0xproject/sol-compiler';
import { logUtils } from '@0xproject/utils';

import { SolidityDocFormat } from './solidity_doc_format';

export class SolidityDocGenerator {
    private readonly _compilerOptions: CompilerOptions;
    constructor(contractsDir: string, artifactsDir: string) {
        // instantiate sol-compiler, passing in options to say we want abi and devdoc
        this._compilerOptions = {
            contractsDir,
            artifactsDir,
            contracts: '*',
            compilerSettings: {
                outputSelection: {
                    ['*']: {
                        ['*']: ['abi', 'devdoc'],
                    },
                },
            },
        };
    }
    /// run `contractsToCompile` through compiler, gathering output
    public async generateAsync(contractsToCompile: string[]): Promise<SolidityDocFormat> {
        if (!_.isUndefined(contractsToCompile)) {
            this._compilerOptions.contracts = contractsToCompile;
        }

        const compiler = new Compiler(this._compilerOptions);

        const doc = new SolidityDocFormat();

        const compilerOutputs = await compiler.getCompilerOutputsAsync();
        for (const compilerOutput of compilerOutputs) {
            const solidityModules = _.keys(compilerOutput.contracts);
            for (const solidityModule of solidityModules) {
                const compiledSolidityModule = compilerOutput.contracts[solidityModule];

                const contracts = _.keys(compiledSolidityModule);
                for (const contract of contracts) {
                    const compiledContract = compiledSolidityModule[contract];

                    // TODO: modify typescript-typings/types/solc/index.d.ts... it doesn't currently support devdoc!
                    // tslint:disable-next-line:no-unnecessary-type-assertion tsc says abi[0] has no property `name` and won't compile without the `as`, but tslint says the `as` is unnecssary.
                    logUtils.log(
                        `TODO: extract data from ${contract}'s abi (eg ${
                            (compiledContract.abi[0] as MethodAbi).name
                        }, etc) and devdoc outputs, and insert it into \`doc\``,
                    );
                }
            }
        }

        return doc;
    }
}