UNPKG

tronbox

Version:

TronBox - Simple development framework for Tron

1 lines 3.1 kB
"use strict";var fs=require("fs-extra");var path=require("path");var axios=require("axios");var vcsurl=require("vcsurl");var tmp=require("tmp");var exec=require("child_process").exec;var ghdownload=require("./download");var cwd=process.cwd();var config=require("../config");function checkDestination(destination){return Promise.resolve().then(function(){var contents=fs.readdirSync(destination);if(contents.length){var err="Something already exists at the destination. "+"`tronbox init` and `tronbox unbox` must be executed in an empty folder. "+"Stopping to prevent overwriting data.";throw new Error(err)}})}function verifyURL(url){return new Promise(function(accept,reject){var configURL=new URL(vcsurl(url).replace("github.com","raw.githubusercontent.com").replace(/#.*/,"")+"/master/tronbox.js");var targetUrl="https://"+configURL.host+configURL.pathname;axios.head(targetUrl).then(function(){return accept()})["catch"](function(error){if(error.response&&error.response.status===404){return reject(new Error("TronBox Box at URL "+url+" doesn't exist. If you believe this is an error, please contact TronBox support."))}else{return reject(new Error("Error connecting to github.com. Please check your internet connection and try again."))}})})}function setupTempDirectory(){return new Promise(function(accept,reject){tmp.dir({dir:cwd,unsafeCleanup:true},function(err,dir,cleanupCallback){if(err)return reject(err);accept(path.join(dir,"box"),cleanupCallback)})})}function fetchRepository(url,dir){return new Promise(function(accept,reject){ghdownload(url,dir).on("err",function(err){reject(err)}).on("end",function(){accept()})})}function copyTempIntoDestination(tmpDir,destination){return new Promise(function(accept,reject){fs.copy(tmpDir,destination,function(err){if(err)return reject(err);accept()})})}function readBoxConfig(destination){var possibleConfigs=[path.join(destination,"tronbox.json"),path.join(destination,"tronbox-init.json")];var configPath=possibleConfigs.reduce(function(path,alt){return path||fs.existsSync(alt)&&alt},undefined);return config.read(configPath)}function cleanupUnpack(boxConfig,destination){var needingRemoval=boxConfig.ignore||[];needingRemoval.push("tronbox.json");needingRemoval.push("tronbox-init.json");var promises=needingRemoval.map(function(file_path){return path.join(destination,file_path)}).map(function(file_path){return new Promise(function(accept,reject){fs.remove(file_path,function(err){if(err)return reject(err);accept()})})});return Promise.all(promises)}function installBoxDependencies(boxConfig,destination){var postUnpack=boxConfig.hooks["post-unpack"];return new Promise(function(accept,reject){if(postUnpack.length===0){return accept()}exec(postUnpack,{cwd:destination},function(err,stdout,stderr){if(err)return reject(err);accept(stdout,stderr)})})}module.exports={checkDestination:checkDestination,verifyURL:verifyURL,setupTempDirectory:setupTempDirectory,fetchRepository:fetchRepository,copyTempIntoDestination:copyTempIntoDestination,readBoxConfig:readBoxConfig,cleanupUnpack:cleanupUnpack,installBoxDependencies:installBoxDependencies};