UNPKG

1.34 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 } = require('@vue/cli-shared-utils')
8
9async 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
40module.exports = (...args) => {
41 return generate(...args).catch(err => {
42 stopSpinner(false) // do not persist
43 error(err)
44 if (!process.env.CONTROLLA_CLI_TEST) {
45 process.exit(1)
46 }
47 })
48}