Skip to content

Commit

Permalink
fix(cli): 修复 create 命令,fix #6852
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Jul 13, 2020
1 parent ad897eb commit fea55af
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 105 deletions.
42 changes: 0 additions & 42 deletions packages/taro-cli/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
13 changes: 0 additions & 13 deletions packages/taro-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 0 additions & 26 deletions packages/taro-cli/src/commands/create.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/taro-cli/src/create/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/src/create/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 17 additions & 15 deletions packages/taro-cli/src/presets/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
10 changes: 6 additions & 4 deletions packages/taro-cli/templates/default/src/pages/index/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
<%}-%>
<%}-%>
}
</script>
14 changes: 11 additions & 3 deletions packages/taro-cli/templates/default/template_creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit fea55af

Please sign in to comment.