1 | #!/usr/bin/env node
|
2 |
|
3 | const inquirer = require('inquirer')
|
4 |
|
5 | const chalk = require('chalk')
|
6 | const fs = require('fs')
|
7 | const path = require('path')
|
8 |
|
9 | const {generate, getTemplates} = require('./index')
|
10 |
|
11 | const templates = getTemplates()
|
12 |
|
13 | const questions = [
|
14 | {
|
15 | type: 'input',
|
16 | name: 'name',
|
17 | message: "What's your project name?",
|
18 | validate: (value) => {
|
19 | value = value.trim();
|
20 | if (!value) {
|
21 | return 'Project name could not be empty!';
|
22 | }
|
23 | if (/\s/g.test(value)) {
|
24 | return 'Project name could not have spacing!';
|
25 | }
|
26 | return true;
|
27 | }
|
28 | },
|
29 | {
|
30 | type: 'list',
|
31 | name: 'template',
|
32 | message: 'Which template will you choose?',
|
33 | choices: templates.map((item) => `${item.template} (${item.description})`),
|
34 | filter: (val) => {
|
35 | return val.toLowerCase();
|
36 | }
|
37 | },
|
38 | {
|
39 | type: 'input',
|
40 | name: 'path',
|
41 | message: 'What\'s path will your project stay?',
|
42 | validate: (value) => {
|
43 | value = value.trim();
|
44 | if (!value) {
|
45 | return 'Project name could not be empty!';
|
46 | }
|
47 | return true;
|
48 | }
|
49 | }
|
50 | ];
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | inquirer.prompt(questions).then((answers) => {
|
57 | const projectName = answers.name.trim()
|
58 | const projectStayPath = answers.path.trim()
|
59 |
|
60 |
|
61 | let templateName
|
62 | for(let item of templates) {
|
63 | if(answers.template.startsWith(item.template)) {
|
64 | templateName = item.template
|
65 | break
|
66 | }
|
67 | }
|
68 | if(!templateName) {
|
69 | return console.log(chalk.red(`! template not found :( `))
|
70 | }
|
71 |
|
72 |
|
73 | const r = generate(templateName, projectName, projectStayPath)
|
74 | if(r.status === 0) {
|
75 | return console.log(chalk.red(r.message))
|
76 | }
|
77 | console.log(chalk.green(`√ generate project succeed :) `))
|
78 | });
|