From fea55af3d8b61f05ea765e504cb0f270691d8f44 Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Tue, 7 Jul 2020 14:40:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=E4=BF=AE=E5=A4=8D=20create=20?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=EF=BC=8Cfix=20#6852?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/__tests__/cli.spec.ts | 42 ------------------- packages/taro-cli/src/cli.ts | 13 ------ packages/taro-cli/src/commands/create.ts | 26 ------------ packages/taro-cli/src/create/page.ts | 3 ++ packages/taro-cli/src/create/project.ts | 2 +- .../taro-cli/src/presets/commands/create.ts | 32 +++++++------- .../default/src/pages/index/index.jsx | 2 +- .../default/src/pages/index/index.vue | 10 +++-- .../templates/default/template_creator.js | 14 +++++-- 9 files changed, 39 insertions(+), 105 deletions(-) delete mode 100644 packages/taro-cli/src/commands/create.ts diff --git a/packages/taro-cli/__tests__/cli.spec.ts b/packages/taro-cli/__tests__/cli.spec.ts index cc5d81a84a9a..b6e75c44b15e 100644 --- a/packages/taro-cli/__tests__/cli.spec.ts +++ b/packages/taro-cli/__tests__/cli.spec.ts @@ -143,48 +143,6 @@ describe('inspect', () => { }) }) - describe('create', () => { - const baseOpts = { - appPath: APP_PATH, - type: undefined, - name: undefined, - description: undefined, - isHelp: false - } - - it('should make configs', () => { - const type = 'component' - const name = 'detail' - const description = 'chore' - setProcessArgv('taro create component detail --description=chore') - cli.run() - const ins = MockedKernel.mock.instances[0] - expect(ins.run).toHaveBeenCalledWith({ - name: 'create', - opts: Object.assign({}, baseOpts, { - type, - name, - description - }) - }) - }) - - it('should set type and name', () => { - const type = 'page' - const name = 'my' - setProcessArgv('taro create --name my') - cli.run() - const ins = MockedKernel.mock.instances[0] - expect(ins.run).toHaveBeenCalledWith({ - name: 'create', - opts: Object.assign({}, baseOpts, { - type, - name - }) - }) - }) - }) - describe('convert', () => { it('should make configs', () => { setProcessArgv('taro convert') diff --git a/packages/taro-cli/src/cli.ts b/packages/taro-cli/src/cli.ts index bbd908dfb028..b9ace3d4ab23 100644 --- a/packages/taro-cli/src/cli.ts +++ b/packages/taro-cli/src/cli.ts @@ -5,7 +5,6 @@ import { Kernel } from '@tarojs/service' import build from './commands/build' import init from './commands/init' -import create from './commands/create' import convert from './commands/convert' import customCommand from './commands/customCommand' import { getPkgVersion } from './util' @@ -68,18 +67,6 @@ export default class CLI { }) break } - case 'create': { - const type = _[1] || 'page' - const name = _[2] || args.name - create(kernel, { - appPath: this.appPath, - type, - name, - description: args.description, - isHelp: args.h - }) - break - } case 'convert': { convert(kernel, { appPath: this.appPath, diff --git a/packages/taro-cli/src/commands/create.ts b/packages/taro-cli/src/commands/create.ts deleted file mode 100644 index 05f9c047da1e..000000000000 --- a/packages/taro-cli/src/commands/create.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Kernel } from '@tarojs/service' - -export default function create (kernel: Kernel, { - appPath, - type, - name, - description, - isHelp -}: { - appPath: string, - type: string, - name: string, - description?: string, - isHelp?: boolean -}) { - kernel.run({ - name: 'create', - opts: { - appPath, - type, - name, - description, - isHelp - } - }) -} diff --git a/packages/taro-cli/src/create/page.ts b/packages/taro-cli/src/create/page.ts index d98e66b084f9..0a187a51bbb6 100644 --- a/packages/taro-cli/src/create/page.ts +++ b/packages/taro-cli/src/create/page.ts @@ -54,8 +54,11 @@ export default class Page extends Creator { css: 'none', typescript: false } + + // set template name templateInfo.template = templateInfo.name delete templateInfo.name + this.conf = Object.assign(this.conf, templateInfo) } diff --git a/packages/taro-cli/src/create/project.ts b/packages/taro-cli/src/create/project.ts index 689a35d78551..7c8580545168 100644 --- a/packages/taro-cli/src/create/project.ts +++ b/packages/taro-cli/src/create/project.ts @@ -129,7 +129,7 @@ export default class Project extends Creator { const newTemplateChoices: ITemplates[] = templateChoices .filter(templateChoice => { const { platforms } = templateChoice - if (typeof platforms === 'string') { + if (typeof platforms === 'string' && platforms) { return answers.framework === templateChoice.platforms } else if (isArray(platforms)) { return templateChoice.platforms?.includes(answers.framework) diff --git a/packages/taro-cli/src/presets/commands/create.ts b/packages/taro-cli/src/presets/commands/create.ts index 1605b989663c..2dbe7295e687 100644 --- a/packages/taro-cli/src/presets/commands/create.ts +++ b/packages/taro-cli/src/presets/commands/create.ts @@ -7,27 +7,29 @@ export default (ctx: IPluginContext) => { '--name [name]': '名称', '--description [description]': '介绍' }, - fn () { - const { - type, - name, - description - } = ctx.runOpts + synopsisList: [ + 'taro create page', + 'taro create --name=page --description=desc' + ], + fn ({ _, options }) { + const name = _[1] || options.name + const description = options.description || '' const { chalk } = ctx.helper const { appPath } = ctx.paths + if (typeof name !== 'string') { return console.log(chalk.red('请输入需要创建的页面名称')) } - if (type === 'page') { - const Page = require('../../create/page').default - const page = new Page({ - pageName: name, - projectDir: appPath, - description - }) - page.create() - } + const Page = require('../../create/page').default + const page = new Page({ + pageName: name, + projectDir: appPath, + description, + framework: ctx.initialConfig.framework + }) + + page.create() } }) } diff --git a/packages/taro-cli/templates/default/src/pages/index/index.jsx b/packages/taro-cli/templates/default/src/pages/index/index.jsx index 059f2566f31f..7dbc97152473 100644 --- a/packages/taro-cli/templates/default/src/pages/index/index.jsx +++ b/packages/taro-cli/templates/default/src/pages/index/index.jsx @@ -4,7 +4,7 @@ import React, { Component } from 'react' import Nerv, { Component } from 'nervjs' <%}-%> import { View, Text } from '@tarojs/components' -import './<%= pageName %>.<%= cssExt %>' +import './index.<%= cssExt %>' export default class <%= _.capitalize(pageName) %> extends Component { diff --git a/packages/taro-cli/templates/default/src/pages/index/index.vue b/packages/taro-cli/templates/default/src/pages/index/index.vue index 7940b04ca257..fe1c9de93bb4 100644 --- a/packages/taro-cli/templates/default/src/pages/index/index.vue +++ b/packages/taro-cli/templates/default/src/pages/index/index.vue @@ -8,21 +8,23 @@ <%if (framework === 'vue3') {-%> import { ref } from 'vue' <%}-%> +import './index.<%= cssExt %>' + export default { - <%if (framework === 'vue') {-%> +<%if (framework === 'vue') {-%> data () { return { msg: 'Hello world!' } } - <%}-%> - <%if (framework === 'vue3') {-%> +<%}-%> +<%if (framework === 'vue3') {-%> setup () { const msg = ref('Hello world') return { msg } } - <%}-%> +<%}-%> } diff --git a/packages/taro-cli/templates/default/template_creator.js b/packages/taro-cli/templates/default/template_creator.js index a468327cd98a..d0987b34c144 100644 --- a/packages/taro-cli/templates/default/template_creator.js +++ b/packages/taro-cli/templates/default/template_creator.js @@ -6,16 +6,24 @@ const handler = { '/global.d.ts': createWhenTs, '/tsconfig.json': createWhenTs, '/src/pages/index/index.jsx' ({ pageName }) { - return { setPageName: `/src/pages/${pageName}/${pageName}.jsx` } + return { setPageName: `/src/pages/${pageName}/index.jsx` } }, '/src/pages/index/index.css' ({ pageName }) { - return { setPageName: `/src/pages/${pageName}/${pageName}.css` } + return { setPageName: `/src/pages/${pageName}/index.css` } + }, + '/src/pages/index/index.vue' ({ pageName }) { + return { setPageName: `/src/pages/${pageName}/index.vue` } + }, + '/src/pages/index/index.config.js' ({ pageName }) { + return { setPageName: `/src/pages/${pageName}/index.config.js` } } } const basePageFiles = [ '/src/pages/index/index.jsx', - '/src/pages/index/index.css' + '/src/pages/index/index.vue', + '/src/pages/index/index.css', + '/src/pages/index/index.config.js' ] module.exports = {