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