UNPKG

@titan-suite/cli

Version:

The complete smart contract development tool

133 lines (132 loc) 4.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const command_1 = require("@oclif/command"); const fs = require("fs"); const mkdirp = require("mkdirp"); const path = require("path"); const index_1 = require("../utils/index"); class Compile extends command_1.Command { constructor() { super(...arguments); this.getSimpleOutput = (compiledContract, name) => { let output; if (name.startsWith(':')) { output = { abi: JSON.parse(compiledContract[`${name}`].interface), bytecode: '0x' + compiledContract[`${name}`].bytecode }; } else { output = { abi: compiledContract[`${name}`].info.abiDefinition, bytecode: compiledContract[`${name}`].code }; } const updated = new Date().toString(); const data = Object.assign({ name }, output, { updated }); this.buildBolt(data); return output; }; this.buildBolt = (data) => { const boltsPath = path.join(process.cwd(), 'build', 'bolts', `${data.name}.json`); let newBolt; const exists = fs.existsSync(boltsPath); if (exists) { const old = index_1.readUtf8(boltsPath); newBolt = JSON.parse(old); newBolt.abi = data.abi; newBolt.bytecode = data.bytecode; } else { newBolt = data; } mkdirp('build/bolts', err => { if (err) { throw err; } else { fs.writeFile(boltsPath, JSON.stringify(newBolt, null, 4), err => { if (err) throw err; }); } }); }; this.stringifyOutput = (o) => { return JSON.stringify(o, null, 2); }; } run() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { args, flags } = this.parse(Compile); let compiled; try { const sol = index_1.readContract(args.file); const compileLocally = flags.locally || false; compileLocally && this.warn('The local compiler is not yet stable'); compiled = yield index_1.compile(sol, compileLocally); } catch (e) { this.error(e); } const contractName = flags.name ? flags.locally ? `:${flags.name}` : flags.name : undefined; try { if (flags.name) compiled[`${contractName}`].info; } catch (_a) { this.error('The specified contract name does not exist'); } if (contractName && flags.detailed) { this.log(this.stringifyOutput(compiled[`${contractName}`])); } else if (contractName && !flags.detailed) { const output = this.getSimpleOutput(compiled, contractName); this.log(this.stringifyOutput(output)); } else if (!contractName && flags.detailed) { Object.keys(compiled).map((i, value) => { this.log('\n\n<<{' + value + '} ' + i + '>>\n'); const name = Object.keys(compiled)[value]; this.log(this.stringifyOutput(compiled[`${name}`])); }); } else { Object.keys(compiled).map((value, i) => { this.log('\n\n<<{' + i + '} ' + value + '>>\n'); const output = this.getSimpleOutput(compiled, value); this.log(this.stringifyOutput(output)); }); } }); } } Compile.description = 'Compile a Solidity smart contract'; Compile.examples = [ '$ titan compile <path/to/Example.sol>', '$ titan compile -n SpecificContract <path/to/ManyContracts.sol>', '$ titan compile -d <path/to/Example.sol>', '$ titan compile -l <path/to/Example.sol>' ]; Compile.flags = { help: command_1.flags.help({ char: 'h' }), name: command_1.flags.string({ char: 'n', description: 'specify which smart contract to compile within the file' }), detailed: command_1.flags.boolean({ char: 'd', description: 'display more details about the contract' }), locally: command_1.flags.boolean({ char: 'l', description: 'compile contract locally' }) }; Compile.args = [{ name: 'file' }]; exports.default = Compile;