UNPKG

4.24 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 config from './commands/config'
10import info from './commands/info'
11import doctor from './commands/doctor'
12import convert from './commands/convert'
13import update from './commands/update'
14import customCommand from './commands/customCommand'
15import { getPkgVersion } from './util'
16
17export default class CLI {
18 appPath: string
19 constructor (appPath) {
20 this.appPath = appPath || process.cwd()
21 }
22
23 run () {
24 this.parseArgs()
25 }
26
27 parseArgs () {
28 const args = minimist(process.argv.slice(2), {
29 alias: {
30 version: ['v'],
31 help: ['h']
32 },
33 boolean: ['version', 'help']
34 })
35 const _ = args._
36 const command = _[0]
37 if (command) {
38 const kernel = new Kernel({
39 appPath: this.appPath,
40 presets: [
41 path.resolve(__dirname, '.', 'presets', 'index.js')
42 ]
43 })
44 switch (command) {
45 case 'build':
46 build(kernel, {
47 platform: args.type,
48 isWatch: !!args.watch,
49 port: args.port,
50 release: args.release,
51 ui: args.ui,
52 uiIndex: args.uiIndex,
53 page: args.page,
54 component: args.component,
55 plugin: args.plugin,
56 isHelp: args.h
57 })
58 break
59 case 'init':
60 const projectName = _[1]
61 init(kernel, {
62 appPath: this.appPath,
63 projectName,
64 typescript: args.typescript,
65 templateSource: args['template-source'],
66 clone: !!args.clone,
67 template: args.template,
68 css: args.css,
69 isHelp: args.h
70 })
71 break
72 case 'create':
73 const type = _[1] || 'page'
74 const name = _[2] || args.name
75 create(kernel, {
76 appPath: this.appPath,
77 type,
78 name,
79 description: args.description,
80 isHelp: args.h
81 })
82 break
83 case 'config':
84 const cmd = _[1]
85 const key = _[2]
86 const value = _[3]
87 config(kernel, {
88 cmd,
89 key,
90 value,
91 json: !!args.json,
92 isHelp: args.h
93 })
94 break
95 case 'info':
96 const rn = _[1]
97 info(kernel, {
98 appPath: this.appPath,
99 rn: !!rn,
100 isHelp: args.h
101 })
102 break
103 case 'doctor':
104 doctor(kernel, {
105 appPath: this.appPath,
106 isHelp: args.h
107 })
108 break
109 case 'convert':
110 convert(kernel, {
111 appPath: this.appPath,
112 isHelp: args.h
113 })
114 break
115 case 'update':
116 const updateType = _[1]
117 const version = _[2]
118 update(kernel, {
119 appPath: this.appPath,
120 updateType,
121 version,
122 isHelp: args.h
123 })
124 break
125 default:
126 customCommand(command, kernel, args)
127 break
128 }
129 } else {
130 if (args.h) {
131 console.log('Usage: taro <command> [options]')
132 console.log()
133 console.log('Options:')
134 console.log(' -v, --version output the version number')
135 console.log(' -h, --help output usage information')
136 console.log()
137 console.log('Commands:')
138 console.log(' init [projectName] Init a project with default templete')
139 console.log(' config <cmd> Taro config')
140 console.log(' create Create page for project')
141 console.log(' build Build a project with options')
142 console.log(' update Update packages of taro')
143 console.log(' info Diagnostics Taro env info')
144 console.log(' doctor Diagnose taro project')
145 console.log(' help [cmd] display help for [cmd]')
146 } else if (args.v) {
147 console.log(getPkgVersion())
148 }
149 }
150 }
151}