UNPKG

tronbox

Version:

TronBox - Simple development framework for Tron

1 lines 3.4 kB
"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));var command={command:"convert",description:"Convert a truffle project to tronbox project",builder:{},run:function run(options,done){var fs=require("fs");var path=require("path");var Config=require("../../components/Config");var config=Config["default"]()["with"]({logger:console});if(!fs.existsSync(path.join(process.cwd(),"./truffle-config.js"))){config.logger.log("It's not a truffle project. Will start `tronbox init`");var InitCommand=require("./init");return InitCommand.run(options,done)}if(fs.existsSync(path.resolve(process.cwd(),"./tronbox-config.js"))){config.logger.log("It's already a tronbox project.");return}function downloadConfig(){function downloadFile(url,target){return new Promise(function(){var _ref=(0,_asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee(resolve,reject){var axios,response;return _regenerator["default"].wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.prev=0;axios=require("axios");_context.next=4;return axios.get(url,{responseType:"stream"});case 4:response=_context.sent;response.data.pipe(fs.createWriteStream(target)).on("close",resolve).on("error",reject);_context.next=11;break;case 8:_context.prev=8;_context.t0=_context["catch"](0);reject(_context.t0);case 11:case"end":return _context.stop()}},_callee,null,[[0,8]])}));return function(_x,_x2){return _ref.apply(this,arguments)}}())}var configFilePromise=downloadFile("https://raw.githubusercontent.com/tronsuper/bare-box/master/tronbox.js",path.join(process.cwd(),"./tronbox-config.js"));var migrationFilePromise=downloadFile("https://raw.githubusercontent.com/Tronbox-boxes/metacoin-box/master/contracts/Migrations.sol",path.join(process.cwd(),"./contracts/Migrations.sol"));var migrationDeployFilePromise=downloadFile("https://raw.githubusercontent.com/Tronbox-boxes/metacoin-box/master/migrations/1_initial_migration.js",path.join(process.cwd(),"./migrations/1_initial_migration.js"));return Promise.all([configFilePromise,migrationFilePromise,migrationDeployFilePromise])}function convertTests(){var testDirPath=path.join(process.cwd(),"./test");if(!fs.existsSync(testDirPath))return;var stats=fs.statSync(testDirPath);if(!stats.isDirectory())return;function translateTestFile(file){var content=fs.readFileSync(file).toString();var translatedContent="\nconst ganache = require('ganache');\nconst Web3 = require('web3');\nconst [web3, provider] = require('tronbox-web3')(new Web3(Web3.givenProvider), ganache.provider());\n".concat(content,"\n ");fs.writeFileSync(file,translatedContent);config.logger.log("convert file ".concat(file," successful"))}function walkTestDir(dir){var files=fs.readdirSync(dir);files.forEach(function(file){var currentFile=path.join(dir,file);var stats=fs.statSync(currentFile);if(stats.isDirectory())return walkTestDir(currentFile);if(!stats.isFile())return;if(path.extname(currentFile)!==".js")return;translateTestFile(currentFile)})}walkTestDir(testDirPath)}downloadConfig().then(function(){config.logger.log("OK. Conversion is done. Enjoy tronbox!");config.logger.log();done()});convertTests()}};module.exports=command;