UNPKG

3.4 kBPlain TextView Raw
1import * as path from 'path'
2
3import * as minimist from 'minimist'
4import { Kernel } from '@tarojs/service'
5
6import build from './commands/build'
7import init from './commands/init'
8import create from './commands/create'
9import convert from './commands/convert'
10import customCommand from './commands/customCommand'
11import { getPkgVersion } from './util'
12
13export default class CLI {
14 appPath: string
15 constructor (appPath) {
16 this.appPath = appPath || process.cwd()
17 }
18
19 run () {
20 this.parseArgs()
21 }
22
23 parseArgs () {
24 const args = minimist(process.argv.slice(2), {
25 alias: {
26 version: ['v'],
27 help: ['h']
28 },
29 boolean: ['version', 'help']
30 })
31 const _ = args._
32 const command = _[0]
33 if (command) {
34 const kernel = new Kernel({
35 appPath: this.appPath,
36 presets: [
37 path.resolve(__dirname, '.', 'presets', 'index.js')
38 ]
39 })
40 switch (command) {
41 case 'build': {
42 build(kernel, {
43 platform: args.type,
44 isWatch: !!args.watch,
45 port: args.port,
46 env: args.env,
47 release: args.release,
48 ui: args.ui,
49 uiIndex: args.uiIndex,
50 page: args.page,
51 component: args.component,
52 plugin: args.plugin,
53 isHelp: args.h
54 })
55 break
56 }
57 case 'init': {
58 const projectName = _[1] || args.name
59 init(kernel, {
60 appPath: this.appPath,
61 projectName,
62 typescript: args.typescript,
63 templateSource: args['template-source'],
64 clone: !!args.clone,
65 template: args.template,
66 css: args.css,
67 isHelp: args.h
68 })
69 break
70 }
71 case 'create': {
72 const type = _[1] || 'page'
73 const name = _[2] || args.name
74 create(kernel, {
75 appPath: this.appPath,
76 type,
77 name,
78 description: args.description,
79 isHelp: args.h
80 })
81 break
82 }
83 case 'convert': {
84 convert(kernel, {
85 appPath: this.appPath,
86 isHelp: args.h
87 })
88 break
89 }
90 default:
91 customCommand(command, kernel, args)
92 break
93 }
94 } else {
95 if (args.h) {
96 console.log('Usage: taro <command> [options]')
97 console.log()
98 console.log('Options:')
99 console.log(' -v, --version output the version number')
100 console.log(' -h, --help output usage information')
101 console.log()
102 console.log('Commands:')
103 console.log(' init [projectName] Init a project with default templete')
104 console.log(' config <cmd> Taro config')
105 console.log(' create Create page for project')
106 console.log(' build Build a project with options')
107 console.log(' update Update packages of taro')
108 console.log(' info Diagnostics Taro env info')
109 console.log(' doctor Diagnose taro project')
110 console.log(' inspect Inspect the webpack config')
111 console.log(' help [cmd] display help for [cmd]')
112 } else if (args.v) {
113 console.log(getPkgVersion())
114 }
115 }
116 }
117}