1 | const fs = require('fs-extra')
|
2 | const path = require('path')
|
3 | const chalk = require('chalk')
|
4 | const inquirer = require('inquirer')
|
5 | const Creator = require('./Creator')
|
6 | const { clearConsole } = require('./util/clearConsole')
|
7 | const { error, stopSpinner } = require('@vue/cli-shared-utils')
|
8 |
|
9 | async function generate (model, options) {
|
10 | const cwd = options.cwd || process.cwd()
|
11 | const targetDir = path.resolve(cwd, model || '.')
|
12 |
|
13 | if (fs.existsSync(targetDir)) {
|
14 | if (!options.force) {
|
15 | await clearConsole(true)
|
16 |
|
17 | const { action } = await inquirer.prompt([
|
18 | {
|
19 | name: 'action',
|
20 | type: 'list',
|
21 | message: `Target directory for ${chalk.cyan(model)} crud view already exists. Pick an action:`,
|
22 | choices: [
|
23 | { name: 'Overwrite', value: 'overwrite' },
|
24 | { name: 'Cancel', value: false }
|
25 | ]
|
26 | }
|
27 | ])
|
28 | if (!action) {
|
29 | return
|
30 | } else if (action === 'overwrite') {
|
31 | console.log(`\nRemoving ${chalk.cyan(model)} crud view...`)
|
32 | }
|
33 | }
|
34 | }
|
35 |
|
36 | const creator = new Creator(name, targetDir)
|
37 | await creator.generate(options)
|
38 | }
|
39 |
|
40 | module.exports = (...args) => {
|
41 | return generate(...args).catch(err => {
|
42 | stopSpinner(false)
|
43 | error(err)
|
44 | if (!process.env.CONTROLLA_CLI_TEST) {
|
45 | process.exit(1)
|
46 | }
|
47 | })
|
48 | }
|