var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=exports.taskRnvDeployNow=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _inquirer=_interopRequireDefault(require("inquirer"));
var _dotenv=_interopRequireDefault(require("dotenv"));
var _fileutils=require("../core/systemManager/fileutils");
var _exec=require("../core/systemManager/exec");
var _common=require("../core/common");
var _logger=require("../core/systemManager/logger");


var _runDeploymentTask=function _runDeploymentTask(c,nowConfigPath){return new Promise(function(resolve,reject){
_dotenv.default.config();
var defaultBuildFolder=_path.default.join((0,_common.getAppFolder)(c,c.platform),c.platform.includes('next')?'out':'public');
var params=[defaultBuildFolder,'-A',nowConfigPath];
if(process.env.NOW_TOKEN)params.push('-t',process.env.NOW_TOKEN);
var nowIsProduction=(0,_common.getConfigProp)(c,c.platform,'nowIsProduction',false)===true;

if(nowIsProduction)params.push('--prod');

(0,_exec.executeAsync)(c,"now "+params.join(' '),{interactive:true}).
then(function(){return resolve();}).
catch(function(error){return reject(error);});
});};

var _createConfigFiles=function _createConfigFiles(
configFilePath,
envConfigPath){var nowParamsExists,_envContent,envContent,content,_await$inquirer$promp,name,_await$inquirer$promp2,token,_args=arguments;return _regenerator.default.async(function _createConfigFiles$(_context){while(1){switch(_context.prev=_context.next){case 0:
nowParamsExists=_args.length>2&&_args[2]!==undefined?_args[2]:false;
_envContent=_args.length>3&&_args[3]!==undefined?_args[3]:'';

envContent=_envContent;if(
(0,_fileutils.fsExistsSync)(configFilePath)){_context.next=19;break;}
content={public:true,version:2};
(0,_logger.logInfo)(
(0,_logger.chalk)().white(
'now.json')+" file does not exist. Creating one for you");_context.next=8;return _regenerator.default.awrap(



_inquirer.default.prompt([
{
type:'input',
name:'name',
message:'What is your project name?',
validate:function validate(i){return!!i||'Please enter a name';}},

{
type:'input',
name:'token',
message:
'Do you have now token? If no leave empty and you will be asked to create one'}]));case 8:_await$inquirer$promp=_context.sent;name=_await$inquirer$promp.name;



content.name=name;if(

nowParamsExists){_context.next=18;break;}_context.next=14;return _regenerator.default.awrap(
_inquirer.default.prompt({
type:'input',
name:'token',
message:
'Do you have now token? If no leave empty and you will be asked to create one'}));case 14:_await$inquirer$promp2=_context.sent;token=_await$inquirer$promp2.token;

if(token){
envContent+="NOW_TOKEN="+token+"\n";
(0,_fileutils.fsWriteFileSync)(envConfigPath,envContent);
}return _context.abrupt("return",
(0,_fileutils.fsWriteFileSync)(
configFilePath,
JSON.stringify(content,null,2)));case 18:return _context.abrupt("return",


(0,_fileutils.fsWriteFileSync)(
configFilePath,
JSON.stringify(content,null,2)));case 19:case"end":return _context.stop();}}},null,null,null,Promise);};




var taskRnvDeployNow=function taskRnvDeployNow(c){var nowConfigPath,envConfigPath,envContent,matched;return _regenerator.default.async(function taskRnvDeployNow$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('taskRnvDeployNow');

nowConfigPath=_path.default.resolve(c.paths.project.dir,'configs',"now."+c.platform+".json");
envConfigPath=_path.default.resolve(c.paths.project.dir,'.env');


try{
envContent=(0,_fileutils.fsReadFileSync)(envConfigPath).toString();
}catch(err){
envContent='';
}

matched=false;
envContent.
split('\n').
map(function(line){return line.split('=');}).
forEach(function(_ref){var _ref2=(0,_slicedToArray2.default)(_ref,1),key=_ref2[0];
if(['NOW_TOKEN'].indexOf(key)>-1){
matched=true;
}
});_context2.next=8;return _regenerator.default.awrap(

_createConfigFiles(
nowConfigPath,
envConfigPath,
matched,
envContent));case 8:_context2.next=10;return _regenerator.default.awrap(

_runDeploymentTask(c,nowConfigPath));case 10:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.taskRnvDeployNow=taskRnvDeployNow;var _default=


{
description:'',
fn:taskRnvDeployNow,
task:'deploy now',
params:[],
platforms:[]};exports.default=_default;
//# sourceMappingURL=index.js.map