<commit_before>const fs = require('fs')
const inquirer = require('inquirer')
const git = require('gift')

const { fileExist } = require('./fs.utils')
const { log } = require('./log.utils')

module.exports = exports = (configFilename) => {
  return (options) => {
    const userForceOverwrite = options.force || false
    const questions = [
      {
        name: 'overwrite_file',
        message: 'A configuration file already exist. Do you want to continue ?',
        type: 'confirm',
        when: () => fileExist(configFilename) && !userForceOverwrite
      },
      {
        name: 'repo_name',
        message: 'Repository url:',
        type: 'input',
        validate: url => URL_REGEX.test(url),
        when: data => !fileExist(configFilename) || data.overwrite_file || userForceOverwrite
      },
      {
        name: 'folder_path',
        message: 'Folder path:',
        type: 'input',
        validate: path => PATH_REGEX.test(path),
        when: data => !fileExist(configFilename) || data.overwrite_file || userForceOverwrite
      }
    ]

    inquirer.prompt(questions)
      .then(data => {
        console.log(data)
        const { repo_name = null, folder_path, overwrite_file = true } = data

        if (!overwrite_file) {
          return
        }

        if (!fileExist(folder_path)) {
          log('info', 'Folder does not exist. It will be created.', ' Info ')
          fs.mkdirSync(folder_path)
        }

        const folderStat = fs.statSync(folder_path)
        if (!folderStat.isDirectory()) {
          log('error', `${folder_path} is not a folder.`, ' Abort ')
          return
        }
        
        const folderIsEmpty = fs.readdirSync(folder_path).length < 1
        if (!folderIsEmpty) {
          log('error', 'Folder is not empty', ' Abort ')
          return
        }

        if (repo_name == null) {
          return
        }

        git.clone(repo_name, folder_path, (error, result) => {
          console.log(error, result)
        })

        fs.writeFileSync(configFilename, JSON.stringify({ repo_name, folder_path }, null, 4))
      })
  }
}

const URL_REGEX = /^((?:(https?):\/\/)?((?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9]))|(?:(?:(?:\w+\.){1,2}[\w]{2,3})))(?::(\d+))?((?:\/[\w]+)*)(?:\/|(\/[\w]+\.[\w]{3,4})|(\?(?:([\w]+=[\w]+)&)*([\w]+=[\w]+))?|\?(?:(wsdl|wadl))))$/
const PATH_REGEX = /^(?:(?:~|\.{1,2})?\/)+(?:[a-zA-Z\.\-\_]+\/?)*$/<commit_msg>Put git clone step in this own then step<commit_after>const fs = require('fs')
const inquirer = require('inquirer')

const { fileExist } = require('./fs.utils')
const { log } = require('./log.utils')
const { gitClone } = require('./git.utils')

module.exports = exports = (configFilename) => {
  return (options) => {
    const userForceOverwrite = options.force || false
    const questions = [
      {
        name: 'overwrite_file',
        message: 'A configuration file already exist. Do you want to continue ?',
        type: 'confirm',
        when: () => fileExist(configFilename) && !userForceOverwrite
      },
      {
        name: 'repo_name',
        message: 'Repository url:',
        type: 'input',
        validate: url => URL_REGEX.test(url),
        when: data => !fileExist(configFilename) || data.overwrite_file || userForceOverwrite
      },
      {
        name: 'folder_path',
        message: 'Folder path:',
        type: 'input',
        validate: path => PATH_REGEX.test(path),
        when: data => !fileExist(configFilename) || data.overwrite_file || userForceOverwrite
      }
    ]

    inquirer.prompt(questions)
      .then(data => {
        const { repo_name = null, folder_path, overwrite_file = true } = data

        if (!overwrite_file) {
          return data
        }

        if (!fileExist(folder_path)) {
          log('info', 'Folder does not exist. It will be created.', ' Info ')
          fs.mkdirSync(folder_path)
        }
        
        const folderStat = fs.statSync(folder_path)
        if (!folderStat.isDirectory()) {
          throw new Error('folder_is_not_directory')
        }
        
        const folderIsEmpty = fs.readdirSync(folder_path).length < 1
        if (!folderIsEmpty) {
          throw new Error('folder_is_not_empty')
        }
        
        fs.writeFileSync(configFilename, JSON.stringify({ repo_name, folder_path }, null, 4))
        return { repo_name, folder_path }
      })
      .then(({ repo_name, folder_path }) => {
        if (repo_name == null) {
          log('warn', 'Impossible to cloned git repository. Need repository URL.', ' Warn ')
          return { repo_name, folder_path }
        }
        
        return gitClone(repo_name, folder_path)
          .then((repo) => {
            log('info', 'Git repository cloned successuly.', ' Info ')
            return { repo_name, folder_path }
          })
      })
      .catch((error) => {
        switch (error.message) {
          case 'folder_is_not_directory':
            log('error', `${folder_path} is not a folder.`, ' Abort ')
            break
          case 'folder_is_not_empty':
            log('error', 'Folder is not empty.', ' Abort ')
            break
          default:
            log('error', 'An error occured.', ' Fail ')
            console.log(error)
        }
      })
  }
}

const URL_REGEX = /^((?:(https?):\/\/)?((?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9]))|(?:(?:(?:\w+\.){1,2}[\w]{2,3})))(?::(\d+))?((?:\/[\w]+)*)(?:\/|(\/[\w]+\.[\w]{3,4})|(\?(?:([\w]+=[\w]+)&)*([\w]+=[\w]+))?|\?(?:(wsdl|wadl))))$/
const PATH_REGEX = /^(?:(?:~|\.{1,2})?\/)+(?:[a-zA-Z\.\-\_]+\/?)*$/
{'fraction_non_alphanumeric': 0.2198581560283688, 'fraction_numerical': 0.030395136778115502, 'mean_word_length': 2.331271091113611, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1734283', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2113, 'n_words': 348}