tronbox
Version:
TronBox - Simple development framework for Tron
1 lines • 3.48 kB
JavaScript
var ReplManager=require("./repl");var Command=require("./command");var provision=require("../components/Provisioner");var contract=require("../components/Contract");var TronWrap=require("../components/TronWrap");var vm=require("vm");var expect=require("@truffle/expect");var TruffleError=require("@truffle/error");var fs=require("fs");var path=require("path");var EventEmitter=require("events");var inherits=require("util").inherits;var logErrorAndExit=require("../components/TronWrap").logErrorAndExit;inherits(Console,EventEmitter);function Console(tasks,options){EventEmitter.call(this);var self=this;expect.options(options,["working_directory","contracts_directory","contracts_build_directory","migrations_directory","network","network_id","provider","resolver","build_directory"]);this.options=options;this.repl=options.repl||new ReplManager(options);this.command=new Command(tasks);if(!options.network&&options.networks.development){options.network="development"}try{this.tronWrap=TronWrap(options.networks[options.network],{evm:options.evm,verify:true,log:options.log})}catch(err){logErrorAndExit(console,err.message)}this.repl.on("exit",function(){self.emit("exit")})}Console.prototype.start=function(callback){var self=this;if(!this.repl){this.repl=new ReplManager(this.options)}this.options.repl=this.repl;this.provision(function(err,abstractions){if(err){self.options.logger.log("Unexpected error: Cannot provision contracts while instantiating the console.");self.options.logger.log(err.stack||err.message||err)}self.repl.start({prompt:"tronbox("+self.options.network+")> ",context:{tronWrap:self.tronWrap,tronWeb:self.tronWrap,web3:self.tronWrap._web3?self.tronWrap._web3:undefined},interpreter:self.interpret.bind(self),done:callback});self.resetContractsInConsoleContext(abstractions)})};Console.prototype.provision=function(callback){var self=this;fs.readdir(this.options.contracts_build_directory,function(err,files){if(err){}var promises=[];files=files||[];files.forEach(function(file){promises.push(new Promise(function(accept,reject){fs.readFile(path.join(self.options.contracts_build_directory,file),"utf8",function(err,body){if(err)return reject(err);try{body=JSON.parse(body)}catch(e){return reject(new Error("Cannot parse "+file+": "+e.message))}accept(body)})}))});Promise.all(promises).then(function(json_blobs){var abstractions=json_blobs.map(function(json){var abstraction=contract(json);provision(abstraction,self.options);return abstraction});self.resetContractsInConsoleContext(abstractions);callback(null,abstractions)})["catch"](callback)})};Console.prototype.resetContractsInConsoleContext=function(abstractions){var self=this;abstractions=abstractions||[];var contextVars={};abstractions.forEach(function(abstraction){contextVars[abstraction.contract_name]=abstraction});self.repl.setContextVars(contextVars)};Console.prototype.interpret=function(cmd,context,filename,callback){var self=this;if(cmd.trim()!=="help"&&this.command.getCommand(cmd.trim(),this.options.noAliases)!=null){return self.command.run(cmd.trim(),this.options,function(err){if(err){if(err instanceof TruffleError){console.error(err.message)}else{console.error(err.stack||err.toString())}return callback()}self.provision(function(err){callback(err)})})}var result;try{result=vm.runInContext(cmd,context,{displayErrors:false})}catch(e){return callback(e)}Promise.resolve(result).then(function(res){callback(null,res)})["catch"](callback)};module.exports=Console;
;