var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.listPipes=exports.executePipe=exports.executeHook=exports.listHooks=exports.buildHooks=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));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 _prompt=require("../systemTools/prompt");
var _constants=require("../constants");
var _exec=require("../systemTools/exec");
var _fileutils=require("../systemTools/fileutils");
var _runner=require("../cli/runner");
var _platform=require("../cli/platform");
var _plugin=require("../cli/plugin");
var _target=require("../cli/target");
var _app=require("../cli/app");function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}

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

var PIPES=_objectSpread({},_runner.PIPES,{},_platform.PIPES,{},_plugin.PIPES,{},_target.PIPES,{},_app.PIPES);





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

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.executeHook=executeHook;

var executePipe=function executePipe(c,key){return new Promise(function(resolve,reject){
(0,_common.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,_common.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,_common.logWarning)("BUILD_HOOK Failed with error: "+e);
resolve();
});
}else{

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

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

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

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

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

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