UNPKG

7.1 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/*
4 * Copyright (C) 2017, hapjs.org. All rights reserved.
5 */
6
7const program = require('commander')
8const chalk = require('chalk')
9const semver = require('semver')
10const { colorconsole } = require('@hap-toolkit/shared-utils')
11
12// 最低支持的node版本
13const NODE_MINIMUM_VERSION = '8.0.0'
14
15function checkVersion() {
16 const currentVersion = process.versions.node
17
18 // 若当前版本小于支持版本
19 if (semver.lt(currentVersion, NODE_MINIMUM_VERSION)) {
20 colorconsole.warn(
21 `检测到当前 NodeJS 版本过低,请升级到 NodeJS 版本 ${NODE_MINIMUM_VERSION} 以上`
22 )
23 }
24}
25
26checkVersion()
27
28program.version(require('../package').version, '-v, --version').usage('<command> [options]')
29
30program
31 .command('init <app-name>')
32 .option('--dsl <name>', 'init project by specific dsl template, eg: vue')
33 .description('create a new project.')
34 .action((name, options) => {
35 const generate = require('../lib/commands/init')
36 generate(name, options)
37 })
38
39program
40 .command('build')
41 .description('build the project')
42 .option('--debug', 'use debug sign')
43 .option('--stats', 'analyse time and size of webpack output files')
44 .option('--devtool <value>', 'source map config')
45 .option('--disable-subpackages', 'disable subpackages')
46 .option('--disable-stream-pack', 'disable stream pack')
47 .option('--disable-script-v8-v65', 'disable compile script match with v8 version 6.5')
48 .option('--optimize-desc-meta', 'optimize desc meta')
49 .option('--optimize-css-attr', 'optimize css attr')
50 .option('--optimize-template-attr', 'optimize template attr')
51 .option('--optimize-style-page-level', 'optimize style in page')
52 .option('--optimize-style-app-level', 'optimize style in app ')
53 .option('--enable-lazy-component', 'lazy load component')
54 .option('--optimize-unused-resource', 'remove unused resource')
55 .option('--include-dsl-from-lib', 'bundle dsl to rpk')
56 .option('--match-sourcemap', 'match sourcemap')
57 .action(options => {
58 const { compile } = require('../lib/commands/compile')
59 compile('native', 'dev', false, options)
60 })
61
62program
63 .command('debug', { noHelp: true })
64 .description('debug the project')
65 .option('--open-browser', 'open QR code page in default browser')
66 .action(options => {
67 const { launchServer } = require('@hap-toolkit/server')
68 const { openBrowser } = options
69 launchServer({
70 modules: ['debugger'],
71 port: 8081,
72 openBrowser
73 })
74 })
75
76program
77 .command('server')
78 .description('open server for project')
79 .option('--port <port>', 'specified port')
80 .option('--watch', 'recompile project while file changes')
81 .option('--clear-records', 'clear device records')
82 .option('--disable-adb', 'disable adb debug')
83 .option('--chrome-path <chrome-path>', 'support for a user specified chrome path')
84 .option('--open-browser', 'open QR code page in default browser')
85 .option('--include-dsl-from-lib', 'bundle dsl to rpk')
86 .action(options => {
87 const { launchServer } = require('@hap-toolkit/server')
88 const { compile } = require('../lib/commands/compile')
89 const { port, watch, clearRecords, chromePath, disableAdb, openBrowser } = options
90 launchServer({
91 port,
92 watch,
93 clearRecords,
94 chromePath,
95 disableADB: disableAdb,
96 openBrowser
97 })
98 if (options.watch) {
99 compile('native', 'dev', true, options)
100 }
101 })
102
103program
104 .command('watch')
105 .description('recompile project while file changes')
106 .option('--devtool <value>', 'source map config')
107 .option('--disable-subpackages', 'disable subpackages')
108 .option('--disable-stream-pack', 'disable stream pack')
109 .option('--disable-script-v8-v65', 'disable compile script match with v8 version 6.5')
110 .option('--include-dsl-from-lib', 'bundle dsl to rpk')
111 .option('--match-sourcemap', 'match sourcemap')
112 .action(options => {
113 const { compile } = require('../lib/commands/compile')
114 compile('native', 'dev', true, options)
115 })
116
117program
118 .command('release')
119 .description('release the project')
120 .option('--debug', 'use debug sign')
121 .option('--stats', 'analyse time and size of webpack output files')
122 .option('--devtool <value>', 'source map config')
123 .option('--disable-subpackages', 'disable subpackages')
124 .option('--disable-stream-pack', 'disable stream pack')
125 .option('--disable-script-v8-v65', 'disable compile script match with v8 version 6.5')
126 .option('--optimize-desc-meta', 'optimize desc meta')
127 .option('--optimize-css-attr', 'optimize css attr')
128 .option('--optimize-template-attr', 'optimize template attr')
129 .option('--optimize-style-page-level', 'optimize style in page')
130 .option('--optimize-style-app-level', 'optimize style in app ')
131 .option('--enable-lazy-component', 'lazy load component')
132 .option('--optimize-unused-resource', 'remove unused resource')
133 .option('--include-dsl-from-lib', 'bundle dsl to rpk')
134 .option('--match-sourcemap', 'match sourcemap')
135 .action(options => {
136 const { compile } = require('../lib/commands/compile')
137 compile('native', 'prod', false, options)
138 })
139
140program
141 .command('preview <target>')
142 .description('preview app in your browser')
143 .option('--port <port>', 'specified port', 8989)
144 .action((target, options) => {
145 const preview = require('../lib/commands/preview')
146 preview(target, options)
147 })
148
149program
150 .command('postinstall', { noHelp: true })
151 .description('Transpiling async/await for nodejs<7.6.x, deprecated.')
152 .action(() => {
153 colorconsole.warn('Deprecated command!')
154 })
155
156// TODO
157// Since we properly have all dependencies included,
158// and if we make {babel, eslint}-configuration built-in,
159// we won't need this `update` command anymore.
160program
161 .command('update')
162 .description('update tools for project')
163 .option('--force', 'force update tools for project')
164 .option('--update-deps', 'update dependencies directly', { noHelp: true })
165 .action(options => {
166 const update = require('../lib/commands/update')
167 colorconsole.warn('hap-toolkit>=0.1.0 不再需要运行此命令\n')
168 update(options)
169 })
170
171program
172 .command('report', { noHelp: true })
173 .description('collect system information and create report.log')
174 .action(() => {
175 const report = require('../lib/commands/report')
176 report()
177 })
178
179program
180 .command('view <rpk-path>')
181 .description('run server to view rpk')
182 .option('--port <port>', 'specified port', 8000)
183 .option('--open-browser', 'open QR code page in default browser')
184 .action((rpkPath, options) => {
185 const { launchServer } = require('@hap-toolkit/server')
186 const { port, openBrowser } = options
187 launchServer({
188 port,
189 openBrowser,
190 rpkPath
191 })
192 })
193
194program.on('--help', () => {
195 console.log()
196 console.log(`Run ${chalk.cyan(`hap <command> --help`)} for detailed usage of given command.`)
197 console.log()
198})
199
200// 更改 NodeJS 10.1.0 上的 "fs.promise is Experiment" 日志输出位置
201require('fs-extra')
202setTimeout(() => {
203 program.parse(process.argv)
204
205 if (!process.argv.slice(2).length) {
206 program.outputHelp()
207 }
208}, 0)