@zeppos/zeus-cli 1.0.22 → 1.1.0-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +1 -1
  2. package/api/index.js +250 -0
  3. package/bin/cli.js +2 -0
  4. package/config/device.js +3 -2
  5. package/config/project.js +27 -11
  6. package/index.js +2 -0
  7. package/modules/bridge.js +205 -0
  8. package/modules/build.js +130 -76
  9. package/modules/config.js +21 -19
  10. package/modules/create/index.js +18 -11
  11. package/modules/create/local-app.js +4 -0
  12. package/modules/help.js +2 -0
  13. package/modules/login.js +78 -145
  14. package/modules/run.js +28 -64
  15. package/modules/simulator.js +8 -1
  16. package/modules/status.js +86 -0
  17. package/modules/storage.js +88 -4
  18. package/modules/websocket.js +105 -0
  19. package/package.json +9 -2
  20. package/public/template/empty/app.js +2 -24
  21. package/public/template/hello-world/app.js +2 -24
  22. package/public/template/hello-world/app.json +1 -1
  23. package/public/template/hello-world/page/gtr3/home/index.page.js +6 -4
  24. package/public/template/hello-world/page/gtr3/home/index.style.js +11 -28
  25. package/public/template/hello-world/page/gtr3-pro/home/index.page.js +6 -4
  26. package/public/template/hello-world/page/gtr3-pro/home/index.style.js +11 -28
  27. package/public/template/hello-world/page/gts3/home/index.page.js +7 -4
  28. package/public/template/hello-world/page/gts3/home/index.style.js +11 -28
  29. package/public/template/jsconfig.json +14 -0
  30. package/public/template/package.json +14 -0
  31. package/public/template/timer/app.js +1 -4
  32. package/public/template/timer/app.json +1 -1
  33. package/public/template/timer/watchface/round/index.js +2 -2
  34. package/utils/common.js +106 -0
  35. package/utils/hm_analytics.js +35 -0
package/README.md CHANGED
@@ -15,4 +15,4 @@ The Zeus CLI can help you install the development application to your device, wh
15
15
 
16
16
  ### Help
17
17
 
18
- For more help, please visit [Huami Open Platform](https://dev.huami.com/) or [ZeppOS Docs](https://docs.huami.com/)
18
+ For more help, please visit [Zepp Open Platform](https://dev.huami.com/) or [ZeppOS Docs](https://docs.zepp.com/)
package/api/index.js ADDED
@@ -0,0 +1,250 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
22
+ }) : function(o, v) {
23
+ o["default"] = v;
24
+ });
25
+ var __importStar = (this && this.__importStar) || function (mod) {
26
+ if (mod && mod.__esModule) return mod;
27
+ var result = {};
28
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29
+ __setModuleDefault(result, mod);
30
+ return result;
31
+ };
32
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
33
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34
+ return new (P || (P = Promise))(function (resolve, reject) {
35
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
36
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
37
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
38
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
39
+ });
40
+ };
41
+ var __generator = (this && this.__generator) || function (thisArg, body) {
42
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
43
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
44
+ function verb(n) { return function (v) { return step([n, v]); }; }
45
+ function step(op) {
46
+ if (f) throw new TypeError("Generator is already executing.");
47
+ while (_) try {
48
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
49
+ if (y = 0, t) op = [op[0] & 2, t.value];
50
+ switch (op[0]) {
51
+ case 0: case 1: t = op; break;
52
+ case 4: _.label++; return { value: op[1], done: false };
53
+ case 5: _.label++; y = op[1]; op = [0]; continue;
54
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
55
+ default:
56
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
57
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
58
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
59
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
60
+ if (t[2]) _.ops.pop();
61
+ _.trys.pop(); continue;
62
+ }
63
+ op = body.call(thisArg, _);
64
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
65
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
66
+ }
67
+ };
68
+ var __importDefault = (this && this.__importDefault) || function (mod) {
69
+ return (mod && mod.__esModule) ? mod : { "default": mod };
70
+ };
71
+ exports.__esModule = true;
72
+ exports.downloadPackage = exports.uploadPackage = exports.getConnectDevServerWebSocketCode = exports.getUserInfo = exports.subRegionalHost = void 0;
73
+ var axios_1 = __importDefault(require("axios"));
74
+ var qs_1 = __importDefault(require("qs"));
75
+ var project_1 = require("../config/project");
76
+ var logger = __importStar(require("../utils/logger"));
77
+ var storage_1 = require("../modules/storage");
78
+ // import { login } from '../modules/login'
79
+ // process.on('exit', (code) => {
80
+ // console.log(`About to exit with code: ${code}`);
81
+ // });
82
+ var _a = storage_1.instanceStorage.getStorage(), _b = project_1.loginStorageKey.accountToken, _c = _a[_b], apptoken = _c === void 0 ? '' : _c, _d = project_1.loginStorageKey.userid, _e = _a[_d], userid = _e === void 0 ? '' : _e, _f = project_1.loginStorageKey.cname, _g = _a[_f], cname = _g === void 0 ? '' : _g;
83
+ // 分区域名
84
+ exports.subRegionalHost = cname && cname.split(',').filter(function (h) { return h.indexOf('api-mifit') > -1; })[0];
85
+ var postForm = function (url, data) {
86
+ return (0, axios_1["default"])({
87
+ url: url,
88
+ method: 'POST',
89
+ headers: {
90
+ 'content-type': 'application/x-www-form-urlencoded'
91
+ },
92
+ data: qs_1["default"].stringify(data)
93
+ });
94
+ };
95
+ // 获取用户信息接口
96
+ var getUserInfo = function (url, token) { return __awaiter(void 0, void 0, void 0, function () {
97
+ var headers, userInfo, data, error_1;
98
+ return __generator(this, function (_a) {
99
+ switch (_a.label) {
100
+ case 0:
101
+ headers = {
102
+ apptoken: token,
103
+ 'Content-Type': 'application/json'
104
+ };
105
+ _a.label = 1;
106
+ case 1:
107
+ _a.trys.push([1, 3, , 4]);
108
+ return [4 /*yield*/, axios_1["default"].get(url, {
109
+ headers: headers
110
+ })];
111
+ case 2:
112
+ userInfo = _a.sent();
113
+ data = userInfo.data;
114
+ return [2 /*return*/, data];
115
+ case 3:
116
+ error_1 = _a.sent();
117
+ if (project_1.ACCOUNT_ENV !== 'production') {
118
+ logger.error("userInfoURL, ".concat(url));
119
+ logger.error("userInfoHeaders', ".concat(JSON.stringify(headers)));
120
+ }
121
+ logger.error("get nickName error");
122
+ return [3 /*break*/, 4];
123
+ case 4: return [2 /*return*/];
124
+ }
125
+ });
126
+ }); };
127
+ exports.getUserInfo = getUserInfo;
128
+ // 获取连接 developer server 的 websocket 连接
129
+ var getConnectDevServerWebSocketCode = function () { return __awaiter(void 0, void 0, void 0, function () {
130
+ var message, codeInfo, url, error_2, message, msg;
131
+ var _a, _b;
132
+ return __generator(this, function (_c) {
133
+ switch (_c.label) {
134
+ case 0:
135
+ if (!exports.subRegionalHost) {
136
+ message = project_1.ACCOUNT_ENV === 'production' ? "Login information is invalid,please login again with the command 'zeus login'" : "".concat(project_1.ACCOUNT_ENV, ":Login information is invalid,please login again with the command 'zeus login'.");
137
+ throw new Error(message);
138
+ }
139
+ _c.label = 1;
140
+ case 1:
141
+ _c.trys.push([1, 3, , 4]);
142
+ return [4 /*yield*/, axios_1["default"].get("https://".concat(exports.subRegionalHost, "/developerbridge/client/relay"), {
143
+ headers: {
144
+ apptoken: apptoken
145
+ }
146
+ })];
147
+ case 2:
148
+ codeInfo = _c.sent();
149
+ url = codeInfo.data.url;
150
+ return [2 /*return*/, url];
151
+ case 3:
152
+ error_2 = _c.sent();
153
+ message = (error_2 === null || error_2 === void 0 ? void 0 : error_2.response) && ((_a = error_2.response) === null || _a === void 0 ? void 0 : _a.data);
154
+ if ((message === null || message === void 0 ? void 0 : message.code) === 401 && ((_b = message === null || message === void 0 ? void 0 : message.data) === null || _b === void 0 ? void 0 : _b.code) === "0102") {
155
+ storage_1.globalStorage.deleteStorage('____login');
156
+ // await login() TODO 不中断命令登录
157
+ throw new Error("Invalid token,please login again with the command 'zeus login'.");
158
+ }
159
+ else {
160
+ msg = project_1.ACCOUNT_ENV === 'production' ? "GetConnectDevServerWebSocketCode error" : "".concat(project_1.ACCOUNT_ENV, ":getConnectDevServerWebSocketCode error ").concat(JSON.stringify(message));
161
+ project_1.____inner_global_variable.bridgeOptionalDevice = '';
162
+ project_1.____inner_global_variable.bridgeChoiredDevice = '';
163
+ throw new Error(msg);
164
+ }
165
+ return [3 /*break*/, 4];
166
+ case 4: return [2 /*return*/];
167
+ }
168
+ });
169
+ }); };
170
+ exports.getConnectDevServerWebSocketCode = getConnectDevServerWebSocketCode;
171
+ // 上传表盘/应用接口
172
+ var uploadPackage = function (form, length) { return __awaiter(void 0, void 0, void 0, function () {
173
+ var packageInfo, error_3;
174
+ return __generator(this, function (_a) {
175
+ switch (_a.label) {
176
+ case 0:
177
+ _a.trys.push([0, 2, , 3]);
178
+ return [4 /*yield*/, axios_1["default"].post("https://".concat(exports.subRegionalHost, "/custom/tools/app-dial/upload?userid=").concat(userid), form, {
179
+ headers: __assign(__assign({}, form.getHeaders()), { 'content-length': "".concat(length), apptoken: apptoken })
180
+ })];
181
+ case 1:
182
+ packageInfo = _a.sent();
183
+ return [2 /*return*/, packageInfo];
184
+ case 2:
185
+ error_3 = _a.sent();
186
+ logger.error("".concat(error_3));
187
+ return [3 /*break*/, 3];
188
+ case 3: return [2 /*return*/];
189
+ }
190
+ });
191
+ }); };
192
+ exports.uploadPackage = uploadPackage;
193
+ // 下载表盘/应用接口
194
+ var downloadPackage = function (code, deviceSource, appType) { return __awaiter(void 0, void 0, void 0, function () {
195
+ var host, packageInfo, error_4;
196
+ return __generator(this, function (_a) {
197
+ switch (_a.label) {
198
+ case 0:
199
+ _a.trys.push([0, 2, , 3]);
200
+ host = "https://".concat(exports.subRegionalHost, "/custom/tools/app-dial/download/").concat(code);
201
+ if (appType === 'app') {
202
+ return [2 /*return*/, host];
203
+ }
204
+ return [4 /*yield*/, axios_1["default"].get("".concat(host, "?userid=").concat(userid, "&deviceSource=").concat(deviceSource), {
205
+ headers: {
206
+ apptoken: apptoken
207
+ }
208
+ })];
209
+ case 1:
210
+ packageInfo = _a.sent();
211
+ return [2 /*return*/, packageInfo];
212
+ case 2:
213
+ error_4 = _a.sent();
214
+ logger.error("".concat(error_4));
215
+ return [3 /*break*/, 3];
216
+ case 3: return [2 /*return*/];
217
+ }
218
+ });
219
+ }); };
220
+ exports.downloadPackage = downloadPackage;
221
+ // 登录接口(暂时没有用到)
222
+ // export const loginByLocal = async (account: string, password: string) => {
223
+ // const data = {
224
+ // client_id: 'HuaMi',
225
+ // country_code: 'zh-CN',
226
+ // json_response: 'true',
227
+ // name: '',
228
+ // password: password,
229
+ // phoneOrEmail: account,
230
+ // redirect_uri: 'https://s3-us-west-2.amazonaws.com/hm-registration/successsignin.html',
231
+ // state: 'REDIRECTION',
232
+ // token: 'access'
233
+ // }
234
+ // if (/^[1][0-9]{10}$/.test(account)) {
235
+ // data.phoneOrEmail = encodeURIComponent(`+86${account}`)
236
+ // }
237
+ // data.name = data.phoneOrEmail.split('@')[0]
238
+ // const checkInfo = await postForm(`${CUR_REGISTER_URL}/registrations/${data.phoneOrEmail}/tokens`, data)
239
+ // const { data: loginInfo } = await postForm(`${LOGIN_URL}/v2/client/login`, {
240
+ // app_name: 'com.huami.webapp',
241
+ // app_version: '4.3.0',
242
+ // code: _.get(checkInfo, 'data.access', ''),
243
+ // country_code: 'zh-cn',
244
+ // device_id: '02:00:00:00:00:00',
245
+ // device_model: 'web',
246
+ // grant_type: 'access_token',
247
+ // third_name: 'huami'
248
+ // })
249
+ // return loginInfo
250
+ // }
package/bin/cli.js CHANGED
@@ -27,5 +27,7 @@ yargs_1["default"].usage('Usage: $0 <command> [args?]')
27
27
  // @ts-ignore
28
28
  .command('build', 'build this project for production', proxy, index_1.buildProd)
29
29
  .command('config', 'set or get the config value', proxy, index_1.config)
30
+ .command('status', 'view information such as login and emulator connection status', proxy, index_1.status)
31
+ .command('bridge', "connect the online App or Simulator for development and debugging", proxy, index_1.bridge)
30
32
  .fail(index_1.help)
31
33
  .argv;
package/config/device.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  var _a, _b;
3
3
  exports.__esModule = true;
4
- exports.deviceInternalCodename = exports.deviceTargets = exports.DeviceScreenType = exports.DeviceSource = exports.DevicePlatformName = exports.DeviceTarget = void 0;
4
+ exports.deviceInternalCodeName = exports.deviceTargets = exports.DeviceScreenType = exports.DeviceSource = exports.DevicePlatformName = exports.DeviceTarget = void 0;
5
5
  var DeviceTarget;
6
6
  (function (DeviceTarget) {
7
7
  DeviceTarget["MADRID"] = "madrid";
@@ -87,7 +87,8 @@ exports.deviceTargets = (_a = {},
87
87
  }
88
88
  },
89
89
  _a);
90
- exports.deviceInternalCodename = (_b = {},
90
+ // 这个方式获取 deviceSource 是不是会有问题呀, 需要手动补充
91
+ exports.deviceInternalCodeName = (_b = {},
91
92
  _b[DeviceSource.MADRID] = DeviceTarget.MADRID,
92
93
  _b[DeviceSource.MADRIDW] = DeviceTarget.MADRID,
93
94
  _b[DeviceSource.VERONA] = DeviceTarget.VERONA,
package/config/project.js CHANGED
@@ -2,9 +2,11 @@
2
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
+ var _a;
5
6
  exports.__esModule = true;
6
- exports.loginStorageKey = exports.loginConfig = exports.LocalServerConfig = exports.TemplateCopyType = exports.AppSourceType = exports.AppType = void 0;
7
+ exports.simulatorStorageKey = exports.buildStorageKey = exports.____inner_global_variable = exports.loginStorageKey = exports.loginConfig = exports.analyticsHost = exports.ACCOUNT_ENV = exports.ACCOUNT_ENV_KEY = exports.TemplateCopyType = exports.AppSourceType = exports.AppType = void 0;
7
8
  var lodash_1 = __importDefault(require("lodash"));
9
+ var storage_1 = require("../modules/storage");
8
10
  var AppType;
9
11
  (function (AppType) {
10
12
  AppType["APP"] = "app";
@@ -20,23 +22,37 @@ var TemplateCopyType;
20
22
  TemplateCopyType["CLONE"] = "clone";
21
23
  TemplateCopyType["EDIT"] = "edit";
22
24
  })(TemplateCopyType = exports.TemplateCopyType || (exports.TemplateCopyType = {}));
23
- var LocalServerConfig;
24
- (function (LocalServerConfig) {
25
- LocalServerConfig["HOSTNAME"] = "localhost";
26
- LocalServerConfig[LocalServerConfig["PORT"] = 8086] = "PORT";
27
- })(LocalServerConfig = exports.LocalServerConfig || (exports.LocalServerConfig = {}));
28
- var loginURL = 'https://user.huami.com/universalLogin/index.html#/login';
29
- var loginUrl = new URL(loginURL);
25
+ exports.ACCOUNT_ENV_KEY = 'account_env';
26
+ exports.ACCOUNT_ENV = storage_1.globalStorage.get(exports.ACCOUNT_ENV_KEY);
27
+ if (!exports.ACCOUNT_ENV) {
28
+ exports.ACCOUNT_ENV = 'production';
29
+ storage_1.globalStorage.set((_a = {}, _a[exports.ACCOUNT_ENV_KEY] = exports.ACCOUNT_ENV, _a));
30
+ }
31
+ var curLoginURL = exports.ACCOUNT_ENV === 'production' ? 'https://user.huami.com/universalLogin/index.html#/login' : 'https://user-testing.huami.com/universalLogin/index.html#/login';
32
+ exports.analyticsHost = exports.ACCOUNT_ENV === 'production' ? 'https://api-analytics.huami.com' : 'https://api-analytics-test.huami.com';
33
+ var loginUrl = new URL(curLoginURL);
30
34
  var accountPrefix = "____".concat(lodash_1["default"].replace(loginUrl.host, /\./g, '_'), "__");
31
35
  exports.loginConfig = {
32
- LOGIN_URL: loginURL,
36
+ LOGIN_URL: curLoginURL,
33
37
  PROJECT_NAME: 'zeppos_zeus_cli',
34
- PLATFORM_APP: 'com.huami.watch.hmwatchmanager',
35
- PROJECT_REDIRECT_URI: encodeURIComponent("http://".concat(LocalServerConfig.HOSTNAME, ":").concat(LocalServerConfig.PORT, "/"))
38
+ PLATFORM_APP: 'com.huami.webapp'
36
39
  };
37
40
  exports.loginStorageKey = {
41
+ nickName: "".concat(accountPrefix, "nickName"),
38
42
  accountToken: "".concat(accountPrefix, "token"),
39
43
  account: "".concat(accountPrefix, "account"),
40
44
  userid: "".concat(accountPrefix, "userid"),
41
45
  cname: "".concat(accountPrefix, "cname")
42
46
  };
47
+ exports.____inner_global_variable = {
48
+ bridgeOptionalDevice: '',
49
+ bridgeChoiredDevice: ''
50
+ };
51
+ exports.buildStorageKey = {
52
+ targetDeviceToBuild: "".concat(accountPrefix, "targetDeviceToBuild")
53
+ };
54
+ exports.simulatorStorageKey = {
55
+ simulatorHost: "simulator host",
56
+ simulatorPort: "simulator port",
57
+ simulatorConnectStatus: "simulator connect status"
58
+ };
package/index.js CHANGED
@@ -16,3 +16,5 @@ __exportStar(require("./modules/build"), exports);
16
16
  __exportStar(require("./modules/create"), exports);
17
17
  __exportStar(require("./modules/login"), exports);
18
18
  __exportStar(require("./modules/config"), exports);
19
+ __exportStar(require("./modules/status"), exports);
20
+ __exportStar(require("./modules/bridge"), exports);
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ var __generator = (this && this.__generator) || function (thisArg, body) {
31
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
32
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
33
+ function verb(n) { return function (v) { return step([n, v]); }; }
34
+ function step(op) {
35
+ if (f) throw new TypeError("Generator is already executing.");
36
+ while (_) try {
37
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
38
+ if (y = 0, t) op = [op[0] & 2, t.value];
39
+ switch (op[0]) {
40
+ case 0: case 1: t = op; break;
41
+ case 4: _.label++; return { value: op[1], done: false };
42
+ case 5: _.label++; y = op[1]; op = [0]; continue;
43
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
44
+ default:
45
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
46
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
47
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
48
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
49
+ if (t[2]) _.ops.pop();
50
+ _.trys.pop(); continue;
51
+ }
52
+ op = body.call(thisArg, _);
53
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
54
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
55
+ }
56
+ };
57
+ var __importDefault = (this && this.__importDefault) || function (mod) {
58
+ return (mod && mod.__esModule) ? mod : { "default": mod };
59
+ };
60
+ exports.__esModule = true;
61
+ exports.bridge = void 0;
62
+ var project_1 = require("../config/project");
63
+ var index_1 = require("../api/index");
64
+ var logger = __importStar(require("../utils/logger"));
65
+ var chalk_1 = __importDefault(require("chalk"));
66
+ var inquirer_1 = __importDefault(require("inquirer"));
67
+ var websocket_1 = __importDefault(require("./websocket"));
68
+ var build_1 = require("./build");
69
+ var common_1 = require("../utils/common");
70
+ // TODO: 需要在项目根目录执行
71
+ var client;
72
+ var hasConnectDevice = false;
73
+ var choiredDevice;
74
+ // 连接设备
75
+ // TODO: 有多个连接的时候退不到可输入状态
76
+ var connectDevice = function () { return __awaiter(void 0, void 0, void 0, function () {
77
+ var debugConnectedDevice, optionalDeviceArr, len, clientId, answer;
78
+ return __generator(this, function (_a) {
79
+ switch (_a.label) {
80
+ case 0:
81
+ debugConnectedDevice = JSON.parse(project_1.____inner_global_variable.bridgeOptionalDevice);
82
+ optionalDeviceArr = Object.keys(debugConnectedDevice);
83
+ len = optionalDeviceArr.length;
84
+ if (!(len === 0)) return [3 /*break*/, 1];
85
+ logger.log(chalk_1["default"].yellow('No connectable online App or Simulator.'));
86
+ return [3 /*break*/, 4];
87
+ case 1:
88
+ clientId = debugConnectedDevice[optionalDeviceArr[0]];
89
+ choiredDevice = optionalDeviceArr[0];
90
+ if (!(len > 1)) return [3 /*break*/, 3];
91
+ return [4 /*yield*/, inquirer_1["default"].prompt({
92
+ name: 'choiredDevice',
93
+ type: 'list',
94
+ choices: optionalDeviceArr,
95
+ message: 'Which online target would you like to connect?'
96
+ })];
97
+ case 2:
98
+ answer = _a.sent();
99
+ clientId = debugConnectedDevice[answer.choiredDevice];
100
+ choiredDevice = answer.choiredDevice;
101
+ _a.label = 3;
102
+ case 3:
103
+ project_1.____inner_global_variable.bridgeChoiredDevice = choiredDevice;
104
+ // 发送连接请求
105
+ client.sendMessage({ clientId: clientId }, 'connectClient');
106
+ hasConnectDevice = true;
107
+ _a.label = 4;
108
+ case 4: return [2 /*return*/];
109
+ }
110
+ });
111
+ }); };
112
+ var connectBridgeWSCallback = function () {
113
+ // 创建:Vorpal
114
+ var vorpal = require('vorpal')();
115
+ vorpal.find('help').hidden();
116
+ vorpal.find('exit').remove();
117
+ vorpal.command('exit', 'Exits application.')
118
+ .action(function () {
119
+ project_1.____inner_global_variable.bridgeOptionalDevice = '';
120
+ project_1.____inner_global_variable.bridgeChoiredDevice = '';
121
+ process.exit(0);
122
+ });
123
+ vorpal
124
+ .delimiter('bridge$')
125
+ .show();
126
+ vorpal["catch"]('[words...]', 'Catches incorrect commands.')
127
+ .action(function (args, cb) {
128
+ logger.log(" - '".concat(args.words.join(' '), "' is not a valid command.Enter 'help' to view Commands."));
129
+ cb();
130
+ });
131
+ vorpal
132
+ .command('connect', 'Connect the online App or Simulator for debugging.')
133
+ .action(function () { return __awaiter(void 0, void 0, void 0, function () {
134
+ return __generator(this, function (_a) {
135
+ // TODO: 断开链接怎么监听,需要处理啥吗
136
+ connectDevice();
137
+ return [2 /*return*/];
138
+ });
139
+ }); });
140
+ vorpal
141
+ .command('install', 'Install applications or watch face.')
142
+ .action(function () { return __awaiter(void 0, void 0, void 0, function () {
143
+ return __generator(this, function (_a) {
144
+ switch (_a.label) {
145
+ case 0:
146
+ if (!hasConnectDevice) return [3 /*break*/, 2];
147
+ return [4 /*yield*/, (0, build_1.buildPreview)({ _: ['install', "".concat(choiredDevice.toLowerCase())], '$0': 'zeus' }, function (params) {
148
+ client.sendMessage(params, 'packagePush');
149
+ })];
150
+ case 1:
151
+ _a.sent();
152
+ return [3 /*break*/, 3];
153
+ case 2:
154
+ logger.log(chalk_1["default"].yellow("No device is connected, please connect the device with the command width 'connect' first."));
155
+ _a.label = 3;
156
+ case 3: return [2 /*return*/];
157
+ }
158
+ });
159
+ }); });
160
+ // 卸载命令
161
+ vorpal
162
+ .command('uninstall', 'Uninstall applications or watch face.')
163
+ .action(function () { return __awaiter(void 0, void 0, void 0, function () {
164
+ var _a, appid, appname, apptype;
165
+ return __generator(this, function (_b) {
166
+ logger.info('Uninstalling');
167
+ _a = (0, common_1.getDeviceInfoFromAppJson)(), appid = _a.appId, appname = _a.appName, apptype = _a.appType;
168
+ client.sendMessage({ type: 'delete', appid: appid, appname: appname, apptype: apptype }, 'sysSettingPush');
169
+ return [2 /*return*/];
170
+ });
171
+ }); });
172
+ };
173
+ var disConnectBridgeWSCallback = function () {
174
+ // 什么情况下会断开连接?
175
+ hasConnectDevice = false;
176
+ logger.log('= 断开连接 =');
177
+ };
178
+ var bridge = function () { return __awaiter(void 0, void 0, void 0, function () {
179
+ var bridgeWebSocketURL, error_1;
180
+ return __generator(this, function (_a) {
181
+ switch (_a.label) {
182
+ case 0:
183
+ _a.trys.push([0, 2, , 3]);
184
+ return [4 /*yield*/, (0, index_1.getConnectDevServerWebSocketCode)()
185
+ // 第二步:连 WebSocket
186
+ ];
187
+ case 1:
188
+ bridgeWebSocketURL = _a.sent();
189
+ // 第二步:连 WebSocket
190
+ client = new websocket_1["default"]({
191
+ url: "".concat(bridgeWebSocketURL, "?type=development&name=CLI"),
192
+ connectCb: connectBridgeWSCallback,
193
+ disconnectCb: disConnectBridgeWSCallback
194
+ });
195
+ logger.log(" - Enter 'help' to view the command.");
196
+ return [3 /*break*/, 3];
197
+ case 2:
198
+ error_1 = _a.sent();
199
+ logger.error(error_1 === null || error_1 === void 0 ? void 0 : error_1.message);
200
+ return [3 /*break*/, 3];
201
+ case 3: return [2 /*return*/];
202
+ }
203
+ });
204
+ }); };
205
+ exports.bridge = bridge;