UNPKG

3.27 kBJavaScriptView Raw
1'use strict'
2
3const chalk = require('chalk')
4const { prompt, Separator } = require('inquirer')
5const config = require('../config')
6const { getPageList } = require('./utils')
7const pages = getPageList(config.paths.entries)
8
9// TL
10// 识别 entry, branch
11// 兼容 yarn 与 npm
12// 可指定输入页面名,或选择页面名
13
14// npm run build
15// npm run build --ftp
16// npm run build --ftp test
17// yarn build
18// yarn build index --ftp
19// yarn build index --ftp test
20// 输入出错
21
22function 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
39async 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
49function getEntryArgs(args, field) {
50 let val = null
51
52 config.build[`arg_${field}`] = process.env[`npm_config_${field}`]
53
54 // npx marax build --ftp
55 // yarn run build --ftp
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 // 兼容 npm run build --ftp xxx
61 // 默认的 config.build.uploadFtp 为 process.env.npm_config_ftp
62 // 当无分支名时,返回 ''
63 val = args._[2] || ''
64 }
65
66 return { [field]: val }
67}
68
69function result(entry = '', args) {
70 // 未启用 ftp 上传时,返回 null
71 let ftpBranch = null
72 let entryArgs = {}
73
74 // npx marax build --ftp
75 // yarn run build --ftp
76 if (args.ftp) {
77 ftpBranch = args.ftp === true ? '' : args.ftp
78 config.build.uploadFtp = true
79 } else if (config.build.uploadFtp) {
80 // 兼容 npm run build --ftp xxx
81 // 默认的 config.build.uploadFtp 为 process.env.npm_config_ftp
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
95function 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
106function chooseMany(args) {
107 const entry = args._[1]
108
109 if (validEntry(entry)) return result(entry, args)
110
111 return chooseEntry(entry && '您输入的页面有误, 请选择:', args)
112}
113
114function validEntry(entry) {
115 return pages.includes(entry)
116}
117
118async function chooseEntry(msg, args) {
119 const list = [...pages]
120 // const list = [...pages, new Separator(), { name: 'exit', value: '' }]
121 const question = {
122 type: 'list',
123 name: 'entry',
124 choices: list,
125 default: list.indexOf('index'),
126 // message 不可为空串
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
137module.exports = getEntry