UNPKG

tronbox

Version:

TronBox - Simple development framework for Tron

1 lines 1.97 kB
"use strict";var Schema=require("./ContractSchema");var fs=require("fs-extra");var path=require("path");var _=require("lodash");function Artifactor(destination){this.destination=destination}Artifactor.prototype.save=function(object,options){var self=this;return new Promise(function(accept,reject){object=Schema.normalize(object);Object.values(object.networks).forEach(function(_){return _.address=_.address.toLowerCase().replace(/^0x/,"41")});if(options.evm){Object.values(object.networks).forEach(function(_){return _.address=_.address.toLowerCase().replace(/^41/,"0x")})}if(!object.contractName){return reject(new Error("You must specify a contract name."))}var output_path=object.contractName;output_path=path.join(self.destination,output_path);output_path=path.resolve(output_path);output_path=output_path+".json";fs.readFile(output_path,{encoding:"utf8"},function(err,json){var finalObject=object;if(!err){var existingObjDirty;try{existingObjDirty=JSON.parse(json)}catch(e){reject(e)}finalObject=Schema.normalize(existingObjDirty);var finalNetworks={};_.merge(finalNetworks,finalObject.networks,object.networks);_.assign(finalObject,object);finalObject.networks=finalNetworks}finalObject.updatedAt=new Date().toISOString();fs.outputFile(output_path,JSON.stringify(finalObject,null,2),"utf8",function(err){if(err)return reject(err);accept()})})})};Artifactor.prototype.saveAll=function(objects,options){var self=this;if(Array.isArray(objects)){var array=objects;objects={};array.forEach(function(item){objects[item.contract_name]=item})}return new Promise(function(accept,reject){fs.stat(self.destination,function(err){if(err){return reject(new Error("Destination "+self.destination+" doesn't exist!"))}accept()})}).then(function(){var promises=[];Object.keys(objects).forEach(function(contractName){var object=objects[contractName];object.contractName=contractName;promises.push(self.save(object,options))});return Promise.all(promises)})};module.exports=Artifactor;