#!/usr/bin/env node

const { program } = require("commander");
const chalk = require("chalk");
const prompts = require("prompts");
const path = require("path");
const { install, initGit } = require("./helpers/install");
const package = require("./package.json");
const copy = require("./helpers/copy");
const Listr = require("listr");

// Output path to create new portal app
let projectPath = ''

// Commander parameters to specify CLI behavior
program
  .name(package.name)
  .version(package.version)
  /**
   * TODO 
   * Add Options
   * Add Example Options
   * Add templates
   */
  .arguments("[dir]")
  .usage(`${chalk.yellow("[dir]")}`)
  .description({
    dir: "Directory to be used on install Portal.js",
  })
  .action((name) => (projectPath = name))
  .allowUnknownOption()
  .parse(process.argv);


  /**
   * Method to ask a custon name if was not passed as parameter
   * returns the value passed from terminal input
   */
  async function promptPath(){
    return prompts({
      type: 'text',
      name: 'path',
      message: 'Choose a name to your project',
      initial: '',
      validate: name => {
        //TODO Method to validate valid path name
        return true
      }
    })
  
  }

  /**
   * Main method to start CLI and validate inputs
   */
  async function run(){
    if(typeof projectPath === 'string'){ 
      projectPath = projectPath.trim()
    }
    if(!projectPath){
      const response = await promptPath()
      if(typeof response.path === 'string'){
        projectPath = response.path.trim()
      }
    }
    if(!projectPath){
      console.log()
      console.log('Please choose a name to your project:')
      console.log()
      console.log('Example:')
      console.log(`${chalk.cyan(program.name())} ${chalk.yellow('ny-portal-app')}`)
      console.log()
  
      process.exit(1)
    }
  
    /**
     * TODO Include workflow to create and manage the files and options
     * 
     * Example: 
     * createApp()
     *   .then(installDependencies)
     *   .then(renameFiles)
     *   .then(accessFolder)
     *   .then(checkUpdated) 
     */
  }

  //Main CLI execution workflow
  run()
  .then(`${chalk.greenBright('Project Installed Sucess')}`)
  .catch(error => {
    if(error.command){
      console.log(`${chalk.cyan('Error on Create App')}`)
    }else{
      console.log(`${chalk.red('Unexpected Erro. Please report it as a bug')}`)
      console.log(error)
    }
    console.log()
    process.exit(1)
  })
@@ -808,24 +808,20 @@
 rgv);%0A%0A%0A
-  
 /**%0A
-  
  * Metho
@@ -874,18 +874,16 @@
 rameter%0A
-  
  * retur
@@ -922,24 +922,20 @@
 l input%0A
-  
  */%0A
-  
 async fu
@@ -953,20 +953,19 @@
 ptPath()
+ 
 %7B%0A
-  
   return
@@ -975,18 +975,16 @@
 ompts(%7B%0A
-  
     type
@@ -989,38 +989,34 @@
 pe: 
-'
+%22
 text
-'
+%22
 ,%0A    
-  
 name: 
-'
+%22
 path
-'
+%22
 ,%0A
-  
     
@@ -1024,17 +1024,17 @@
 essage: 
-'
+%22
 Choose a
@@ -1054,21 +1054,19 @@
  project
-'
+%22
 ,%0A
-  
     init
@@ -1074,14 +1074,12 @@
 al: 
-'',%0A  
+%22%22,%0A
     
@@ -1092,20 +1092,20 @@
 te: 
+(
 name
+)
  =%3E %7B%0A
-  
     
@@ -1154,18 +1154,16 @@
 e%0A      
-  
 return t
@@ -1169,29 +1169,23 @@
 true
+;
 %0A
- 
     
- %7D%0A 
+%7D,%0A
   
- 
 %7D)
+;
 %0A
-  %0A  
 %7D%0A%0A 

update linter fixes