Skip to content

Commit

Permalink
feat(cli): 支持更改 CLI 展示的模板名称
Browse files Browse the repository at this point in the history
  • Loading branch information
koppthe committed Jun 18, 2024
1 parent 9afb28a commit 2fc0933
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 9 additions & 6 deletions packages/taro-cli/src/create/fetchTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TEMPLATE_CREATOR } from './constants'

export interface ITemplates {
name: string
value: string
platforms?: string | string[]
desc?: string
compiler?: string[]
Expand Down Expand Up @@ -108,11 +109,12 @@ export default function fetchTemplate (templateSource: string, templateRootPath:
const res: ITemplates[] = files.map(name => {
const creatorFile = path.join(templateRootPath, name, TEMPLATE_CREATOR)

if (!fs.existsSync(creatorFile)) return { name }
const { platforms = '', desc = '', compiler } = require(creatorFile)
if (!fs.existsSync(creatorFile)) return { name, value: name }
const { name: displayName, platforms = '', desc = '', compiler } = require(creatorFile)

return {
name,
name: displayName || name,
value: name,
platforms,
compiler,
desc
Expand All @@ -124,15 +126,16 @@ export default function fetchTemplate (templateSource: string, templateRootPath:
await fs.move(templateFolder, path.join(templateRootPath, name), { overwrite: true })
await fs.remove(tempPath)

let res: ITemplates = { name, desc: type === 'url' ? templateSource : '' }
let res: ITemplates = { name, value: name, desc: type === 'url' ? templateSource : '' }

const creatorFile = path.join(templateRootPath, name, TEMPLATE_CREATOR)

if (fs.existsSync(creatorFile)) {
const { platforms = '', desc = '', compiler } = require(creatorFile)
const { name: displayName, platforms = '', desc = '', compiler } = require(creatorFile)

res = {
name,
name: displayName || name,
value: name,
platforms,
compiler,
desc: desc || templateSource
Expand Down
6 changes: 4 additions & 2 deletions packages/taro-cli/src/create/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export default class Project extends Creator {
askTemplate: AskMethods = function (conf, prompts, list = []) {
const choices = list.map(item => ({
name: item.desc ? `${item.name}${item.desc})` : item.name,
value: item.name
value: item.value || item.name
}))

if (!conf.hideDefaultTemplate) {
Expand Down Expand Up @@ -388,6 +388,7 @@ export default class Project extends Creator {

async fetchTemplates (answers: IProjectConf): Promise<ITemplates[]> {
const { templateSource, framework, compiler } = answers
this.conf.framework = this.conf.framework || framework || ''
this.conf.templateSource = this.conf.templateSource || templateSource

// 使用默认模版
Expand All @@ -402,7 +403,8 @@ export default class Project extends Creator {
const templateChoices = await fetchTemplate(this.conf.templateSource, this.templatePath(''), isClone)

const filterFramework = (_framework) => {
const current = framework.toLowerCase()
const current = this.conf.framework?.toLowerCase()

if (typeof _framework === 'string' && _framework) {
return current === _framework.toLowerCase()
} else if (isArray(_framework)) {
Expand Down

0 comments on commit 2fc0933

Please sign in to comment.