UNPKG

solidity-docgen

Version:

Solidity API documentation automatic generator.

101 lines 4.53 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const globby_1 = __importDefault(require("globby")); const semver_1 = __importDefault(require("semver")); const outputSelection = { '*': { '': [ 'ast', ], }, }; function compile(directory, ignore = [], solcModule = 'solc', solcSettings = { optimizer: { enabled: true, runs: 200 } }) { return __awaiter(this, void 0, void 0, function* () { const solc = yield SolcAdapter.require(solcModule); const files = yield globby_1.default(path_1.default.join(directory, '**/*.sol'), { ignore: ignore.map(i => path_1.default.join(i, '**/*')), }); const sources = lodash_1.fromPairs(yield Promise.all(files.map((file) => __awaiter(this, void 0, void 0, function* () { return [ file, { content: yield fs_extra_1.default.readFile(file, 'utf8') }, ]; })))); const solcInput = { language: "Solidity", sources: sources, settings: Object.assign({}, solcSettings, { outputSelection }), }; const solcOutput = solc.compile(solcInput); const { errors: allErrors } = solcOutput; if (allErrors && allErrors.some(e => e.severity === 'error')) { const errors = allErrors.filter(e => e.severity === 'error'); const firstError = errors[0].formattedMessage; const moreErrors = errors.length === 1 ? '' : ` (And ${errors.length - 1} other errors...)`; throw new Error(`Solidity was unable to compile. ${firstError}${moreErrors}`); } return solcOutput; }); } exports.compile = compile; class SolcAdapter { constructor(solc) { this.solc = solc; } static require(solcModule) { return __awaiter(this, void 0, void 0, function* () { const solc = yield Promise.resolve().then(() => __importStar(require(solcModule))); return new SolcAdapter(solc); }); } compile(input) { const inputJSON = JSON.stringify(input); const solcOutputString = this.solc.compileStandardWrapper(inputJSON); const solcOutput = JSON.parse(solcOutputString); if (semver_1.default.satisfies(this.solc.version(), '^0.4')) { for (const source of Object.values(solcOutput.sources)) { for (const fileNode of source.ast.nodes) { if (fileNode.nodeType === 'ContractDefinition') { for (const contractNode of fileNode.nodes) { if (contractNode.nodeType === 'FunctionDefinition') { if (contractNode.isConstructor) { contractNode.kind = 'constructor'; } else if (contractNode.name === '') { contractNode.kind = 'fallback'; } else { contractNode.kind = 'function'; } } } } } } ; } return solcOutput; } } //# sourceMappingURL=compile.js.map