@ohos-ports/tarojs-cli 0.0.0-stage → 4.3.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/LICENSE +174 -0
  2. package/README.md +0 -3
  3. package/bin/taro +7 -0
  4. package/dist/cli.d.ts +6 -0
  5. package/dist/cli.js +252 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/commands/customCommand.d.ts +5 -0
  8. package/dist/commands/customCommand.js +23 -0
  9. package/dist/commands/customCommand.js.map +1 -0
  10. package/dist/config/index.d.ts +8 -0
  11. package/dist/config/index.js +10 -0
  12. package/dist/config/index.js.map +1 -0
  13. package/dist/config/packagesManagement.d.ts +19 -0
  14. package/dist/config/packagesManagement.js +22 -0
  15. package/dist/config/packagesManagement.js.map +1 -0
  16. package/dist/create/constants.d.ts +1 -0
  17. package/dist/create/constants.js +5 -0
  18. package/dist/create/constants.js.map +1 -0
  19. package/dist/create/creator.d.ts +9 -0
  20. package/dist/create/creator.js +31 -0
  21. package/dist/create/creator.js.map +1 -0
  22. package/dist/create/fetchTemplate.d.ts +8 -0
  23. package/dist/create/fetchTemplate.js +137 -0
  24. package/dist/create/fetchTemplate.js.map +1 -0
  25. package/dist/create/page.d.ts +65 -0
  26. package/dist/create/page.js +252 -0
  27. package/dist/create/page.js.map +1 -0
  28. package/dist/create/plugin.d.ts +16 -0
  29. package/dist/create/plugin.js +48 -0
  30. package/dist/create/plugin.js.map +1 -0
  31. package/dist/create/project.d.ts +80 -0
  32. package/dist/create/project.js +519 -0
  33. package/dist/create/project.js.map +1 -0
  34. package/dist/doctor/index.d.ts +4 -0
  35. package/dist/doctor/index.js +32 -0
  36. package/dist/doctor/index.js.map +1 -0
  37. package/dist/index.d.ts +16 -0
  38. package/dist/index.js +21 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/presets/commands/build.d.ts +3 -0
  41. package/dist/presets/commands/build.js +239 -0
  42. package/dist/presets/commands/build.js.map +1 -0
  43. package/dist/presets/commands/config.d.ts +3 -0
  44. package/dist/presets/commands/config.js +88 -0
  45. package/dist/presets/commands/config.js.map +1 -0
  46. package/dist/presets/commands/create.d.ts +3 -0
  47. package/dist/presets/commands/create.js +91 -0
  48. package/dist/presets/commands/create.js.map +1 -0
  49. package/dist/presets/commands/global-config.d.ts +3 -0
  50. package/dist/presets/commands/global-config.js +139 -0
  51. package/dist/presets/commands/global-config.js.map +1 -0
  52. package/dist/presets/commands/help.d.ts +3 -0
  53. package/dist/presets/commands/help.js +29 -0
  54. package/dist/presets/commands/help.js.map +1 -0
  55. package/dist/presets/commands/info.d.ts +3 -0
  56. package/dist/presets/commands/info.js +79 -0
  57. package/dist/presets/commands/info.js.map +1 -0
  58. package/dist/presets/commands/init.d.ts +3 -0
  59. package/dist/presets/commands/init.js +59 -0
  60. package/dist/presets/commands/init.js.map +1 -0
  61. package/dist/presets/commands/inspect.d.ts +3 -0
  62. package/dist/presets/commands/inspect.js +118 -0
  63. package/dist/presets/commands/inspect.js.map +1 -0
  64. package/dist/presets/commands/update.d.ts +3 -0
  65. package/dist/presets/commands/update.js +171 -0
  66. package/dist/presets/commands/update.js.map +1 -0
  67. package/dist/presets/constant/hooks.d.ts +13 -0
  68. package/dist/presets/constant/hooks.js +17 -0
  69. package/dist/presets/constant/hooks.js.map +1 -0
  70. package/dist/presets/constant/index.d.ts +1 -0
  71. package/dist/presets/constant/index.js +18 -0
  72. package/dist/presets/constant/index.js.map +1 -0
  73. package/dist/presets/files/generateFrameworkInfo.d.ts +3 -0
  74. package/dist/presets/files/generateFrameworkInfo.js +30 -0
  75. package/dist/presets/files/generateFrameworkInfo.js.map +1 -0
  76. package/dist/presets/files/generateProjectConfig.d.ts +3 -0
  77. package/dist/presets/files/generateProjectConfig.js +40 -0
  78. package/dist/presets/files/generateProjectConfig.js.map +1 -0
  79. package/dist/presets/files/writeFileToDist.d.ts +3 -0
  80. package/dist/presets/files/writeFileToDist.js +17 -0
  81. package/dist/presets/files/writeFileToDist.js.map +1 -0
  82. package/dist/presets/hooks/build.d.ts +3 -0
  83. package/dist/presets/hooks/build.js +22 -0
  84. package/dist/presets/hooks/build.js.map +1 -0
  85. package/dist/presets/hooks/create.d.ts +3 -0
  86. package/dist/presets/hooks/create.js +11 -0
  87. package/dist/presets/hooks/create.js.map +1 -0
  88. package/dist/presets/index.d.ts +4 -0
  89. package/dist/presets/index.js +17 -0
  90. package/dist/presets/index.js.map +1 -0
  91. package/dist/presets/platforms/plugin.d.ts +3 -0
  92. package/dist/presets/platforms/plugin.js +76 -0
  93. package/dist/presets/platforms/plugin.js.map +1 -0
  94. package/dist/presets/platforms/rn.d.ts +3 -0
  95. package/dist/presets/platforms/rn.js +101 -0
  96. package/dist/presets/platforms/rn.js.map +1 -0
  97. package/dist/util/appConfig.d.ts +11 -0
  98. package/dist/util/appConfig.js +25 -0
  99. package/dist/util/appConfig.js.map +1 -0
  100. package/dist/util/createPage.d.ts +9 -0
  101. package/dist/util/createPage.js +117 -0
  102. package/dist/util/createPage.js.map +1 -0
  103. package/dist/util/defineConfig.d.ts +17 -0
  104. package/dist/util/defineConfig.js +12 -0
  105. package/dist/util/defineConfig.js.map +1 -0
  106. package/dist/util/index.d.ts +23 -0
  107. package/dist/util/index.js +125 -0
  108. package/dist/util/index.js.map +1 -0
  109. package/dist/util/types.d.ts +58 -0
  110. package/dist/util/types.js +3 -0
  111. package/dist/util/types.js.map +1 -0
  112. package/global.d.ts +4 -0
  113. package/index.js +3 -0
  114. package/package.json +76 -4
  115. package/postinstall.js +14 -0
  116. package/src/__tests__/__mocks__/presets.ts +15 -0
  117. package/src/__tests__/build-config.spec.ts +99 -0
  118. package/src/__tests__/cli.spec.ts +233 -0
  119. package/src/__tests__/config.spec.ts +232 -0
  120. package/src/__tests__/doctor-config.spec.ts +805 -0
  121. package/src/__tests__/doctor-eslint.spec.ts +31 -0
  122. package/src/__tests__/doctor-recommand.spec.ts +158 -0
  123. package/src/__tests__/doctor.spec.ts +77 -0
  124. package/src/__tests__/dotenv-parse.spec.ts +105 -0
  125. package/src/__tests__/env/.env +2 -0
  126. package/src/__tests__/fixtures/default/.env +3 -0
  127. package/src/__tests__/fixtures/default/.env.development +1 -0
  128. package/src/__tests__/fixtures/default/.env.local +1 -0
  129. package/src/__tests__/fixtures/default/.env.pre +3 -0
  130. package/src/__tests__/fixtures/default/.env.production +1 -0
  131. package/src/__tests__/fixtures/default/.env.uat +5 -0
  132. package/src/__tests__/fixtures/default/.env.uat.local +1 -0
  133. package/src/__tests__/fixtures/default/babel.config.js +10 -0
  134. package/src/__tests__/fixtures/default/config/dev.js +9 -0
  135. package/src/__tests__/fixtures/default/config/index.js +84 -0
  136. package/src/__tests__/fixtures/default/config/prod.js +18 -0
  137. package/src/__tests__/fixtures/default/package.json +38 -0
  138. package/src/__tests__/fixtures/default/src/app.config.js +11 -0
  139. package/src/__tests__/fixtures/default/src/app.js +16 -0
  140. package/src/__tests__/fixtures/default/src/app.scss +0 -0
  141. package/src/__tests__/fixtures/default/src/index.html +19 -0
  142. package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
  143. package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
  144. package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
  145. package/src/__tests__/info.spec.ts +74 -0
  146. package/src/__tests__/inspect.spec.ts +163 -0
  147. package/src/__tests__/project.spec.ts +304 -0
  148. package/src/__tests__/update.spec.ts +324 -0
  149. package/src/__tests__/utils/index.ts +54 -0
  150. package/src/cli.ts +250 -0
  151. package/src/commands/customCommand.ts +26 -0
  152. package/src/config/index.ts +7 -0
  153. package/src/config/manifest.default.json +35 -0
  154. package/src/config/packagesManagement.ts +20 -0
  155. package/src/config/rn-stylelint.json +5 -0
  156. package/src/config/tsconfig.json +24 -0
  157. package/src/create/constants.ts +1 -0
  158. package/src/create/creator.ts +37 -0
  159. package/src/create/fetchTemplate.ts +152 -0
  160. package/src/create/page.ts +299 -0
  161. package/src/create/plugin.ts +55 -0
  162. package/src/create/project.ts +591 -0
  163. package/src/doctor/index.ts +27 -0
  164. package/src/index.ts +24 -0
  165. package/src/presets/commands/build.ts +227 -0
  166. package/src/presets/commands/config.ts +86 -0
  167. package/src/presets/commands/create.ts +105 -0
  168. package/src/presets/commands/global-config.ts +140 -0
  169. package/src/presets/commands/help.ts +19 -0
  170. package/src/presets/commands/info.ts +77 -0
  171. package/src/presets/commands/init.ts +66 -0
  172. package/src/presets/commands/inspect.ts +138 -0
  173. package/src/presets/commands/update.ts +179 -0
  174. package/src/presets/constant/hooks.ts +14 -0
  175. package/src/presets/constant/index.ts +1 -0
  176. package/src/presets/files/generateFrameworkInfo.ts +30 -0
  177. package/src/presets/files/generateProjectConfig.ts +41 -0
  178. package/src/presets/files/writeFileToDist.ts +17 -0
  179. package/src/presets/hooks/build.ts +22 -0
  180. package/src/presets/hooks/create.ts +11 -0
  181. package/src/presets/index.ts +15 -0
  182. package/src/presets/platforms/plugin.ts +81 -0
  183. package/src/presets/platforms/rn.ts +113 -0
  184. package/src/util/appConfig.ts +29 -0
  185. package/src/util/createPage.ts +132 -0
  186. package/src/util/defineConfig.ts +22 -0
  187. package/src/util/index.ts +127 -0
  188. package/src/util/types.ts +81 -0
  189. package/templates/default/.husky/commit-msg +4 -0
  190. package/templates/default/_editorconfig +12 -0
  191. package/templates/default/_env.development +2 -0
  192. package/templates/default/_env.production +1 -0
  193. package/templates/default/_env.test +1 -0
  194. package/templates/default/_eslintrc +11 -0
  195. package/templates/default/_gitignore +8 -0
  196. package/templates/default/babel.config.js +14 -0
  197. package/templates/default/commitlint.config.mjs +1 -0
  198. package/templates/default/config/dev.js +10 -0
  199. package/templates/default/config/index.js +106 -0
  200. package/templates/default/config/prod.js +46 -0
  201. package/templates/default/package.json.tmpl +158 -0
  202. package/templates/default/project.config.json +15 -0
  203. package/templates/default/src/app.config.js +11 -0
  204. package/templates/default/src/app.css +0 -0
  205. package/templates/default/src/app.js +44 -0
  206. package/templates/default/src/index.html +17 -0
  207. package/templates/default/src/pages/index/index.config.js +3 -0
  208. package/templates/default/src/pages/index/index.css +0 -0
  209. package/templates/default/src/pages/index/index.jsx +15 -0
  210. package/templates/default/src/pages/index/index.vue +23 -0
  211. package/templates/default/stylelint.config.mjs +4 -0
  212. package/templates/default/template_creator.js +77 -0
  213. package/templates/default/tsconfig.json +32 -0
  214. package/templates/default/types/global.d.ts +35 -0
  215. package/templates/default/types/solid.d.ts +12 -0
  216. package/templates/default/types/vue.d.ts +10 -0
  217. package/templates/global-config/index.json +4 -0
  218. package/templates/global-config/package.json +5 -0
  219. package/templates/plugin-compile/README.md +32 -0
  220. package/templates/plugin-compile/index.js +3 -0
  221. package/templates/plugin-compile/package.json.tmpl +43 -0
  222. package/templates/plugin-compile/src/index.ts +153 -0
  223. package/templates/plugin-compile/tsconfig.json +29 -0
@@ -0,0 +1,591 @@
1
+ import * as path from 'node:path'
2
+
3
+ import { CompilerType, createProject, CSSType, FrameworkType, NpmType, PeriodType } from '@tarojs/binding'
4
+ import {
5
+ chalk,
6
+ DEFAULT_TEMPLATE_SRC,
7
+ DEFAULT_TEMPLATE_SRC_GITEE,
8
+ fs,
9
+ getUserHomeDir,
10
+ SOURCE_DIR,
11
+ TARO_BASE_CONFIG,
12
+ TARO_CONFIG_FOLDER
13
+ } from '@tarojs/helper'
14
+ import { isArray } from '@tarojs/shared'
15
+ import axios from 'axios'
16
+ import * as inquirer from 'inquirer'
17
+ import * as ora from 'ora'
18
+ import * as semver from 'semver'
19
+
20
+ import { clearConsole, getPkgVersion, getRootPath } from '../util'
21
+ import { TEMPLATE_CREATOR } from './constants'
22
+ import Creator from './creator'
23
+ import fetchTemplate from './fetchTemplate'
24
+
25
+ import type { ITemplates } from './fetchTemplate'
26
+
27
+ export interface IProjectConf {
28
+ projectName: string
29
+ projectDir: string
30
+ npm: NpmType
31
+ templateSource: string
32
+ clone?: boolean
33
+ template: string
34
+ description?: string
35
+ typescript?: boolean
36
+ buildEs5?: boolean
37
+ css: CSSType
38
+ date?: string
39
+ src?: string
40
+ sourceRoot?: string
41
+ env?: string
42
+ autoInstall?: boolean
43
+ hideDefaultTemplate?: boolean
44
+ framework: FrameworkType
45
+ compiler?: CompilerType
46
+ platforms?: string[]
47
+ ask?: (config: object) => Promise<void> | void
48
+ /**
49
+ * 内部标记:由 `taro init .` / `./` 触发。
50
+ * 表示目标目录即当前工作目录,projectName 已被解析为当前目录名。
51
+ * 仅在 CLI 内部流转,不会传递给底层 createProject。
52
+ */
53
+ initInCurrentDir?: boolean
54
+ }
55
+
56
+ type CustomPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
57
+
58
+ type IProjectConfOptions = CustomPartial<IProjectConf, 'projectName' | 'projectDir' | 'template' | 'css' | 'npm' | 'framework' | 'templateSource'>
59
+
60
+ interface AskMethods {
61
+ (conf: IProjectConfOptions, prompts: Record<string, unknown>[], choices?: ITemplates[]): void
62
+ }
63
+
64
+ const NONE_AVAILABLE_TEMPLATE = '无可用模板'
65
+
66
+ function resolveProjectConf (conf: IProjectConfOptions): IProjectConfOptions {
67
+ if (!conf.projectName || !/^\.(?:[\\/])?$/.test(conf.projectName)) return conf
68
+
69
+ const projectDir = conf.projectDir || process.cwd()
70
+ const currentDirName = path.basename(projectDir)
71
+ // 文件系统根目录(如 `/` 或 `C:\`)下 basename 为空,无法推断项目名,
72
+ // 显式报错而非静默传空名,避免在根目录创建文件、init git、装依赖。
73
+ if (!currentDirName) {
74
+ throw new Error('无法在文件系统根目录下初始化项目,请切换到具体目录,或使用 `taro init <项目名>` 指定项目名称')
75
+ }
76
+ return {
77
+ ...conf,
78
+ projectDir: path.dirname(projectDir),
79
+ projectName: currentDirName,
80
+ initInCurrentDir: true
81
+ }
82
+ }
83
+
84
+ export default class Project extends Creator {
85
+ public rootPath: string
86
+ public conf: IProjectConfOptions
87
+
88
+ constructor (options: IProjectConfOptions) {
89
+ super(options.sourceRoot)
90
+ const unSupportedVer = semver.lt(process.version, 'v18.0.0')
91
+ if (unSupportedVer) {
92
+ throw new Error('Node.js 版本过低,推荐升级 Node.js 至 v18.0.0+')
93
+ }
94
+ this.rootPath = this._rootPath
95
+
96
+ this.conf = resolveProjectConf(Object.assign(
97
+ {
98
+ projectName: '',
99
+ projectDir: '',
100
+ template: '',
101
+ description: '',
102
+ npm: ''
103
+ },
104
+ options
105
+ ))
106
+ }
107
+
108
+ init () {
109
+ clearConsole()
110
+ console.log(chalk.green('Taro 即将创建一个新项目!'))
111
+ console.log(`Need help? Go and open issue: ${chalk.blueBright('https://tls.jd.com/taro-issue-helper')}`)
112
+ console.log()
113
+ }
114
+
115
+ async create () {
116
+ try {
117
+ const answers = await this.ask()
118
+ const date = new Date()
119
+ this.conf = Object.assign(this.conf, answers)
120
+ this.conf.date = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
121
+ this.write()
122
+ } catch (error) {
123
+ console.log(chalk.red('创建项目失败: ', error))
124
+ }
125
+ }
126
+
127
+ async ask () {
128
+ let prompts: Record<string, unknown>[] = []
129
+ const conf = this.conf
130
+
131
+ this.askProjectName(conf, prompts)
132
+ this.askDescription(conf, prompts)
133
+ this.askFramework(conf, prompts)
134
+ this.askTypescript(conf, prompts)
135
+ this.askBuildEs5(conf, prompts)
136
+ this.askCSS(conf, prompts)
137
+ this.askPlatforms(conf, prompts)
138
+ this.askNpm(conf, prompts)
139
+ const answers = await inquirer.prompt<IProjectConf>(prompts)
140
+
141
+ // Note: 由于 Solid 框架适配 Vite 还存在某些问题,所以在选择 Solid 框架时,不再询问编译工具
142
+ prompts = []
143
+ if (answers.framework === FrameworkType.Solid || conf.framework === FrameworkType.Solid) {
144
+ answers.compiler = CompilerType.Webpack5
145
+ } else {
146
+ this.askCompiler(conf, prompts)
147
+ }
148
+ await this.askTemplateSource(conf, prompts)
149
+ const compilerAndTemplateSourceAnswer = await inquirer.prompt<IProjectConf>(prompts)
150
+
151
+ prompts = []
152
+ const templates = await this.fetchTemplates(Object.assign({}, answers, compilerAndTemplateSourceAnswer))
153
+ await this.askTemplate(conf, prompts, templates)
154
+ const templateChoiceAnswer = await inquirer.prompt<IProjectConf>(prompts)
155
+
156
+ // 导航步骤扩展
157
+ try {
158
+ if (typeof conf.ask === 'function') {
159
+ const { ask, ...other } = conf
160
+ await ask({ ...other, templatePath: this.templatePath(templateChoiceAnswer.template) })
161
+ }
162
+ } catch (e) {
163
+ console.error(e)
164
+ }
165
+
166
+ return {
167
+ ...answers,
168
+ ...compilerAndTemplateSourceAnswer,
169
+ ...templateChoiceAnswer
170
+ }
171
+ }
172
+
173
+ askProjectName: AskMethods = function (conf, prompts) {
174
+ // `taro init .` 场景:目标目录即当前目录,projectName 已解析为当前目录名,
175
+ // 目录存在是预期行为,跳过“同名目录已存在”校验,避免误报。
176
+ if (conf.initInCurrentDir) return
177
+
178
+ if ((typeof conf.projectName) !== 'string') {
179
+ prompts.push({
180
+ type: 'input',
181
+ name: 'projectName',
182
+ message: '请输入项目名称!',
183
+ validate (input) {
184
+ if (!input) {
185
+ return '项目名不能为空!'
186
+ }
187
+ if (fs.existsSync(input)) {
188
+ return '当前目录已经存在同名项目,请换一个项目名!'
189
+ }
190
+ return true
191
+ }
192
+ })
193
+ } else if (fs.existsSync(conf.projectName!)) {
194
+ prompts.push({
195
+ type: 'input',
196
+ name: 'projectName',
197
+ message: '当前目录已经存在同名项目,请换一个项目名!',
198
+ validate (input) {
199
+ if (!input) {
200
+ return '项目名不能为空!'
201
+ }
202
+ if (fs.existsSync(input)) {
203
+ return '项目名依然重复!'
204
+ }
205
+ return true
206
+ }
207
+ })
208
+ }
209
+ }
210
+
211
+ askDescription: AskMethods = function (conf, prompts) {
212
+ if (typeof conf.description !== 'string') {
213
+ prompts.push({
214
+ type: 'input',
215
+ name: 'description',
216
+ message: '请输入项目介绍'
217
+ })
218
+ }
219
+ }
220
+
221
+ askTypescript: AskMethods = function (conf, prompts) {
222
+ if (typeof conf.typescript !== 'boolean') {
223
+ prompts.push({
224
+ type: 'confirm',
225
+ name: 'typescript',
226
+ message: '是否需要使用 TypeScript ?'
227
+ })
228
+ }
229
+ }
230
+
231
+ askBuildEs5: AskMethods = function (conf, prompts) {
232
+ if (typeof conf.buildEs5 !== 'boolean') {
233
+ prompts.push({
234
+ type: 'confirm',
235
+ name: 'buildEs5',
236
+ message: '是否需要编译为 ES5 ?',
237
+ default: false
238
+ })
239
+ }
240
+ }
241
+
242
+ askCSS: AskMethods = function (conf, prompts) {
243
+ const cssChoices = [
244
+ {
245
+ name: 'Sass',
246
+ value: CSSType.Sass
247
+ },
248
+ {
249
+ name: 'Less',
250
+ value: CSSType.Less
251
+ },
252
+ {
253
+ name: 'Stylus',
254
+ value: CSSType.Stylus
255
+ },
256
+ {
257
+ name: '无',
258
+ value: CSSType.None
259
+ }
260
+ ]
261
+
262
+ if (typeof conf.css !== 'string') {
263
+ prompts.push({
264
+ type: 'list',
265
+ name: 'css',
266
+ message: '请选择 CSS 预处理器(Sass/Less/Stylus)',
267
+ choices: cssChoices
268
+ })
269
+ }
270
+ }
271
+
272
+ askPlatforms: AskMethods = function (conf, prompts) {
273
+ if (typeof conf.platforms === 'string') {
274
+ conf.platforms = (conf.platforms as unknown as string).split(',').map(p => p.trim()).filter(Boolean)
275
+ }
276
+
277
+ const platformChoices = [
278
+ { name: '微信小程序', value: 'weapp' },
279
+ { name: '支付宝小程序', value: 'alipay' },
280
+ { name: '百度小程序', value: 'swan' },
281
+ { name: '字节跳动小程序', value: 'tt' },
282
+ { name: 'QQ 小程序', value: 'qq' },
283
+ { name: '京东小程序', value: 'jd' },
284
+ { name: 'H5', value: 'h5' },
285
+ { name: '鸿蒙元服务(Hybrid)', value: 'harmony-hybrid' },
286
+ { name: 'React Native', value: 'rn' }
287
+ ]
288
+
289
+ if (isArray(conf.platforms)) {
290
+ const supportedPlatforms = new Set(platformChoices.map(choice => choice.value))
291
+ const invalidPlatforms = conf.platforms.filter(
292
+ platform => !supportedPlatforms.has(platform)
293
+ )
294
+
295
+ if (conf.platforms.length === 0 || invalidPlatforms.length > 0) {
296
+ throw new Error(`无效的平台配置: ${invalidPlatforms.join(', ') || '未选择平台'}`)
297
+ }
298
+ return
299
+ }
300
+
301
+ prompts.push({
302
+ type: 'checkbox',
303
+ name: 'platforms',
304
+ message: '请选择需要支持的平台?',
305
+ choices: platformChoices,
306
+ default: ['weapp', 'h5'],
307
+ validate (input: string[]) {
308
+ return input.length > 0 || '请至少选择一个平台!'
309
+ }
310
+ })
311
+ }
312
+
313
+ askCompiler: AskMethods = function (conf, prompts) {
314
+ const compilerChoices = [
315
+ {
316
+ name: 'Webpack5',
317
+ value: CompilerType.Webpack5
318
+ },
319
+ {
320
+ name: 'Vite',
321
+ value: CompilerType.Vite
322
+ }
323
+ ]
324
+
325
+ if (typeof conf.compiler !== 'string') {
326
+ prompts.push({
327
+ type: 'list',
328
+ name: 'compiler',
329
+ message: '请选择编译工具',
330
+ choices: compilerChoices
331
+ })
332
+ }
333
+ }
334
+
335
+ askFramework: AskMethods = function (conf, prompts) {
336
+ const frameworks = [
337
+ {
338
+ name: 'React',
339
+ value: FrameworkType.React
340
+ },
341
+ {
342
+ name: 'PReact',
343
+ value: FrameworkType.Preact
344
+ },
345
+ {
346
+ name: 'Vue3',
347
+ value: FrameworkType.Vue3
348
+ },
349
+ {
350
+ name: 'Solid',
351
+ value: FrameworkType.Solid
352
+ }
353
+ ]
354
+
355
+ if (typeof conf.framework !== 'string') {
356
+ prompts.push({
357
+ type: 'list',
358
+ name: 'framework',
359
+ message: '请选择框架',
360
+ choices: frameworks
361
+ })
362
+ }
363
+ }
364
+
365
+ askTemplateSource: AskMethods = async function (conf, prompts) {
366
+ if (conf.template === 'default' || conf.templateSource) return
367
+
368
+ const homedir = getUserHomeDir()
369
+ const taroConfigPath = path.join(homedir, TARO_CONFIG_FOLDER)
370
+ const taroConfig = path.join(taroConfigPath, TARO_BASE_CONFIG)
371
+
372
+ let localTemplateSource: string
373
+
374
+ // 检查本地配置
375
+ if (fs.existsSync(taroConfig)) {
376
+ // 存在则把模板源读出来
377
+ const config = await fs.readJSON(taroConfig)
378
+ localTemplateSource = config?.templateSource
379
+ } else {
380
+ // 不存在则创建配置
381
+ await fs.createFile(taroConfig)
382
+ await fs.writeJSON(taroConfig, { templateSource: DEFAULT_TEMPLATE_SRC })
383
+ localTemplateSource = DEFAULT_TEMPLATE_SRC
384
+ }
385
+
386
+ const choices = [
387
+ {
388
+ name: 'Gitee(最快)',
389
+ value: DEFAULT_TEMPLATE_SRC_GITEE
390
+ },
391
+ {
392
+ name: 'Github(最新)',
393
+ value: DEFAULT_TEMPLATE_SRC
394
+ },
395
+ {
396
+ name: 'CLI 内置默认模板',
397
+ value: 'default-template'
398
+ },
399
+ {
400
+ name: '自定义',
401
+ value: 'self-input'
402
+ },
403
+ {
404
+ name: '社区优质模板源',
405
+ value: 'open-source'
406
+ }
407
+ ]
408
+
409
+ if (localTemplateSource && localTemplateSource !== DEFAULT_TEMPLATE_SRC && localTemplateSource !== DEFAULT_TEMPLATE_SRC_GITEE) {
410
+ choices.unshift({
411
+ name: `本地模板源:${localTemplateSource}`,
412
+ value: localTemplateSource
413
+ })
414
+ }
415
+
416
+ prompts.push({
417
+ type: 'list',
418
+ name: 'templateSource',
419
+ message: '请选择模板源',
420
+ choices
421
+ }, {
422
+ type: 'input',
423
+ name: 'templateSource',
424
+ message: '请输入模板源!',
425
+ askAnswered: true,
426
+ when (answers) {
427
+ return answers.templateSource === 'self-input'
428
+ }
429
+ }, {
430
+ type: 'list',
431
+ name: 'templateSource',
432
+ message: '请选择社区模板源',
433
+ async choices (answers) {
434
+ const choices = await getOpenSourceTemplates(answers.framework)
435
+ return choices
436
+ },
437
+ askAnswered: true,
438
+ when (answers) {
439
+ return answers.templateSource === 'open-source'
440
+ }
441
+ })
442
+ }
443
+
444
+ askTemplate: AskMethods = function (conf, prompts, list = []) {
445
+ const choices = list.map(item => ({
446
+ name: item.desc ? `${item.name}(${item.desc})` : item.name,
447
+ value: item.value || item.name
448
+ }))
449
+
450
+ if (!conf.hideDefaultTemplate) {
451
+ choices.unshift({
452
+ name: '默认模板',
453
+ value: 'default'
454
+ })
455
+ }
456
+
457
+ if ((typeof conf.template as 'string' | undefined) !== 'string') {
458
+ prompts.push({
459
+ type: 'list',
460
+ name: 'template',
461
+ message: '请选择模板',
462
+ choices
463
+ })
464
+ }
465
+ }
466
+
467
+ askNpm: AskMethods = function (conf, prompts) {
468
+ const packages = [
469
+ {
470
+ name: 'yarn',
471
+ value: NpmType.Yarn
472
+ },
473
+ {
474
+ name: 'pnpm',
475
+ value: NpmType.Pnpm
476
+ },
477
+ {
478
+ name: 'npm',
479
+ value: NpmType.Npm
480
+ },
481
+ {
482
+ name: 'cnpm',
483
+ value: NpmType.Cnpm
484
+ }
485
+ ]
486
+
487
+ if ((typeof conf.npm as string | undefined) !== 'string') {
488
+ prompts.push({
489
+ type: 'list',
490
+ name: 'npm',
491
+ message: '请选择包管理工具',
492
+ choices: packages
493
+ })
494
+ }
495
+ }
496
+
497
+ async fetchTemplates (answers: IProjectConf): Promise<ITemplates[]> {
498
+ const { templateSource, framework, compiler } = answers
499
+ this.conf.framework = this.conf.framework || framework || ''
500
+ this.conf.templateSource = this.conf.templateSource || templateSource
501
+
502
+ // 使用默认模版
503
+ if (answers.templateSource === 'default-template') {
504
+ this.conf.template = 'default'
505
+ answers.templateSource = DEFAULT_TEMPLATE_SRC_GITEE
506
+ }
507
+ if (this.conf.template === 'default' || answers.templateSource === NONE_AVAILABLE_TEMPLATE) return Promise.resolve([])
508
+
509
+ // 从模板源下载模板
510
+ const isClone = /gitee/.test(this.conf.templateSource) || this.conf.clone
511
+ const templateChoices = await fetchTemplate(this.conf.templateSource, this.templatePath(''), isClone)
512
+
513
+ const filterFramework = (_framework) => {
514
+ const current = this.conf.framework?.toLowerCase()
515
+
516
+ if (typeof _framework === 'string' && _framework) {
517
+ return current === _framework.toLowerCase()
518
+ } else if (isArray(_framework)) {
519
+ return _framework?.map(name => name.toLowerCase()).includes(current)
520
+ } else {
521
+ return true
522
+ }
523
+ }
524
+
525
+ const filterCompiler = (_compiler) => {
526
+ if (_compiler && isArray(_compiler)) {
527
+ return _compiler?.includes(compiler)
528
+ }
529
+ return true
530
+ }
531
+
532
+ // 根据用户选择的框架筛选模板
533
+ const newTemplateChoices: ITemplates[] = templateChoices
534
+ .filter(templateChoice => {
535
+ const { platforms, compiler } = templateChoice
536
+ return filterFramework(platforms) && filterCompiler(compiler)
537
+ })
538
+
539
+ return newTemplateChoices
540
+ }
541
+
542
+ write (cb?: () => void) {
543
+ this.conf.src = SOURCE_DIR
544
+ const { projectName, projectDir, template, autoInstall = true, framework, npm } = this.conf as IProjectConf
545
+ // 引入模板编写者的自定义逻辑
546
+ const templatePath = this.templatePath(template)
547
+ const handlerPath = path.join(templatePath, TEMPLATE_CREATOR)
548
+ const handler = fs.existsSync(handlerPath) ? require(handlerPath).handler : {}
549
+ createProject({
550
+ projectRoot: projectDir,
551
+ projectName,
552
+ template,
553
+ npm,
554
+ framework,
555
+ css: this.conf.css || CSSType.None,
556
+ autoInstall: autoInstall,
557
+ templateRoot: getRootPath(),
558
+ version: getPkgVersion(),
559
+ typescript: this.conf.typescript,
560
+ buildEs5: this.conf.buildEs5,
561
+ date: this.conf.date,
562
+ description: this.conf.description,
563
+ compiler: this.conf.compiler,
564
+ period: PeriodType.CreateAPP,
565
+ platforms: this.conf.platforms,
566
+ }, handler).then(() => {
567
+ cb && cb()
568
+ })
569
+ }
570
+ }
571
+
572
+ function getOpenSourceTemplates (platform: string) {
573
+ return new Promise((resolve, reject) => {
574
+ const spinner = ora({ text: '正在拉取开源模板列表...', discardStdin: false }).start()
575
+ axios.get('https://gitee.com/NervJS/awesome-taro/raw/next/index.json')
576
+ .then(response => {
577
+ spinner.succeed(`${chalk.grey('拉取开源模板列表成功!')}`)
578
+ const collection = response.data
579
+ switch (platform.toLowerCase()) {
580
+ case 'react':
581
+ return resolve(collection.react)
582
+ default:
583
+ return resolve([NONE_AVAILABLE_TEMPLATE])
584
+ }
585
+ })
586
+ .catch(_error => {
587
+ spinner.fail(chalk.red('拉取开源模板列表失败!'))
588
+ return reject(new Error())
589
+ })
590
+ })
591
+ }
@@ -0,0 +1,27 @@
1
+ import {
2
+ validateConfig,
3
+ validateEnv,
4
+ validateEslint,
5
+ validatePackage,
6
+ validateRecommend
7
+ } from '@tarojs/plugin-doctor'
8
+
9
+ export default {
10
+ validators: [
11
+ () => {
12
+ return validateEnv.call(this)
13
+ },
14
+ (args) => {
15
+ return validateConfig.call(this, args.projectConfig, args.helper)
16
+ },
17
+ (args) => {
18
+ return validatePackage.call(this, args.appPath, args.nodeModulesPath)
19
+ },
20
+ (args) => {
21
+ return validateRecommend.call(this, args.appPath)
22
+ },
23
+ async (args) => {
24
+ return await validateEslint.call(this, args.projectConfig, args.chalk)
25
+ }
26
+ ]
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ import Creator from './create/creator'
2
+ import Project from './create/project'
3
+ import doctor from './doctor'
4
+ import { getRootPath } from './util'
5
+ import { type ConfigEnv, type UserConfigExport, type UserConfigFn, defineConfig } from './util/defineConfig'
6
+
7
+ export default {
8
+ doctor,
9
+ Project,
10
+ Creator,
11
+ defineConfig,
12
+ getRootPath
13
+ }
14
+
15
+ export {
16
+ type ConfigEnv,
17
+ type UserConfigExport,
18
+ type UserConfigFn,
19
+ Creator,
20
+ defineConfig,
21
+ doctor,
22
+ getRootPath,
23
+ Project
24
+ }