1 | 'use strict'
|
2 |
|
3 | const fs = require('fs')
|
4 | const chalk = require('chalk')
|
5 | const prompts = require('prompts')
|
6 | const config = require('../config')
|
7 | const { getViews, rootPath } = require('./utils')
|
8 | const skeleton = require('./skeleton')
|
9 | const views = getViews(config.paths.entryGlob)
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function empty() {
|
25 | let msg = '请按如下结构创建页面'
|
26 |
|
27 | if (fs.existsSync(rootPath('src/view'))) {
|
28 | msg += ',如果您从 marax@1.x 迁移,请将 view 目录重命名为 views'
|
29 | }
|
30 |
|
31 | console.log(chalk.red(msg))
|
32 | console.log(skeleton.project, '\n')
|
33 | process.exit(0)
|
34 | }
|
35 |
|
36 | function getEntryArgs(argv, optField) {
|
37 | let val = null
|
38 |
|
39 | config.build[`arg_${optField}`] = process.env[`npm_config_${optField}`]
|
40 |
|
41 |
|
42 |
|
43 | if (argv[optField] !== undefined) {
|
44 | val = argv[optField] === true ? '' : argv[optField]
|
45 | config.build[`arg_${optField}`] = true
|
46 | } else if (config.build[`arg_${optField}`]) {
|
47 |
|
48 |
|
49 |
|
50 | val = argv._[2] || ''
|
51 | }
|
52 |
|
53 | return { [optField]: val }
|
54 | }
|
55 |
|
56 | function result(entry = '', argv) {
|
57 |
|
58 | let ftpBranch = null
|
59 | let entryArgs = {}
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | if (argv.ftp !== undefined) {
|
65 | ftpBranch = argv.ftp === true ? '' : argv.ftp
|
66 | config.build.uploadFtp = true
|
67 | } else if (config.build.uploadFtp) {
|
68 |
|
69 |
|
70 |
|
71 | ftpBranch = argv._[2] || ''
|
72 | }
|
73 |
|
74 | entryArgs = Object.assign(
|
75 | {},
|
76 | getEntryArgs(argv, 'ftp'),
|
77 | getEntryArgs(argv, 'test')
|
78 | )
|
79 |
|
80 | return Promise.resolve({ entry, ftpBranch, entryArgs })
|
81 | }
|
82 |
|
83 | function chooseOne(argv) {
|
84 | const entry = argv._[1]
|
85 |
|
86 | if (entry && !validEntry(entry)) {
|
87 | return chooseEntry('Incorrect view, please re-pick', argv)
|
88 | } else {
|
89 |
|
90 | return result(views[0], argv)
|
91 | }
|
92 | }
|
93 |
|
94 | function chooseMany(argv) {
|
95 | const entry = argv._[1]
|
96 |
|
97 | if (validEntry(entry)) return result(entry, argv)
|
98 |
|
99 | return chooseEntry(entry && 'Incorrect view, please re-pick', argv)
|
100 | }
|
101 |
|
102 | function validEntry(entry) {
|
103 | return views.includes(entry)
|
104 | }
|
105 |
|
106 | async function chooseEntry(msg, argv) {
|
107 | const list = [...views]
|
108 | const initial = list.indexOf('index')
|
109 |
|
110 | const question = {
|
111 | type: 'autocomplete',
|
112 | name: 'entry',
|
113 | choices: list.map(view => ({ title: view, value: view })),
|
114 | initial: initial < 0 ? 0 : initial,
|
115 |
|
116 | message: msg || 'Pick target view'
|
117 | }
|
118 |
|
119 | const { entry } = await prompts(question)
|
120 |
|
121 | if (!entry) process.exit(0)
|
122 | console.log()
|
123 |
|
124 | return result(entry, argv)
|
125 | }
|
126 |
|
127 | module.exports = async function getEntry(argv) {
|
128 | if (!views.length) {
|
129 | empty()
|
130 | } else if (views.length === 1) {
|
131 | return chooseOne(argv)
|
132 | } else {
|
133 | return chooseMany(argv)
|
134 | }
|
135 | }
|