var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.executeTelnet=exports.openCommand=exports.commandExistsSync=exports.commandExists=exports.execCLI=exports.executeAsync=exports.parseErrorMessage=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));



var _path=_interopRequireDefault(require("path"));
var _fs=require("fs");
var _execa=_interopRequireDefault(require("execa"));
var _ora=_interopRequireDefault(require("ora"));
var _client=_interopRequireDefault(require("netcat/client"));
var _config=_interopRequireDefault(require("../configManager/config"));

var _logger=require("./logger");
var _fileutils=require("./fileutils");
var _utils=require("./utils");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(Object(source),true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}

var _require=require('child_process'),exec=_require.exec,execSync=_require.execSync;



















var _execute=function _execute(c,command){var _c$program,_c$program2,_opts$rawCommand,_c$program3,_child,_child$stdout,_child2,_child2$stdout;var opts=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};
var defaultOpts={
stdio:'pipe',
localDir:_path.default.resolve('./node_modules/.bin'),
preferLocal:true,
all:true,
maxErrorLength:(_c$program=c.program)==null?void 0:_c$program.maxErrorLength,
mono:(_c$program2=c.program)==null?void 0:_c$program2.mono};


if(opts.interactive){
defaultOpts.silent=true;
defaultOpts.stdio='inherit';
defaultOpts.shell=true;
}

var mergedOpts=_objectSpread(_objectSpread({},defaultOpts),opts);

var env=opts.env&&c.program.info?Object.keys(opts.env).map(function(k){return k+"="+opts.env[k];}).join(' '):null;

var cleanCommand=command;
var interval;
var intervalTimer=30000;
var timer=intervalTimer;
var privateMask='*******';
var cleanRawCmd=((_opts$rawCommand=opts.rawCommand)==null?void 0:_opts$rawCommand.args)||[];
if(Array.isArray(command))cleanCommand=command.join(' ');

cleanCommand+=cleanRawCmd.join(' ');
var logMessage=cleanCommand;
var privateParams=mergedOpts.privateParams;
if(privateParams&&Array.isArray(privateParams)){
logMessage=(0,_utils.replaceOverridesInString)(
command,
privateParams,
privateMask);

}

logMessage=""+(env?env+" ":'')+logMessage;
(0,_logger.logDebug)("_execute: "+logMessage);
var silent=mergedOpts.silent,mono=mergedOpts.mono,maxErrorLength=mergedOpts.maxErrorLength,ignoreErrors=mergedOpts.ignoreErrors;
var spinner=!silent&&!mono&&(0,_ora.default)({text:"Executing: "+logMessage}).start();
if(opts.interactive){
(0,_logger.logRaw)((0,_logger.chalk)().green('✔')+" Executing: "+logMessage+"\n");
}

if(mono){
interval=setInterval(function(){
(0,_logger.logRaw)("Executing: "+logMessage+" - "+timer/1000+"s");
timer+=intervalTimer;
},intervalTimer);
}
var child;
if(opts.rawCommand){
var args=opts.rawCommand.args;
child=(0,_execa.default)(command,args,mergedOpts);
}else{
child=_execa.default.command(cleanCommand,mergedOpts);
}

var MAX_OUTPUT_LENGTH=200;

var printLastLine=function printLastLine(buffer){
var text=Buffer.from(buffer).
toString().
trim();
var lastLine=text.split('\n').pop();
spinner.text=(0,_utils.replaceOverridesInString)(
lastLine.substring(0,MAX_OUTPUT_LENGTH),
privateParams,
privateMask);

if(lastLine.length===MAX_OUTPUT_LENGTH)spinner.text+='...\n';
};

if((_c$program3=c.program)!=null&&_c$program3.info&&(_child=child)!=null&&(_child$stdout=_child.stdout)!=null&&_child$stdout.pipe){
child.stdout.pipe(process.stdout);
}else if(spinner&&(_child2=child)!=null&&(_child2$stdout=_child2.stdout)!=null&&_child2$stdout.on){
child.stdout.on('data',printLastLine);
}

return child.
then(function(res){var _child3,_child3$stdout;
if((_child3=child)!=null&&(_child3$stdout=_child3.stdout)!=null&&_child3$stdout.off){
spinner&&child.stdout.off('data',printLastLine);
}

!silent&&!mono&&spinner.succeed("Executing: "+logMessage);
(0,_logger.logDebug)(
(0,_utils.replaceOverridesInString)(res.all,privateParams,privateMask));

interval&&clearInterval(interval);

return res.stdout;
}).
catch(function(err){var _child4,_child4$stdout;
if((_child4=child)!=null&&(_child4$stdout=_child4.stdout)!=null&&_child4$stdout.off){
spinner&&child.stdout.off('data',printLastLine);
}

if(!silent&&!mono&&!ignoreErrors){spinner.fail("FAILED: "+logMessage);}
(0,_logger.logDebug)(
(0,_utils.replaceOverridesInString)(err.all,privateParams,privateMask));

interval&&clearInterval(interval);

if(ignoreErrors&&!silent&&!mono){
spinner.succeed("Executing: "+logMessage);
return true;
}

var errMessage=parseErrorMessage(err.all,maxErrorLength);

if(!errMessage){
errMessage='';
}else{
errMessage+='\n\n';
}

if(err.message){
errMessage+=err.message+"\n\n";
}

if(err.stderr){
errMessage+=err.stderr+"\n\n";
}

if(err.stack){
errMessage+=err.stack+"\n\n";
}

errMessage=(0,_utils.replaceOverridesInString)(
errMessage,
privateParams,
privateMask);


return Promise.reject("COMMAND: \n\n"+
logMessage+" \n\nFAILED with ERROR: \n\n"+errMessage);

});
};












var execCLI=function execCLI(c,cli,command){var opts=arguments.length>3&&arguments[3]!==undefined?arguments[3]:{};
if(!c.program){
return Promise.reject(
'You need to pass c object as first parameter to execCLI()');

}
var p=c.cli[cli];
if(!(0,_fileutils.fsExistsSync)(p)){var _c$buildConfig;
(0,_logger.logDebug)("execCLI error: "+cli+" | "+command,'\nCLI Config:\n',c.cli,'\nSDK Config:\n',(_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.sdks);
return Promise.reject("Location of your cli "+
(0,_logger.chalk)().white(
p)+" does not exists. check your "+
(0,_logger.chalk)().white(
c.paths.workspace.config)+" file if your "+
(0,_logger.chalk)().white('sdks')+" paths are correct");

}

return _execute(c,p+" "+command,_objectSpread(_objectSpread({},opts),{},{shell:true}));
};exports.execCLI=execCLI;










var executeAsync=function executeAsync(_c,_cmd,_opts){var c,cmd,opts,result;return _regenerator.default.async(function executeAsync$(_context){while(1){switch(_context.prev=_context.next){case 0:

c=_c;
cmd=_cmd;
opts=_opts;
if(typeof c==='string'){
opts=cmd;
cmd=c;
c=_config.default.getConfig();
}
opts=opts||{};
if(cmd.includes('npm')&&process.platform==='win32'){cmd.replace('npm','npm.cmd');}_context.next=8;return _regenerator.default.awrap(




















_execute(c,cmd,opts));case 8:result=_context.sent;return _context.abrupt("return",

result);case 10:case"end":return _context.stop();}}},null,null,null,Promise);};exports.executeAsync=executeAsync;











var executeTelnet=function executeTelnet(c,port,command){return new Promise(function(resolve){
(0,_logger.logDebug)("execTelnet: "+port+" "+command);
try{
var output='';
var nc2=new _client.default();
nc2.addr(c.runtime.localhost).
port(parseInt(port,10)).
connect().
send(command+"\n");
nc2.on('data',function(data){
var resp=Buffer.from(data).toString();
output+=resp;
if(output.includes('OK'))nc2.close();
});
nc2.on('close',function(){return resolve(output);});
}catch(e){
(0,_logger.logError)(e);
resolve();
}
});};exports.executeTelnet=executeTelnet;





















var parseErrorMessage=function parseErrorMessage(text){var maxErrorLength=arguments.length>1&&arguments[1]!==undefined?arguments[1]:800;
if(!text)return'';

var gradleFailIndex=text.indexOf('FAILURE: Build failed with an exception.');
if(gradleFailIndex!==-1){
return text.substring(gradleFailIndex);
}

var nextFailIndex=text.indexOf('> Build error occurred');
if(nextFailIndex!==-1){
return text.substring(nextFailIndex);
}
var toSearch=/(exception|error|fatal|\[!])/i;
var arr=text.split('\n');

var errFound=0;
arr=arr.filter(function(v){
if(v==='')return false;

if(
v.includes('-Werror')||
v.includes('following modules are linked manually')||
v.includes('warn ')||
v.includes('note: ')||
v.includes('warning: ')||
v.includes('Could not find the following native modules')||
v.includes('⚠️')||
v.includes('/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain'))
{
return false;
}

if(
v.includes('[DEBUG]')||
v.includes('[INFO]')||
v.includes('[LIFECYCLE]')||
v.includes('[WARN]')||
v.includes(':+HeapDumpOnOutOfMemoryError')||
v.includes('.errors.')||
v.includes('-exception-')||
v.includes('error_prone_annotations'))
{
return false;
}




if(v.search(toSearch)!==-1){
errFound=5;
return true;
}
if(errFound>0){
errFound-=1;
return true;
}

return false;
});

arr=arr.map(function(str){
var v=str.replace(/\s{2,}/g,' ');
var extractedError=v.substring(0,maxErrorLength);
if(extractedError.length===maxErrorLength)extractedError+='...';
return extractedError;
});

return arr.join('\n');
};exports.parseErrorMessage=parseErrorMessage;

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

var fileNotExists=function fileNotExists(commandName,callback){
(0,_fs.access)(commandName,_fs.constants.F_OK,function(err){
callback(!err);
});
};

var fileNotExistsSync=function fileNotExistsSync(commandName){
try{
(0,_fs.accessSync)(commandName,_fs.constants.F_OK);
return false;
}catch(e){
return true;
}
};

var localExecutable=function localExecutable(commandName,callback){
(0,_fs.access)(commandName,_fs.constants.F_OK|_fs.constants.X_OK,function(err){
callback(null,!err);
});
};

var localExecutableSync=function localExecutableSync(commandName){
try{
(0,_fs.accessSync)(commandName,_fs.constants.F_OK|_fs.constants.X_OK);
return true;
}catch(e){
return false;
}
};

var commandExistsUnix=function commandExistsUnix(commandName,cleanedCommandName,callback){
fileNotExists(commandName,function(isFile){
if(!isFile){
exec(
"command -v "+cleanedCommandName+" 2>/dev/null"+(" && { echo >&1 "+
cleanedCommandName+"; exit 0; }"),
function(error,stdout){
callback(null,!!stdout);
});

return;
}

localExecutable(commandName,callback);
});
};

var commandExistsWindows=function commandExistsWindows(commandName,cleanedCommandName,callback){
if(/[\x00-\x1f<>:"|?*]/.test(commandName)){
callback(null,false);
return;
}
exec("where "+cleanedCommandName,function(error){
if(error!==null){
callback(null,false);
}else{
callback(null,true);
}
});
};

var commandExistsUnixSync=function commandExistsUnixSync(commandName,cleanedCommandName){
if(fileNotExistsSync(commandName)){
try{
var stdout=execSync(
"command -v "+cleanedCommandName+" 2>/dev/null"+(" && { echo >&1 "+
cleanedCommandName+"; exit 0; }"));

return!!stdout;
}catch(error){
return false;
}
}
return localExecutableSync(commandName);
};

var commandExistsWindowsSync=function commandExistsWindowsSync(commandName,cleanedCommandName){
if(/[\x00-\x1f<>:"|?*]/.test(commandName)){
return false;
}
try{
var stdout=execSync("where "+cleanedCommandName,{stdio:[]});
return!!stdout;
}catch(error){
return false;
}
};

var cleanInput=function cleanInput(_s){
var s=_s;
if(/[^A-Za-z0-9_/:=-]/.test(s)){
s="'"+s.replace(/'/g,"'\\''")+"'";
s=s.
replace(/^(?:'')+/g,'').
replace(/\\'''/g,"\\'");
}
return s;
};

if(isUsingWindows){
cleanInput=function cleanInput(s){
var isPathName=/[\\]/.test(s);
if(isPathName){
var dirname="\""+_path.default.dirname(s)+"\"";
var basename="\""+_path.default.basename(s)+"\"";
return dirname+":"+basename;
}
return"\""+s+"\"";
};
}

var commandExists=function commandExists(commandName,callback){
var cleanedCommandName=cleanInput(commandName);
if(!callback&&typeof Promise!=='undefined'){
return new Promise(function(resolve,reject){
commandExists(commandName,function(error,output){
if(output){
resolve(commandName);
}else{
reject(error);
}
});
});
}
if(isUsingWindows){
commandExistsWindows(commandName,cleanedCommandName,callback);
}else{
commandExistsUnix(commandName,cleanedCommandName,callback);
}
};exports.commandExists=commandExists;

var commandExistsSync=function commandExistsSync(commandName){
var cleanedCommandName=cleanInput(commandName);
if(isUsingWindows){
return commandExistsWindowsSync(commandName,cleanedCommandName);
}
return commandExistsUnixSync(commandName,cleanedCommandName);
};exports.commandExistsSync=commandExistsSync;



var openCommand=process.platform==='darwin'?
'open':
process.platform==='win32'?
'start':
'xdg-open';exports.openCommand=openCommand;var _default=










{
executeAsync:executeAsync,
execCLI:execCLI,
openCommand:openCommand,
executeTelnet:executeTelnet};exports.default=_default;
//# sourceMappingURL=exec.js.map