text: const fs = require('fs-extra')
  const path = require('path')
  const inquirer = require('inquirer')
  const Creator = require('./Creator')
  const {
    clearConsole,
    chalk,
    error,
    exit,
  } = require('young-common-utils')
  const { getPromptModules } = require('./util/createTools')
  const validateProjectName = require('validate-npm-package-name')
  
  async function create(projectName) {
    const cwd = process.cwd()
    const inCurrent = projectName === '.'
    const name = inCurrent ? path.relative('../', cwd) : projectName
    const targetDir = path.resolve(cwd, projectName || '.')
  
    // 校验项目名
    const result = validateProjectName(name)
    if (!result.validForNewPackages) {
      console.error(chalk.red(`Invalid project name: "${name}"`))
      result.errors &&
        result.errors.forEach((err) => {
          console.error(chalk.red.dim('Error: ' + err))
        })
      result.warnings &&
        result.warnings.forEach((warn) => {
          console.error(chalk.red.dim('Warning: ' + warn))
        })
      exit(1)
    }
  
    if (fs.existsSync(targetDir)) {
      clearConsole()
      if (inCurrent) {
        const { ok } = await inquirer.prompt([
          {
            name: 'ok',
            type: 'confirm',
            message: `Generate project in current directory?`
          },
        ])
        if (!ok) {
          return
        }
      } else {
        const { action } = await inquirer.prompt([
          {
            name: 'action',
            type: 'list',
            message: `Target directory ${chalk.cyan(
              targetDir,
            )} already exists. Pick an action:`,
            choices: [
              { name: 'Overwrite', value: 'overwrite' },
              { name: 'Merge', value: 'merge' },
              { name: 'Cancel', value: false },
            ],
          },
        ])
        if (!action) {
          return
        } else if (action === 'overwrite') {
          console.log(`\nRemoving ${chalk.cyan(targetDir)}...`)
          await fs.remove(targetDir)
        }
      }
    }
  
    const creator = new Creator(name, targetDir, getPromptModules())
    await creator.create()
  }
  
  module.exports = (...args) => {
    return create(...args).catch((err) => {
      error(err)
      process.exit(1)
    })
  }
  