UNPKG

2.38 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3const chalk = require('chalk')
4const inquirer = require('inquirer')
5const Creator = require('./Creator')
6const { clearConsole } = require('./util/clearConsole')
7const { error, stopSpinner, exit } = require('@vue/cli-shared-utils')
8const validateProjectName = require('validate-npm-package-name')
9
10async function create (projectName, options) {
11 const cwd = options.cwd || process.cwd()
12 const inCurrent = projectName === '.'
13 const name = inCurrent ? path.relative('../', cwd) : projectName
14 const targetDir = path.resolve(cwd, projectName || '.')
15
16 const result = validateProjectName(name)
17 if (!result.validForNewPackages) {
18 console.error(chalk.red(`Invalid project name: "${name}"`))
19 result.errors && result.errors.forEach(err => {
20 console.error(chalk.red.dim('Error: ' + err))
21 })
22 result.warnings && result.warnings.forEach(warn => {
23 console.error(chalk.red.dim('Warning: ' + warn))
24 })
25 exit(1)
26 }
27
28 if (fs.existsSync(targetDir)) {
29 if (options.force) {
30 await fs.remove(targetDir)
31 } else {
32 await clearConsole(true)
33 if (inCurrent) {
34 const { ok } = await inquirer.prompt([
35 {
36 name: 'ok',
37 type: 'confirm',
38 message: `Generate project in current directory?`
39 }
40 ])
41 if (!ok) {
42 return
43 }
44 } else {
45 const { action } = await inquirer.prompt([
46 {
47 name: 'action',
48 type: 'list',
49 message: `Target directory ${chalk.cyan(targetDir)} already exists. Pick an action:`,
50 choices: [
51 { name: 'Overwrite', value: 'overwrite' },
52 { name: 'Merge', value: 'merge' },
53 { name: 'Cancel', value: false }
54 ]
55 }
56 ])
57 if (!action) {
58 return
59 } else if (action === 'overwrite') {
60 console.log(`\nRemoving ${chalk.cyan(targetDir)}...`)
61 await fs.remove(targetDir)
62 }
63 }
64 }
65 }
66 await fs.mkdirs(targetDir)
67
68 const creator = new Creator(name, targetDir)
69 await creator.create(options)
70}
71
72module.exports = (...args) => {
73 return create(...args).catch(err => {
74 stopSpinner(false) // do not persist
75 error(err)
76 if (!process.env.CONTROLLA_CLI_TEST) {
77 process.exit(1)
78 }
79 })
80}