Skip to content

Commit

Permalink
fix(cli): 创建页面时模板不存在则先拉取模板,fix #4106
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Aug 13, 2019
1 parent a6e31b5 commit c79ef23
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
8 changes: 3 additions & 5 deletions packages/taro-cli/src/create/fetchTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import * as ora from 'ora'
import * as AdmZip from 'adm-zip'
import * as download from 'download-git-repo'
import * as request from 'request'
import Project from './project'
import { TemplateSourceType, readDirWithFileTypes } from '../util'
import { getTemplateSourceType, readDirWithFileTypes } from '../util'

const TEMP_DOWNLOAD_FLODER = 'taro-temp'

export default function fetchTemplate (creater: Project, type: TemplateSourceType): Promise<any> {
const { templateSource } = creater.conf
const templateRootPath = creater.templatePath('')
export default function fetchTemplate (templateSource: string, templateRootPath: string): Promise<any> {
const type = getTemplateSourceType(templateSource)
const tempPath = path.join(templateRootPath, TEMP_DOWNLOAD_FLODER)
let name: string

Expand Down
2 changes: 2 additions & 0 deletions packages/taro-cli/src/create/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export async function createPage (
// path
const templatePath = creater.templatePath(template)

if (!fs.existsSync(templatePath)) return console.log(chalk.red(`创建页面错误:找不到模板${templatePath}`))

// 引入模板编写者的自定义逻辑
const handlerPath = path.join(templatePath, TEMPLATE_CREATOR)
const basePageFiles = fs.existsSync(handlerPath) ? require(handlerPath).basePageFiles : []
Expand Down
31 changes: 30 additions & 1 deletion packages/taro-cli/src/create/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as fs from 'fs-extra'
import chalk from 'chalk'
import Creator from './creator'
import { createPage } from './init'
import fetchTemplate from './fetchTemplate'
import { DEFAULT_TEMPLATE_SRC, TARO_CONFIG_FLODER, TARO_BASE_CONFIG } from '../util/constants'
import { getUserHomeDir } from '../util'


export interface IPageConf {
Expand Down Expand Up @@ -54,10 +57,36 @@ export default class Page extends Creator {
this.conf = Object.assign(this.conf, templateInfo)
}

create () {
async fetchTemplates (): Promise<string[]> {
const homedir = getUserHomeDir()
let templateSource = DEFAULT_TEMPLATE_SRC
if (!homedir) chalk.yellow('找不到用户根目录,使用默认模版源!')

const taroConfigPath = path.join(homedir, TARO_CONFIG_FLODER)
const taroConfig = path.join(taroConfigPath, TARO_BASE_CONFIG)

if (fs.existsSync(taroConfig)) {
const config = await fs.readJSON(taroConfig)
templateSource = config && config.templateSource ? config.templateSource : DEFAULT_TEMPLATE_SRC
} else {
await fs.createFile(taroConfig)
await fs.writeJSON(taroConfig, { templateSource: DEFAULT_TEMPLATE_SRC })
templateSource = DEFAULT_TEMPLATE_SRC
}

// 从模板源下载模板
await fetchTemplate(templateSource, this.templatePath(''))
}

async create () {
const date = new Date()
this.getTemplateInfo()
this.conf.date = `${date.getFullYear()}-${(date.getMonth() + 1)}-${date.getDate()}`

if (!fs.existsSync(this.templatePath(this.conf.template))) {
await this.fetchTemplates()
}

this.write()
}

Expand Down
8 changes: 2 additions & 6 deletions packages/taro-cli/src/create/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import { createApp } from './init'
import fetchTemplate from './fetchTemplate'
import Creator from './creator'
import CONFIG from '../config'
import { DEFAULT_TEMPLATE_SRC, TARO_CONFIG_FLODER, TARO_BASE_CONFIG } from '../util/constants'
import { getUserHomeDir, getTemplateSourceType } from '../util'

const TARO_CONFIG_FLODER = '.taro'
const TARO_BASE_CONFIG = 'index.json'
const DEFAULT_TEMPLATE_SRC = 'github:NervJS/taro-project-templates'

export interface IProjectConf {
projectName: string,
projectDir: string,
Expand Down Expand Up @@ -97,8 +94,7 @@ export default class Project extends Creator {
}

// 从模板源下载模板
const templateSourceType = getTemplateSourceType(conf.templateSource)
return fetchTemplate(this, templateSourceType)
return fetchTemplate(this.conf.templateSource, this.templatePath(''))
}

ask (templateChoices: string[]) {
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-cli/src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,7 @@ export const taroJsMobxCommon = '@tarojs/mobx-common'

export const DEVICE_RATIO_NAME = 'deviceRatio'
export const isWindows = os.platform() === 'win32'

export const DEFAULT_TEMPLATE_SRC = 'github:NervJS/taro-project-templates'
export const TARO_CONFIG_FLODER = '.taro'
export const TARO_BASE_CONFIG = 'index.json'

0 comments on commit c79ef23

Please sign in to comment.