UNPKG

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