1 | 'use strict'
|
2 |
|
3 | const chalk = require('chalk')
|
4 | const { prompt, Separator } = require('inquirer')
|
5 | const config = require('../config')
|
6 | const { getPageList } = require('./utils')
|
7 | const pages = getPageList(config.paths.entries)
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function empty() {
|
23 | console.log(`😶 ${chalk.red('请按如下结构创建入口文件')}`)
|
24 | console.log(
|
25 | `
|
26 | src
|
27 | └── view
|
28 | ├── page1
|
29 | │ ├── ${chalk.green('index.html')}
|
30 | │ └── ${chalk.green('index.(js|ts)')}
|
31 | └── page2
|
32 | ├── ${chalk.green('index.html')}
|
33 | └── ${chalk.green('index.(js|ts)')}`,
|
34 | '\n'
|
35 | )
|
36 | process.exit(1)
|
37 | }
|
38 |
|
39 | async function getEntry(args) {
|
40 | if (!pages.length) {
|
41 | empty()
|
42 | } else if (pages.length === 1) {
|
43 | return chooseOne(args)
|
44 | } else {
|
45 | return chooseMany(args)
|
46 | }
|
47 | }
|
48 |
|
49 | function getEntryArgs(args, field) {
|
50 | let val = null
|
51 |
|
52 | config.build[`arg_${field}`] = process.env[`npm_config_${field}`]
|
53 |
|
54 |
|
55 |
|
56 | if (args[field]) {
|
57 | val = args[field] === true ? '' : args[field]
|
58 | config.build[`arg_${field}`] = true
|
59 | } else if (config.build[`arg_${field}`]) {
|
60 |
|
61 |
|
62 |
|
63 | val = args._[2] || ''
|
64 | }
|
65 |
|
66 | return { [field]: val }
|
67 | }
|
68 |
|
69 | function result(entry = '', args) {
|
70 |
|
71 | let ftpBranch = null
|
72 | let entryArgs = {}
|
73 |
|
74 |
|
75 |
|
76 | if (args.ftp) {
|
77 | ftpBranch = args.ftp === true ? '' : args.ftp
|
78 | config.build.uploadFtp = true
|
79 | } else if (config.build.uploadFtp) {
|
80 |
|
81 |
|
82 |
|
83 | ftpBranch = args._[2] || ''
|
84 | }
|
85 |
|
86 | entryArgs = Object.assign(
|
87 | {},
|
88 | getEntryArgs(args, 'ftp'),
|
89 | getEntryArgs(args, 'test')
|
90 | )
|
91 |
|
92 | return Promise.resolve({ entry, ftpBranch, entryArgs })
|
93 | }
|
94 |
|
95 | function chooseOne(args) {
|
96 | const entry = args._[1]
|
97 |
|
98 | if (entry && !validEntry(entry)) {
|
99 | return chooseEntry('您输入的页面有误, 请选择:', args)
|
100 | } else {
|
101 |
|
102 | return result(pages[0], args)
|
103 | }
|
104 | }
|
105 |
|
106 | function chooseMany(args) {
|
107 | const entry = args._[1]
|
108 |
|
109 | if (validEntry(entry)) return result(entry, args)
|
110 |
|
111 | return chooseEntry(entry && '您输入的页面有误, 请选择:', args)
|
112 | }
|
113 |
|
114 | function validEntry(entry) {
|
115 | return pages.includes(entry)
|
116 | }
|
117 |
|
118 | async function chooseEntry(msg, args) {
|
119 | const list = [...pages]
|
120 |
|
121 | const question = {
|
122 | type: 'list',
|
123 | name: 'entry',
|
124 | choices: list,
|
125 | default: list.indexOf('index'),
|
126 |
|
127 | message: msg || '请选择目标页面:'
|
128 | }
|
129 | const { entry } = await prompt(question)
|
130 |
|
131 | if (!entry) process.exit(0)
|
132 | console.log()
|
133 |
|
134 | return result(entry, args)
|
135 | }
|
136 |
|
137 | module.exports = getEntry
|