@zeppos/zeus-cli 1.0.24 → 1.1.0

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.
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,260 @@
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
+ // import { bridge } from '../modules/bridge'
80
+ // mark 调试时有用
81
+ // process.on('exit', (code) => {
82
+ // console.log(`About to exit with code: ${code}`);
83
+ // });
84
+ 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;
85
+ // 分区域名
86
+ exports.subRegionalHost = cname && cname.split(',').filter(function (h) { return h.indexOf('api-mifit') > -1; })[0];
87
+ var postForm = function (url, data) {
88
+ return (0, axios_1["default"])({
89
+ url: url,
90
+ method: 'POST',
91
+ headers: {
92
+ 'content-type': 'application/x-www-form-urlencoded'
93
+ },
94
+ data: qs_1["default"].stringify(data)
95
+ });
96
+ };
97
+ // 获取用户信息接口
98
+ var getUserInfo = function (url, token) { return __awaiter(void 0, void 0, void 0, function () {
99
+ var headers, userInfo, data, error_1;
100
+ return __generator(this, function (_a) {
101
+ switch (_a.label) {
102
+ case 0:
103
+ headers = {
104
+ apptoken: token,
105
+ 'Content-Type': 'application/json'
106
+ };
107
+ _a.label = 1;
108
+ case 1:
109
+ _a.trys.push([1, 3, , 4]);
110
+ return [4 /*yield*/, axios_1["default"].get(url, {
111
+ headers: headers
112
+ })];
113
+ case 2:
114
+ userInfo = _a.sent();
115
+ data = userInfo.data;
116
+ return [2 /*return*/, data];
117
+ case 3:
118
+ error_1 = _a.sent();
119
+ if (project_1.ACCOUNT_ENV !== 'production') {
120
+ logger.error("userInfoURL, ".concat(url));
121
+ logger.error("userInfoHeaders', ".concat(JSON.stringify(headers)));
122
+ }
123
+ logger.error("get nickName error");
124
+ return [3 /*break*/, 4];
125
+ case 4: return [2 /*return*/];
126
+ }
127
+ });
128
+ }); };
129
+ exports.getUserInfo = getUserInfo;
130
+ // 获取连接 developer server 的 websocket 连接
131
+ var getConnectDevServerWebSocketCode = function () { return __awaiter(void 0, void 0, void 0, function () {
132
+ var message, codeInfo, url, error_2, message, msg;
133
+ var _a, _b;
134
+ return __generator(this, function (_c) {
135
+ switch (_c.label) {
136
+ case 0:
137
+ if (!exports.subRegionalHost) {
138
+ storage_1.globalStorage.deleteStorage('____login');
139
+ 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'.");
140
+ throw new Error(message);
141
+ }
142
+ _c.label = 1;
143
+ case 1:
144
+ _c.trys.push([1, 3, , 4]);
145
+ return [4 /*yield*/, axios_1["default"].get("https://".concat(exports.subRegionalHost, "/developerbridge/client/relay"), {
146
+ headers: {
147
+ apptoken: apptoken
148
+ }
149
+ })];
150
+ case 2:
151
+ codeInfo = _c.sent();
152
+ url = codeInfo.data.url;
153
+ return [2 /*return*/, url];
154
+ case 3:
155
+ error_2 = _c.sent();
156
+ 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);
157
+ 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") {
158
+ storage_1.globalStorage.deleteStorage('____login');
159
+ // need login
160
+ // await login()
161
+ throw new Error("".concat(project_1.ACCOUNT_ENV === 'production' ? '' : 'test: ', "Invalid token,please login again with the command 'zeus login'."));
162
+ }
163
+ else {
164
+ msg = project_1.ACCOUNT_ENV === 'production' ? "GetConnectDevServerWebSocketCode error" : "".concat(project_1.ACCOUNT_ENV, ":getConnectDevServerWebSocketCode error ").concat(JSON.stringify(message));
165
+ project_1.____inner_global_variable.bridgeOptionalDevice = '';
166
+ project_1.____inner_global_variable.bridgeChoiredDevice = '';
167
+ throw new Error(msg);
168
+ }
169
+ return [3 /*break*/, 4];
170
+ case 4: return [2 /*return*/];
171
+ }
172
+ });
173
+ }); };
174
+ exports.getConnectDevServerWebSocketCode = getConnectDevServerWebSocketCode;
175
+ // 上传表盘/应用接口
176
+ var uploadPackage = function (form, length) { return __awaiter(void 0, void 0, void 0, function () {
177
+ var packageInfo, error_3, message;
178
+ return __generator(this, function (_a) {
179
+ switch (_a.label) {
180
+ case 0:
181
+ _a.trys.push([0, 2, , 3]);
182
+ return [4 /*yield*/, axios_1["default"].post("https://".concat(exports.subRegionalHost, "/custom/tools/app-dial/upload?userid=").concat(userid), form, {
183
+ headers: __assign(__assign({}, form.getHeaders()), { 'content-length': "".concat(length), apptoken: apptoken })
184
+ })];
185
+ case 1:
186
+ packageInfo = _a.sent();
187
+ return [2 /*return*/, packageInfo];
188
+ case 2:
189
+ error_3 = _a.sent();
190
+ logger.error("".concat(error_3));
191
+ if (typeof error_3 === 'string') {
192
+ message = error_3.includes('401') ? "Invalid token,please login again with the command 'zeus login'." : error_3;
193
+ logger.error("".concat(message));
194
+ }
195
+ // need login
196
+ process.exit(1);
197
+ return [3 /*break*/, 3];
198
+ case 3: return [2 /*return*/];
199
+ }
200
+ });
201
+ }); };
202
+ exports.uploadPackage = uploadPackage;
203
+ // 下载表盘/应用接口
204
+ var downloadPackage = function (code, deviceSource, appType) { return __awaiter(void 0, void 0, void 0, function () {
205
+ var host, packageInfo, error_4;
206
+ return __generator(this, function (_a) {
207
+ switch (_a.label) {
208
+ case 0:
209
+ _a.trys.push([0, 2, , 3]);
210
+ host = "https://".concat(exports.subRegionalHost, "/custom/tools/app-dial/download/").concat(code);
211
+ if (appType === 'app') {
212
+ return [2 /*return*/, host];
213
+ }
214
+ return [4 /*yield*/, axios_1["default"].get("".concat(host, "?userid=").concat(userid, "&deviceSource=").concat(deviceSource), {
215
+ headers: {
216
+ apptoken: apptoken
217
+ }
218
+ })];
219
+ case 1:
220
+ packageInfo = _a.sent();
221
+ return [2 /*return*/, packageInfo];
222
+ case 2:
223
+ error_4 = _a.sent();
224
+ logger.error("".concat(error_4));
225
+ return [3 /*break*/, 3];
226
+ case 3: return [2 /*return*/];
227
+ }
228
+ });
229
+ }); };
230
+ exports.downloadPackage = downloadPackage;
231
+ // 登录接口(暂时没有用到)
232
+ // export const loginByLocal = async (account: string, password: string) => {
233
+ // const data = {
234
+ // client_id: 'HuaMi',
235
+ // country_code: 'zh-CN',
236
+ // json_response: 'true',
237
+ // name: '',
238
+ // password: password,
239
+ // phoneOrEmail: account,
240
+ // redirect_uri: 'https://s3-us-west-2.amazonaws.com/hm-registration/successsignin.html',
241
+ // state: 'REDIRECTION',
242
+ // token: 'access'
243
+ // }
244
+ // if (/^[1][0-9]{10}$/.test(account)) {
245
+ // data.phoneOrEmail = encodeURIComponent(`+86${account}`)
246
+ // }
247
+ // data.name = data.phoneOrEmail.split('@')[0]
248
+ // const checkInfo = await postForm(`${CUR_REGISTER_URL}/registrations/${data.phoneOrEmail}/tokens`, data)
249
+ // const { data: loginInfo } = await postForm(`${LOGIN_URL}/v2/client/login`, {
250
+ // app_name: 'com.huami.webapp',
251
+ // app_version: '4.3.0',
252
+ // code: _.get(checkInfo, 'data.access', ''),
253
+ // country_code: 'zh-cn',
254
+ // device_id: '02:00:00:00:00:00',
255
+ // device_model: 'web',
256
+ // grant_type: 'access_token',
257
+ // third_name: 'huami'
258
+ // })
259
+ // return loginInfo
260
+ // }
package/bin/cli.js CHANGED
@@ -28,5 +28,6 @@ yargs_1["default"].usage('Usage: $0 <command> [args?]')
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
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)
31
32
  .fail(index_1.help)
32
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,8 +2,9 @@
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.ACCOUNT_ENV = exports.ACCOUNT_ENV_KEY = 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"));
8
9
  var storage_1 = require("../modules/storage");
9
10
  var AppType;
@@ -22,21 +23,36 @@ var TemplateCopyType;
22
23
  TemplateCopyType["EDIT"] = "edit";
23
24
  })(TemplateCopyType = exports.TemplateCopyType || (exports.TemplateCopyType = {}));
24
25
  exports.ACCOUNT_ENV_KEY = 'account_env';
25
- var storage = new storage_1.Storage({ cmd: '' });
26
- exports.ACCOUNT_ENV = storage.get(exports.ACCOUNT_ENV_KEY) ? storage.get(exports.ACCOUNT_ENV_KEY) : storage.set(exports.ACCOUNT_ENV_KEY, 'production');
27
- var loginTestURL = 'https://user-testing.huami.com/universalLogin/index.html#/login';
28
- var loginProURL = 'https://user.huami.com/universalLogin/index.html#/login';
29
- var curLoginURL = exports.ACCOUNT_ENV === 'production' ? loginProURL : loginTestURL;
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.zepp.com/universalLogin/index.html#/login' : 'https://user-testing.zepp.com/universalLogin/index.html#/login';
32
+ exports.analyticsHost = exports.ACCOUNT_ENV === 'production' ? 'https://api-analytics.huami.com' : 'https://api-analytics-test.huami.com';
30
33
  var loginUrl = new URL(curLoginURL);
31
34
  var accountPrefix = "____".concat(lodash_1["default"].replace(loginUrl.host, /\./g, '_'), "__");
32
35
  exports.loginConfig = {
33
36
  LOGIN_URL: curLoginURL,
34
37
  PROJECT_NAME: 'zeppos_zeus_cli',
35
- PLATFORM_APP: 'com.huami.watch.hmwatchmanager'
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
@@ -17,3 +17,4 @@ __exportStar(require("./modules/create"), exports);
17
17
  __exportStar(require("./modules/login"), exports);
18
18
  __exportStar(require("./modules/config"), exports);
19
19
  __exportStar(require("./modules/status"), exports);
20
+ __exportStar(require("./modules/bridge"), exports);
@@ -0,0 +1,221 @@
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 websocket_1 = __importDefault(require("./websocket"));
67
+ var build_1 = require("./build");
68
+ var common_1 = require("../utils/common");
69
+ var project_2 = require("../config/project");
70
+ var client;
71
+ var hasConnectDevice = false;
72
+ var choiredDevice;
73
+ // 连接设备
74
+ // TODO: 有多个连接的时候退不到可输入状态
75
+ var connectDevice = function (vorpal) { return __awaiter(void 0, void 0, void 0, function () {
76
+ var debugConnectedDevice, optionalDeviceArr, len, clientId_1;
77
+ return __generator(this, function (_a) {
78
+ switch (_a.label) {
79
+ case 0:
80
+ debugConnectedDevice = JSON.parse(project_1.____inner_global_variable.bridgeOptionalDevice);
81
+ optionalDeviceArr = Object.keys(debugConnectedDevice);
82
+ len = optionalDeviceArr.length;
83
+ if (!(len === 0)) return [3 /*break*/, 1];
84
+ logger.log(chalk_1["default"].yellow('No connectable online App or Simulator.'));
85
+ return [3 /*break*/, 4];
86
+ case 1:
87
+ clientId_1 = debugConnectedDevice[optionalDeviceArr[0]];
88
+ choiredDevice = optionalDeviceArr[0];
89
+ if (!(len > 1)) return [3 /*break*/, 3];
90
+ return [4 /*yield*/, vorpal.activeCommand.prompt({
91
+ name: 'choiredDevice',
92
+ type: 'list',
93
+ choices: optionalDeviceArr,
94
+ message: 'Which online target would you like to connect?'
95
+ }, function (result) {
96
+ clientId_1 = debugConnectedDevice[result.choiredDevice];
97
+ choiredDevice = result.choiredDevice;
98
+ vorpal.delimiter('bridge$').show();
99
+ })];
100
+ case 2:
101
+ _a.sent();
102
+ _a.label = 3;
103
+ case 3:
104
+ project_1.____inner_global_variable.bridgeChoiredDevice = choiredDevice;
105
+ // 发送连接请求
106
+ client.sendMessage({ clientId: clientId_1 }, 'connectClient');
107
+ hasConnectDevice = true;
108
+ _a.label = 4;
109
+ case 4: return [2 /*return*/];
110
+ }
111
+ });
112
+ }); };
113
+ var connectBridgeWSCallback = function () {
114
+ // 创建:Vorpal
115
+ var vorpal = require('vorpal')();
116
+ vorpal.find('help').hidden();
117
+ vorpal.find('exit').remove();
118
+ vorpal.command('exit', 'Exits application.')
119
+ .action(function () {
120
+ project_1.____inner_global_variable.bridgeOptionalDevice = '';
121
+ project_1.____inner_global_variable.bridgeChoiredDevice = '';
122
+ process.exit(0);
123
+ });
124
+ vorpal
125
+ .delimiter('bridge$')
126
+ .show();
127
+ vorpal["catch"]('[words...]', 'Catches incorrect commands.')
128
+ .action(function (args, cb) {
129
+ logger.log(" - '".concat(args.words.join(' '), "' is not a valid command.Enter 'help' to view Commands."));
130
+ cb();
131
+ });
132
+ vorpal
133
+ .command('connect', 'Connect the online App or Simulator for debugging.')
134
+ .option('--anchovies')
135
+ .action(function (args, cb) { return __awaiter(void 0, void 0, void 0, function () {
136
+ return __generator(this, function (_a) {
137
+ // TODO: 断开链接怎么监听,需要处理啥吗
138
+ connectDevice(vorpal);
139
+ cb();
140
+ return [2 /*return*/];
141
+ });
142
+ }); });
143
+ vorpal
144
+ .command('install', 'Install applications or watch face.')
145
+ .action(function (args, cb) { return __awaiter(void 0, void 0, void 0, function () {
146
+ return __generator(this, function (_a) {
147
+ switch (_a.label) {
148
+ case 0:
149
+ if (!hasConnectDevice) return [3 /*break*/, 2];
150
+ return [4 /*yield*/, (0, build_1.buildPreview)({ _: ['install', "".concat(choiredDevice.toLowerCase())], '$0': 'zeus' }, function (params) {
151
+ client.sendMessage(params, 'packagePush');
152
+ })];
153
+ case 1:
154
+ _a.sent();
155
+ return [3 /*break*/, 3];
156
+ case 2:
157
+ logger.log(chalk_1["default"].yellow("No device is connected, please connect the device with the command width 'connect' first."));
158
+ _a.label = 3;
159
+ case 3:
160
+ cb();
161
+ return [2 /*return*/];
162
+ }
163
+ });
164
+ }); });
165
+ // 卸载命令
166
+ vorpal
167
+ .command('uninstall [appid]', 'Uninstall applications or watch face.')
168
+ .action(function (args, cb) { return __awaiter(void 0, void 0, void 0, function () {
169
+ var _a, appid, appName, appType;
170
+ return __generator(this, function (_b) {
171
+ if (!args.appid) {
172
+ logger.info('Uninstalling');
173
+ _a = (0, common_1.getDeviceInfoFromAppJson)(), appid = _a.appId, appName = _a.appName, appType = _a.appType;
174
+ client.sendMessage({ type: 'delete', appid: appid, appName: appName, appType: appType }, 'sysSettingPush');
175
+ }
176
+ else if (args.appid && typeof args.appid === 'number') {
177
+ logger.info('Uninstalling');
178
+ client.sendMessage({ type: 'delete', appid: args.appid }, 'sysSettingPush');
179
+ }
180
+ else if (args.appid) {
181
+ logger.info("invalid '".concat(args.appid, "', the 'uninstall' command should be followed by appid or blank."));
182
+ }
183
+ cb();
184
+ return [2 /*return*/];
185
+ });
186
+ }); });
187
+ };
188
+ var disConnectBridgeWSCallback = function () {
189
+ // 什么情况下会断开连接?
190
+ hasConnectDevice = false;
191
+ logger.log('= 断开连接 =');
192
+ process.exit(1);
193
+ };
194
+ var bridge = function () { return __awaiter(void 0, void 0, void 0, function () {
195
+ var bridgeWebSocketURL, error_1;
196
+ return __generator(this, function (_a) {
197
+ switch (_a.label) {
198
+ case 0:
199
+ _a.trys.push([0, 2, , 3]);
200
+ return [4 /*yield*/, (0, index_1.getConnectDevServerWebSocketCode)()
201
+ // 第二步:连 WebSocket
202
+ ];
203
+ case 1:
204
+ bridgeWebSocketURL = _a.sent();
205
+ // 第二步:连 WebSocket
206
+ client = new websocket_1["default"]({
207
+ url: "".concat(bridgeWebSocketURL, "?type=development&name=CLI"),
208
+ connectCb: connectBridgeWSCallback,
209
+ disconnectCb: disConnectBridgeWSCallback
210
+ });
211
+ logger.log("".concat(project_2.ACCOUNT_ENV === 'production' ? '' : 'test:', " - Enter 'help' to view the command."));
212
+ return [3 /*break*/, 3];
213
+ case 2:
214
+ error_1 = _a.sent();
215
+ logger.error(error_1 === null || error_1 === void 0 ? void 0 : error_1.message);
216
+ return [3 /*break*/, 3];
217
+ case 3: return [2 /*return*/];
218
+ }
219
+ });
220
+ }); };
221
+ exports.bridge = bridge;