From c79ef23b3ff702fe93dbddf421a27c486b1f8c39 Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Tue, 13 Aug 2019 11:34:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=E5=88=9B=E5=BB=BA=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E6=97=B6=E6=A8=A1=E6=9D=BF=E4=B8=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=88=99=E5=85=88=E6=8B=89=E5=8F=96=E6=A8=A1=E6=9D=BF=EF=BC=8C?= =?UTF-8?q?fix=20#4106?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/src/create/fetchTemplate.ts | 8 ++--- packages/taro-cli/src/create/init.ts | 2 ++ packages/taro-cli/src/create/page.ts | 31 ++++++++++++++++++- packages/taro-cli/src/create/project.ts | 8 ++--- packages/taro-cli/src/util/constants.ts | 4 +++ 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/packages/taro-cli/src/create/fetchTemplate.ts b/packages/taro-cli/src/create/fetchTemplate.ts index f73ee6fcb283..eaa8cde2b613 100644 --- a/packages/taro-cli/src/create/fetchTemplate.ts +++ b/packages/taro-cli/src/create/fetchTemplate.ts @@ -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 { - const { templateSource } = creater.conf - const templateRootPath = creater.templatePath('') +export default function fetchTemplate (templateSource: string, templateRootPath: string): Promise { + const type = getTemplateSourceType(templateSource) const tempPath = path.join(templateRootPath, TEMP_DOWNLOAD_FLODER) let name: string diff --git a/packages/taro-cli/src/create/init.ts b/packages/taro-cli/src/create/init.ts index b39b85842047..9763624afeb9 100644 --- a/packages/taro-cli/src/create/init.ts +++ b/packages/taro-cli/src/create/init.ts @@ -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 : [] diff --git a/packages/taro-cli/src/create/page.ts b/packages/taro-cli/src/create/page.ts index 35df63918bd2..161759fad070 100644 --- a/packages/taro-cli/src/create/page.ts +++ b/packages/taro-cli/src/create/page.ts @@ -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 { @@ -54,10 +57,36 @@ export default class Page extends Creator { this.conf = Object.assign(this.conf, templateInfo) } - create () { + async fetchTemplates (): Promise { + 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() } diff --git a/packages/taro-cli/src/create/project.ts b/packages/taro-cli/src/create/project.ts index f75a8c1f0c6b..7f6982b998f9 100644 --- a/packages/taro-cli/src/create/project.ts +++ b/packages/taro-cli/src/create/project.ts @@ -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, @@ -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[]) { diff --git a/packages/taro-cli/src/util/constants.ts b/packages/taro-cli/src/util/constants.ts index 123a086a3316..0275ee5e5b56 100644 --- a/packages/taro-cli/src/util/constants.ts +++ b/packages/taro-cli/src/util/constants.ts @@ -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'