UNPKG

tronbox

Version:

TronBox - Simple development framework for Tron

1 lines 4.91 kB
"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _typeof2=_interopRequireDefault(require("@babel/runtime/helpers/typeof"));var EventEmitter=require("events").EventEmitter;var vcsurl=require("vcsurl");var axios=require("axios");var path=require("path");var fs=require("fs-extra");var util=require("util");var cwd=process.cwd();function GithubDownloader(user,repo,ref,dir){this.user=user;this.repo=repo;this.ref=ref||"master";this.dir=dir;this._log=[];this._getZip=false}util.inherits(GithubDownloader,EventEmitter);GithubDownloader.prototype.start=function(){var _this=this;var initialUrl="https://api.github.com/repos/"+this.user+"/"+this.repo+"/contents/";var initialUrlRef=this.ref?"?ref="+this.ref:"";var rawUrl="https://raw.github.com/"+this.user+"/"+this.repo+"/"+this.ref+"/";var pending=0;var gonnaProcess=0;gonnaProcess+=1;requestJSON.call(this,initialUrl+initialUrlRef,processItems);function processItems(items){pending+=items.length;gonnaProcess-=1;items.forEach(handleItem);checkDone()}function handleItem(item){if(item.type==="dir"){var dir=path.join(_this.dir,item.path);fs.mkdirs(dir,function(err){if(err)_this.emit("error",err);_this._log.push(dir);gonnaProcess+=1;requestJSON.call(_this,initialUrl+item.path+initialUrlRef,processItems);_this.emit("dir",item.path);pending-=1;checkDone()})}else if(item.type==="file"){var file=path.join(_this.dir,item.path);fs.createFile(file,function(err){if(err)_this.emit("error",err);axios.get(rawUrl+item.path,{responseType:"stream"}).then(function(response){response.data.pipe(fs.createWriteStream(file)).on("close",function(){_this._log.push(file);_this.emit("file",item.path);pending-=1;checkDone()})})["catch"](function(err){return _this.emit("error",err)})})}else{_this.emit("Error",new Error(JSON.stringify(item,null,2)+"\n does not have type."))}}function checkDone(){if(pending===0&&gonnaProcess===0&&!_this._getZip){_this.emit("end")}}return this};module.exports=function GithubDownload(params,dir){if(typeof params==="string"){var pieces=params.split("#");var ref=pieces[1];var url=(vcsurl(pieces[0])||pieces[0]).split("/");params={user:url[url.length-2],repo:url[url.length-1],ref:ref}}if((0,_typeof2["default"])(params)!=="object"){throw new Error("Invalid parameter type. Should be repo URL string or object containing repo and user.")}dir=dir||process.cwd();var gh=new GithubDownloader(params.user,params.repo,params.ref,dir);return gh.start()};function requestJSON(url,callback){var _this=this;axios.get(url).then(function(response){callback(response.data)})["catch"](function(err){if(err.response&&err.response.status===403){return downloadZip.call(_this)}if(err.response&&err.response.status!==200){_this.emit("error",new Error(url+": returned "+err.response.status+"\n\nbody:\n"+err.response.data))}else{_this.emit("error",err)}})}function extractZip(zipFile,outputDir,callback){var yauzl=require("yauzl");var _this=this;yauzl.open(zipFile,{lazyEntries:true},function(err,zipfile){if(err)return _this.emit("error",err);var folderName=null;var pending=0;zipfile.on("entry",function(entry){if(!folderName&&entry.fileName.includes("/")){folderName=entry.fileName.split("/")[0]}if(/\/$/.test(entry.fileName)){zipfile.readEntry()}else{pending++;zipfile.openReadStream(entry,function(err,readStream){if(err)return _this.emit("error",err);var file=path.resolve(outputDir,entry.fileName);fs.ensureDir(path.dirname(file),function(err){if(err)return _this.emit("error",err);var writeStream=fs.createWriteStream(file);readStream.pipe(writeStream);writeStream.on("close",function(){pending--;if(pending===0){callback(folderName||path.basename(zipFile,".zip"))}});writeStream.on("error",function(err){return _this.emit("error",err)})})});zipfile.readEntry()}});zipfile.on("end",function(){if(pending===0){callback(folderName||path.basename(zipFile,".zip"))}});zipfile.readEntry()})}function downloadZip(){var _this=this;if(_this._getZip)return;_this._getZip=true;_this._log.forEach(function(file){fs.remove(file)});var tmpdir=generateTempDir();var zipBaseDir=_this.repo+"-"+_this.ref;var zipFile=path.join(tmpdir,zipBaseDir+".zip");var zipUrl="https://github.com/"+_this.user+"/"+_this.repo+"/archive/"+_this.ref+".zip";_this.emit("zip",zipUrl);fs.mkdir(tmpdir,function(err){if(err)_this.emit("error",err);axios.get(zipUrl,{responseType:"stream"}).then(function(response){response.data.pipe(fs.createWriteStream(zipFile)).on("close",function(){extractZip.call(_this,zipFile,tmpdir,function(extractedFolderName){var oldPath=path.join(tmpdir,extractedFolderName);fs.rename(oldPath,_this.dir,function(err){if(err)_this.emit("error",err);fs.remove(tmpdir,function(err){if(err)_this.emit("error",err);_this.emit("end")})})})})})["catch"](function(err){return _this.emit("error",err)})})}function generateTempDir(){return path.join(cwd,Date.now().toString()+"-"+Math.random().toString().substring(2))}