var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.rnvHooksPipes=exports.executePipe=exports.rnvHooksRun=exports.rnvHooksList=exports.buildHooks=void 0;var _chalk=_interopRequireDefault(require("chalk"));
var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _child_process=_interopRequireDefault(require("child_process"));
var _common=require("../common");



var _logger=require("../systemTools/logger");
var _prompt=require("../systemTools/prompt");
var _constants=require("../constants");
var _exec=require("../systemTools/exec");
var _fileutils=require("../systemTools/fileutils");

var isRunningOnWindows=process.platform==='win32';





var rnvHooksRun=function rnvHooksRun(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('rnvHooksRun');

buildHooks(c).
then(function(){
if(c.buildHooks[c.program.exeMethod]){
c.buildHooks[c.program.exeMethod](c).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
}else{
reject("Method name "+_chalk.default.white(c.program.exeMethod)+" does not exists in your buildHooks!");
}
}).
catch(function(e){return reject(e);});
});};exports.rnvHooksRun=rnvHooksRun;

var executePipe=function executePipe(c,key){return new Promise(function(resolve,reject){
(0,_logger.logTask)("executePipe:"+key);

buildHooks(c).
then(function(){
var pipe=c.buildPipes?c.buildPipes[key]:null;

if(Array.isArray(pipe)){
var chain=pipe.
reduce(function(accumulatorPromise,next){return accumulatorPromise.then(function(){return next(c);});},Promise.resolve()).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
return;
}
if(pipe){
c.buildPipes[key](c).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
return;
}

resolve();
}).
catch(function(e){return reject(e);});
});};exports.executePipe=executePipe;

var buildHooks=function buildHooks(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('buildHooks');

if(_fs.default.existsSync(c.paths.buildHooks.index)){
if(c.isBuildHooksReady){
resolve();
return;
}

(0,_exec.executeAsync)(c,"babel --no-babelrc "+c.paths.buildHooks.dir+" -d "+c.paths.buildHooks.dist.dir+" --presets=@babel/env").
then(function(){
var h=require(c.paths.buildHooks.dist.index);
c.buildHooks=h.hooks;
c.buildPipes=h.pipes;
c.isBuildHooksReady=true;
resolve();
}).
catch(function(e){
(0,_logger.logWarning)("BUILD_HOOK Failed with error: "+e);
resolve();
});
}else{

resolve();
}
});};exports.buildHooks=buildHooks;

var rnvHooksList=function rnvHooksList(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('rnvHooksList');

buildHooks(c).
then(function(){
if(c.buildHooks){
var hookOpts=(0,_prompt.generateOptions)(c.buildHooks);
var hooksAsString="\n"+'Hooks:'+"\n"+hookOpts.asString;

if(c.buildPipes){
var pipeOpts=(0,_prompt.generateOptions)(c.buildPipes);
hooksAsString+="\n"+'Pipes:'+"\n"+pipeOpts.asString;
}
(0,_logger.logToSummary)(hooksAsString);
resolve();
}else{
reject('Your buildHooks object is empty!');
}
}).
catch(function(e){return reject(e);});
});};exports.rnvHooksList=rnvHooksList;

var rnvHooksPipes=function rnvHooksPipes(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('rnvHooksPipes');

buildHooks(c).
then(function(){
var pipeOpts=(0,_prompt.generateOptions)(c.buildPipes);
console.log("Pipes:\n"+pipeOpts.asString);
}).catch(function(e){return reject(e);});
});};exports.rnvHooksPipes=rnvHooksPipes;