@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.
- package/LICENSE +174 -0
- package/README.md +0 -3
- package/bin/taro +7 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +252 -0
- package/dist/cli.js.map +1 -0
- package/dist/commands/customCommand.d.ts +5 -0
- package/dist/commands/customCommand.js +23 -0
- package/dist/commands/customCommand.js.map +1 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +10 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/packagesManagement.d.ts +19 -0
- package/dist/config/packagesManagement.js +22 -0
- package/dist/config/packagesManagement.js.map +1 -0
- package/dist/create/constants.d.ts +1 -0
- package/dist/create/constants.js +5 -0
- package/dist/create/constants.js.map +1 -0
- package/dist/create/creator.d.ts +9 -0
- package/dist/create/creator.js +31 -0
- package/dist/create/creator.js.map +1 -0
- package/dist/create/fetchTemplate.d.ts +8 -0
- package/dist/create/fetchTemplate.js +137 -0
- package/dist/create/fetchTemplate.js.map +1 -0
- package/dist/create/page.d.ts +65 -0
- package/dist/create/page.js +252 -0
- package/dist/create/page.js.map +1 -0
- package/dist/create/plugin.d.ts +16 -0
- package/dist/create/plugin.js +48 -0
- package/dist/create/plugin.js.map +1 -0
- package/dist/create/project.d.ts +80 -0
- package/dist/create/project.js +519 -0
- package/dist/create/project.js.map +1 -0
- package/dist/doctor/index.d.ts +4 -0
- package/dist/doctor/index.js +32 -0
- package/dist/doctor/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/presets/commands/build.d.ts +3 -0
- package/dist/presets/commands/build.js +239 -0
- package/dist/presets/commands/build.js.map +1 -0
- package/dist/presets/commands/config.d.ts +3 -0
- package/dist/presets/commands/config.js +88 -0
- package/dist/presets/commands/config.js.map +1 -0
- package/dist/presets/commands/create.d.ts +3 -0
- package/dist/presets/commands/create.js +91 -0
- package/dist/presets/commands/create.js.map +1 -0
- package/dist/presets/commands/global-config.d.ts +3 -0
- package/dist/presets/commands/global-config.js +139 -0
- package/dist/presets/commands/global-config.js.map +1 -0
- package/dist/presets/commands/help.d.ts +3 -0
- package/dist/presets/commands/help.js +29 -0
- package/dist/presets/commands/help.js.map +1 -0
- package/dist/presets/commands/info.d.ts +3 -0
- package/dist/presets/commands/info.js +79 -0
- package/dist/presets/commands/info.js.map +1 -0
- package/dist/presets/commands/init.d.ts +3 -0
- package/dist/presets/commands/init.js +59 -0
- package/dist/presets/commands/init.js.map +1 -0
- package/dist/presets/commands/inspect.d.ts +3 -0
- package/dist/presets/commands/inspect.js +118 -0
- package/dist/presets/commands/inspect.js.map +1 -0
- package/dist/presets/commands/update.d.ts +3 -0
- package/dist/presets/commands/update.js +171 -0
- package/dist/presets/commands/update.js.map +1 -0
- package/dist/presets/constant/hooks.d.ts +13 -0
- package/dist/presets/constant/hooks.js +17 -0
- package/dist/presets/constant/hooks.js.map +1 -0
- package/dist/presets/constant/index.d.ts +1 -0
- package/dist/presets/constant/index.js +18 -0
- package/dist/presets/constant/index.js.map +1 -0
- package/dist/presets/files/generateFrameworkInfo.d.ts +3 -0
- package/dist/presets/files/generateFrameworkInfo.js +30 -0
- package/dist/presets/files/generateFrameworkInfo.js.map +1 -0
- package/dist/presets/files/generateProjectConfig.d.ts +3 -0
- package/dist/presets/files/generateProjectConfig.js +40 -0
- package/dist/presets/files/generateProjectConfig.js.map +1 -0
- package/dist/presets/files/writeFileToDist.d.ts +3 -0
- package/dist/presets/files/writeFileToDist.js +17 -0
- package/dist/presets/files/writeFileToDist.js.map +1 -0
- package/dist/presets/hooks/build.d.ts +3 -0
- package/dist/presets/hooks/build.js +22 -0
- package/dist/presets/hooks/build.js.map +1 -0
- package/dist/presets/hooks/create.d.ts +3 -0
- package/dist/presets/hooks/create.js +11 -0
- package/dist/presets/hooks/create.js.map +1 -0
- package/dist/presets/index.d.ts +4 -0
- package/dist/presets/index.js +17 -0
- package/dist/presets/index.js.map +1 -0
- package/dist/presets/platforms/plugin.d.ts +3 -0
- package/dist/presets/platforms/plugin.js +76 -0
- package/dist/presets/platforms/plugin.js.map +1 -0
- package/dist/presets/platforms/rn.d.ts +3 -0
- package/dist/presets/platforms/rn.js +101 -0
- package/dist/presets/platforms/rn.js.map +1 -0
- package/dist/util/appConfig.d.ts +11 -0
- package/dist/util/appConfig.js +25 -0
- package/dist/util/appConfig.js.map +1 -0
- package/dist/util/createPage.d.ts +9 -0
- package/dist/util/createPage.js +117 -0
- package/dist/util/createPage.js.map +1 -0
- package/dist/util/defineConfig.d.ts +17 -0
- package/dist/util/defineConfig.js +12 -0
- package/dist/util/defineConfig.js.map +1 -0
- package/dist/util/index.d.ts +23 -0
- package/dist/util/index.js +125 -0
- package/dist/util/index.js.map +1 -0
- package/dist/util/types.d.ts +58 -0
- package/dist/util/types.js +3 -0
- package/dist/util/types.js.map +1 -0
- package/global.d.ts +4 -0
- package/index.js +3 -0
- package/package.json +76 -4
- package/postinstall.js +14 -0
- package/src/__tests__/__mocks__/presets.ts +15 -0
- package/src/__tests__/build-config.spec.ts +99 -0
- package/src/__tests__/cli.spec.ts +233 -0
- package/src/__tests__/config.spec.ts +232 -0
- package/src/__tests__/doctor-config.spec.ts +805 -0
- package/src/__tests__/doctor-eslint.spec.ts +31 -0
- package/src/__tests__/doctor-recommand.spec.ts +158 -0
- package/src/__tests__/doctor.spec.ts +77 -0
- package/src/__tests__/dotenv-parse.spec.ts +105 -0
- package/src/__tests__/env/.env +2 -0
- package/src/__tests__/fixtures/default/.env +3 -0
- package/src/__tests__/fixtures/default/.env.development +1 -0
- package/src/__tests__/fixtures/default/.env.local +1 -0
- package/src/__tests__/fixtures/default/.env.pre +3 -0
- package/src/__tests__/fixtures/default/.env.production +1 -0
- package/src/__tests__/fixtures/default/.env.uat +5 -0
- package/src/__tests__/fixtures/default/.env.uat.local +1 -0
- package/src/__tests__/fixtures/default/babel.config.js +10 -0
- package/src/__tests__/fixtures/default/config/dev.js +9 -0
- package/src/__tests__/fixtures/default/config/index.js +84 -0
- package/src/__tests__/fixtures/default/config/prod.js +18 -0
- package/src/__tests__/fixtures/default/package.json +38 -0
- package/src/__tests__/fixtures/default/src/app.config.js +11 -0
- package/src/__tests__/fixtures/default/src/app.js +16 -0
- package/src/__tests__/fixtures/default/src/app.scss +0 -0
- package/src/__tests__/fixtures/default/src/index.html +19 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.config.js +3 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.jsx +22 -0
- package/src/__tests__/fixtures/default/src/pages/index/index.scss +0 -0
- package/src/__tests__/info.spec.ts +74 -0
- package/src/__tests__/inspect.spec.ts +163 -0
- package/src/__tests__/project.spec.ts +304 -0
- package/src/__tests__/update.spec.ts +324 -0
- package/src/__tests__/utils/index.ts +54 -0
- package/src/cli.ts +250 -0
- package/src/commands/customCommand.ts +26 -0
- package/src/config/index.ts +7 -0
- package/src/config/manifest.default.json +35 -0
- package/src/config/packagesManagement.ts +20 -0
- package/src/config/rn-stylelint.json +5 -0
- package/src/config/tsconfig.json +24 -0
- package/src/create/constants.ts +1 -0
- package/src/create/creator.ts +37 -0
- package/src/create/fetchTemplate.ts +152 -0
- package/src/create/page.ts +299 -0
- package/src/create/plugin.ts +55 -0
- package/src/create/project.ts +591 -0
- package/src/doctor/index.ts +27 -0
- package/src/index.ts +24 -0
- package/src/presets/commands/build.ts +227 -0
- package/src/presets/commands/config.ts +86 -0
- package/src/presets/commands/create.ts +105 -0
- package/src/presets/commands/global-config.ts +140 -0
- package/src/presets/commands/help.ts +19 -0
- package/src/presets/commands/info.ts +77 -0
- package/src/presets/commands/init.ts +66 -0
- package/src/presets/commands/inspect.ts +138 -0
- package/src/presets/commands/update.ts +179 -0
- package/src/presets/constant/hooks.ts +14 -0
- package/src/presets/constant/index.ts +1 -0
- package/src/presets/files/generateFrameworkInfo.ts +30 -0
- package/src/presets/files/generateProjectConfig.ts +41 -0
- package/src/presets/files/writeFileToDist.ts +17 -0
- package/src/presets/hooks/build.ts +22 -0
- package/src/presets/hooks/create.ts +11 -0
- package/src/presets/index.ts +15 -0
- package/src/presets/platforms/plugin.ts +81 -0
- package/src/presets/platforms/rn.ts +113 -0
- package/src/util/appConfig.ts +29 -0
- package/src/util/createPage.ts +132 -0
- package/src/util/defineConfig.ts +22 -0
- package/src/util/index.ts +127 -0
- package/src/util/types.ts +81 -0
- package/templates/default/.husky/commit-msg +4 -0
- package/templates/default/_editorconfig +12 -0
- package/templates/default/_env.development +2 -0
- package/templates/default/_env.production +1 -0
- package/templates/default/_env.test +1 -0
- package/templates/default/_eslintrc +11 -0
- package/templates/default/_gitignore +8 -0
- package/templates/default/babel.config.js +14 -0
- package/templates/default/commitlint.config.mjs +1 -0
- package/templates/default/config/dev.js +10 -0
- package/templates/default/config/index.js +106 -0
- package/templates/default/config/prod.js +46 -0
- package/templates/default/package.json.tmpl +158 -0
- package/templates/default/project.config.json +15 -0
- package/templates/default/src/app.config.js +11 -0
- package/templates/default/src/app.css +0 -0
- package/templates/default/src/app.js +44 -0
- package/templates/default/src/index.html +17 -0
- package/templates/default/src/pages/index/index.config.js +3 -0
- package/templates/default/src/pages/index/index.css +0 -0
- package/templates/default/src/pages/index/index.jsx +15 -0
- package/templates/default/src/pages/index/index.vue +23 -0
- package/templates/default/stylelint.config.mjs +4 -0
- package/templates/default/template_creator.js +77 -0
- package/templates/default/tsconfig.json +32 -0
- package/templates/default/types/global.d.ts +35 -0
- package/templates/default/types/solid.d.ts +12 -0
- package/templates/default/types/vue.d.ts +10 -0
- package/templates/global-config/index.json +4 -0
- package/templates/global-config/package.json +5 -0
- package/templates/plugin-compile/README.md +32 -0
- package/templates/plugin-compile/index.js +3 -0
- package/templates/plugin-compile/package.json.tmpl +43 -0
- package/templates/plugin-compile/src/index.ts +153 -0
- 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
|
+
}
|