const fs = require('fs-extra')
const path = require('path')
const inquirer = require('inquirer')
const chalk = require('chalk')
const Generator = require('./Generator')

const error = chalk.bold.red;
const info = chalk.bold.cyan;
const success = chalk.green

module.exports = async function (name, options) {
    // 当前目录
    const cwd  = process.cwd();
    const targetAir  = path.join(cwd, name)
    const generator = new Generator(name, targetAir)
    // 目录是否已经存在
    if (fs.existsSync(targetAir)) {
        // 覆盖存在的文件
        if (options.force) {
            await fs.remove(targetAir)
            generator.create()
        } else {
            inquirer.prompt([{
                type: 'confirm',
                name: 'remove',
                message: `是否移除文件${name}?`,
            }])
                .then(async answer => {
                    if (answer.remove) {
                        console.log(`${info(`正在移除文件${name}...`)}`)
                        await fs.remove(targetAir)
                        console.log(`${success('项目已移除...')}`)
                        generator.create()
                    } else {
                        console.log(`${error('创建项目失败...')}`)
                    }
                })
        }
    } else {
        generator.create()
    }
}
