UNPKG

@chainspin/compiler

Version:

Solidity contract compiler for ChainSpin framework.

94 lines 3.85 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const path = require("path"); const utils = require("@chainspin/utils"); const solc = require("solc"); class Compiler { constructor(recipe) { this.recipe = Object.assign({ filter: (f) => f.substr(-4) === '.sol' }, recipe); this.data = []; } compile() { return __awaiter(this, void 0, void 0, function* () { this.data = []; yield utils.ensureDirectoryDeeply(this.recipe.dist); yield this.buildContracts(); }); } save() { return __awaiter(this, void 0, void 0, function* () { yield Promise.all(this.data.map((d) => { return utils.writeFile(path.join(this.recipe.dist, `${d.name}.json`), JSON.stringify(d, null, 2)); })); }); } buildContracts() { return __awaiter(this, void 0, void 0, function* () { const sources = yield this.getSolcSources(); const output = solc.compile({ sources }, 1); if (output.errors) { } for (const name in output.contracts) { const contract = output.contracts[name]; this.data.push({ abi: JSON.parse(contract.interface), bin: contract.bytecode, name: name.split(':')[1], }); } }); } getSolcSources() { return __awaiter(this, void 0, void 0, function* () { const dirs = [ ...yield this.getModuleDirectories('node_modules'), this.recipe.src, ]; const files = yield Promise.all(dirs.map((p) => utils.getFilesDeeply(p))).then((f) => { return f.reduce((a, b) => a.concat(b), []); }).then((f) => { return f.filter(this.recipe.filter); }); const sources = {}; for (const file of files) { const key = this.normalizeSolcPath(file); sources[key] = yield utils.readFile(file).then((s) => s.toString()); } return sources; }); } normalizeSolcPath(dest) { return dest.indexOf('node_modules') === 0 ? dest.substr(13) : `./${dest}`; } getModuleDirectories(dest) { return __awaiter(this, void 0, void 0, function* () { const paths = []; const dirs = yield utils.getDirectories(dest); for (const dir of dirs) { if (dir[13] === '@' && dir.split(/\\|\//).length === 2) { paths.push(...yield this.getModuleDirectories(dir)); } else { yield Promise.all([path.join(dir, 'contracts'), path.join(dir, 'src', 'contracts'), ].map((d) => { return utils.isDirectory(d).then((b) => b ? d : null); })).then((results) => { results.forEach((r) => r ? paths.push(r) : null); }); } } return paths; }); } } exports.Compiler = Compiler; //# sourceMappingURL=compiler.js.map