var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.deployToNow=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _chalk=_interopRequireDefault(require("chalk"));
var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _exec=require("../systemTools/exec");
var _common=require("../common");



var _prompt=require("../systemTools/prompt");

var _runDeploymentTask=function _runDeploymentTask(c,nowConfigPath){return new Promise(function(resolve,reject){
require('dotenv').config();
var defaultBuildFolder=_path.default.join((0,_common.getAppFolder)(c,'web'),'public');
var params=[defaultBuildFolder,'-A',nowConfigPath];
if(process.env.NOW_TOKEN)params.push('-t',process.env.NOW_TOKEN);
(0,_exec.executeAsync)(c,"now "+params.join(' ')).
then(function(){return resolve();}).
catch(function(error){return reject(error);});
});};

var _createConfigFiles=function _createConfigFiles(configFilePath,envConfigPath){var nowParamsExists=arguments.length>2&&arguments[2]!==undefined?arguments[2]:false;var envContent=arguments.length>3&&arguments[3]!==undefined?arguments[3]:'';return new Promise(function(resolve,reject){
if(!_fs.default.existsSync(configFilePath)){
var content={public:true,version:2};
(0,_common.logInfo)(_chalk.default.white('now.json')+" file does not exist. Creating one for you");
(0,_prompt.askQuestion)('What is your project name?').
then(function(v){
(0,_prompt.finishQuestion)();
content.name=v;
if(!nowParamsExists){
(0,_prompt.askQuestion)('Do you have now token? If no leave empty and you will be asked to create one').
then(function(v){
(0,_prompt.finishQuestion)();
if(v){
envContent+="NOW_TOKEN="+v+"\n";
_fs.default.writeFileSync(envConfigPath,envContent);
}
_fs.default.writeFileSync(configFilePath,JSON.stringify(content,null,2));
resolve();
});
return;
}
_fs.default.writeFileSync(configFilePath,JSON.stringify(content,null,2));
resolve();
});
return;
}
resolve();
});};

var deployToNow=function deployToNow(c){return new Promise(function(resolve,reject){
var nowConfigPath=_path.default.resolve(c.paths.project.dir,'now.json');
var envConfigPath=_path.default.resolve(c.paths.project.dir,'.env');

var envContent;
try{
envContent=_fs.default.readFileSync(envConfigPath).toString();
}catch(err){
envContent='';
}

var 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;
}
});

_createConfigFiles(nowConfigPath,envConfigPath,matched,envContent).
then(function(){
_runDeploymentTask(c,nowConfigPath).
then(function(){
resolve();
}).
catch(function(err){return reject(err);});
});
});};exports.deployToNow=deployToNow;