UNPKG

3.26 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const chalk = require('chalk')
5const prompts = require('prompts')
6const config = require('../config')
7const { getViews, rootPath } = require('./utils')
8const skeleton = require('./skeleton')
9const views = getViews(config.paths.entryGlob)
10
11// TL
12// 识别 entry, branch
13// 兼容 yarn 与 npm
14// 可指定输入页面名,或选择页面名
15
16// npm run build
17// npm run build --ftp
18// npm run build --ftp test
19// yarn build
20// yarn build index --ftp
21// yarn build index --ftp test
22// 输入出错
23
24function 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
36function getEntryArgs(argv, optField) {
37 let val = null
38
39 config.build[`arg_${optField}`] = process.env[`npm_config_${optField}`]
40
41 // npx marax build --ftp
42 // yarn run build --ftp
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 // 兼容 npm run build --ftp xxx
48 // 默认的 config.build.uploadFtp 为 process.env.npm_config_ftp
49 // 当无分支名时,返回 ''
50 val = argv._[2] || ''
51 }
52
53 return { [optField]: val }
54}
55
56function result(entry = '', argv) {
57 // 未启用 ftp 上传时,返回 null
58 let ftpBranch = null
59 let entryArgs = {}
60
61 // npx marax build --ftp
62 // npm run build --ftp
63 // yarn build --ftp
64 if (argv.ftp !== undefined) {
65 ftpBranch = argv.ftp === true ? '' : argv.ftp
66 config.build.uploadFtp = true
67 } else if (config.build.uploadFtp) {
68 // 兼容 npm run build --ftp xxx
69 // 默认的 config.build.uploadFtp 为 process.env.npm_config_ftp
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
83function 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
94function 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
102function validEntry(entry) {
103 return views.includes(entry)
104}
105
106async 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 // message 不可为空串
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
127module.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}