@aiot-toolkit/emulator 2.0.3-beta.1 → 2.0.3-beta.11

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 (45) hide show
  1. package/README.md +133 -8
  2. package/lib/emulatorutil/EmulatorLog.js +22 -18
  3. package/lib/emulatorutil/constants.d.ts +18 -5
  4. package/lib/emulatorutil/constants.js +94 -53
  5. package/lib/emulatorutil/index.d.ts +3 -2
  6. package/lib/emulatorutil/index.js +38 -8
  7. package/lib/emulatorutil/running.d.ts +24 -0
  8. package/lib/emulatorutil/running.js +108 -0
  9. package/lib/emulatorutil/skinLayoutParser.d.ts +14 -0
  10. package/lib/emulatorutil/skinLayoutParser.js +111 -0
  11. package/lib/index.d.ts +5 -5
  12. package/lib/index.js +76 -26
  13. package/lib/instance/common.d.ts +39 -39
  14. package/lib/instance/common.js +151 -221
  15. package/lib/instance/dev.d.ts +7 -42
  16. package/lib/instance/dev.js +53 -235
  17. package/lib/instance/index.d.ts +6 -5
  18. package/lib/instance/index.js +51 -35
  19. package/lib/instance/miwear.d.ts +14 -75
  20. package/lib/instance/miwear.js +92 -370
  21. package/lib/instance/pre.d.ts +11 -3
  22. package/lib/instance/pre.js +54 -93
  23. package/lib/instance/pre5.d.ts +11 -0
  24. package/lib/instance/pre5.js +37 -0
  25. package/lib/static/avdConfigIni.json +5 -5
  26. package/lib/typing/Instance.d.ts +32 -15
  27. package/lib/typing/Instance.js +13 -6
  28. package/lib/typing/Vvd.d.ts +105 -0
  29. package/lib/typing/Vvd.js +31 -0
  30. package/lib/utils/file.d.ts +0 -0
  31. package/lib/utils/file.js +1 -0
  32. package/lib/utils/index.js +86 -100
  33. package/lib/vvd/index.d.ts +107 -0
  34. package/lib/vvd/index.js +715 -0
  35. package/lib/vvd/logcat.d.ts +16 -0
  36. package/lib/vvd/logcat.js +67 -0
  37. package/package.json +9 -8
  38. package/lib/avd/index.d.ts +0 -28
  39. package/lib/avd/index.js +0 -173
  40. package/lib/emulatorutil/EmulatorCmd.d.ts +0 -9
  41. package/lib/emulatorutil/EmulatorCmd.js +0 -226
  42. package/lib/instance/preDev.d.ts +0 -53
  43. package/lib/instance/preDev.js +0 -249
  44. package/lib/typing/Avd.d.ts +0 -23
  45. package/lib/typing/Avd.js +0 -8
@@ -1,230 +1,160 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
17
5
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- const UxFileUtils_1 = __importDefault(require("@aiot-toolkit/aiotpack/lib/utils/ux/UxFileUtils"));
39
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
40
- const adbMiwt = __importStar(require("@miwt/adb"));
41
- const child_process_1 = require("child_process");
42
- const fs_extra_1 = __importDefault(require("fs-extra"));
43
- const os_1 = __importDefault(require("os"));
44
- const path_1 = __importDefault(require("path"));
45
- const ws_1 = require("ws");
46
- const avd_1 = __importDefault(require("../avd"));
47
- const constants_1 = require("../emulatorutil/constants");
48
- const utils_1 = require("../utils");
49
- /**
50
- * CommonInstance
51
- */
52
- class CommonInstance {
53
- constructor(params) {
54
- this.quickappStartedFlag = /Start App loop/;
55
- this.isFirstStart = true;
56
- this.isDistributedApp = false;
57
- this.projectPath = params.projectPath;
58
- this.sdkHome = params.sdkHome || constants_1.defaultSDKHome;
59
- this.avdHome = params.avdHome || constants_1.defaultAvdHome;
60
- this.isRpk = params.sourceRoot === './';
61
- const projectJsonFile = path_1.default.resolve(this.projectPath, '.project.json');
62
- const projectJsonExist = fs_extra_1.default.existsSync(projectJsonFile);
63
- if (projectJsonExist) {
64
- const projectJsonInfo = fs_extra_1.default.readJSONSync(projectJsonFile);
65
- this.isDistributedApp = projectJsonInfo.projectType === 'distributed';
66
- }
67
- this.velaAvdCls = new avd_1.default({
68
- sdkHome: this.sdkHome,
69
- avdHome: this.avdHome
70
- });
71
- this.projectInfo = UxFileUtils_1.default.getMainfestInfo(this.projectPath, params.sourceRoot);
72
- }
73
- /** 获取模拟器二进制文件所在位置 */
74
- getEmulatorBinPath() {
75
- const osPlatform = os_1.default.platform();
76
- const arch = (0, utils_1.getSystemArch)();
77
- const platform = osPlatform === 'win32' ? 'windows' : osPlatform;
78
- const emulatorHome = path_1.default.resolve(this.sdkHome, 'emulator');
79
- return path_1.default.resolve(emulatorHome, `${platform}-${arch}`, 'emulator');
80
- }
81
- /** 在goldfish模拟器中运行快应用 */
82
- start(options) {
83
- return __awaiter(this, void 0, void 0, function* () {
84
- throw new Error(`start method not implemented, ${options}`);
6
+ exports.default = void 0;
7
+ var _ColorConsole = _interopRequireDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
8
+ var _ILog = require("@aiot-toolkit/shared-utils/lib/interface/ILog");
9
+ var adbMiwt = _interopRequireWildcard(require("@miwt/adb"));
10
+ var _utils = require("../utils");
11
+ var _logcat = require("../vvd/logcat");
12
+ var _emulatorutil = require("../emulatorutil");
13
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
14
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
15
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
16
+ class CommonEmulatorInstance {
17
+ appDir = '/data/quickapp/app';
18
+ quickappStartedFlag = /Start App loop/;
19
+ constructor(params) {
20
+ this.sn = `emulator-${params.serialPort}`;
21
+ this.vvdName = params.vvdName;
22
+ this.onStdout = params.onStdout || console.log;
23
+ this.onErrout = params.onErrout || console.error;
24
+ this.debugPort = params.debugPort || (0, _emulatorutil.getRunningVvdDebugPort)(this.vvdName);
25
+ if (params.customLogger) {
26
+ this.logger = msg => {
27
+ const prefix = _ColorConsole.default.createDefaultPrefix({
28
+ level: _ILog.Loglevel.INFO,
29
+ message: msg
85
30
  });
31
+ params.customLogger(`${prefix[1]} ${msg?.trim()}`);
32
+ };
33
+ } else {
34
+ this.logger = _ColorConsole.default.info;
86
35
  }
87
- /**
88
- * 判断模拟器是否 ready
89
- */
90
- isConnected() {
91
- return __awaiter(this, void 0, void 0, function* () {
92
- return (0, utils_1.tryRun)(() => __awaiter(this, void 0, void 0, function* () {
93
- const devices = yield adbMiwt.getAdbDevices();
94
- ColorConsole_1.default.log(`### Emulator ### adb devices: ${JSON.stringify(devices)}`);
95
- const curDev = devices.find((t) => t.sn === this.sn);
96
- return (curDev === null || curDev === void 0 ? void 0 : curDev.status) === 'device';
97
- }), 10, 500);
98
- });
36
+ if (params.logcatProcess) {
37
+ this.logcatProcess = params.logcatProcess;
38
+ if (!params.stdoutReadline || !params.stderrReadline) {
39
+ const readlines = (0, _logcat.attachReadline)(params.logcatProcess, this.onStdout, this.onErrout);
40
+ params.stdoutReadline = readlines.stdoutReadline;
41
+ params.stderrReadline = readlines.stderrReadline;
42
+ params.dispose = readlines.dispose;
43
+ }
44
+ this.stdoutReadline = params.stdoutReadline;
45
+ this.stderrReadline = params.stderrReadline;
46
+ this.disposeReadlines = params.dispose;
47
+ } else {
48
+ const r = (0, _logcat.creatLogcat)(this.sn, this.onStdout, this.onErrout);
49
+ this.logcatProcess = r.logcatProcess;
50
+ this.stdoutReadline = r.stdoutReadline;
51
+ this.stderrReadline = r.stderrReadline;
52
+ this.disposeReadlines = r.dispose;
99
53
  }
100
- /**
101
- * 通过adb连接模拟器。
102
- * 时间限制为 @param timeout 秒,超时则表示连接失败
103
- * @returns
104
- */
105
- connectGoldfish() {
106
- return __awaiter(this, arguments, void 0, function* (timeout = 10) {
107
- let adbConnected = false; // adb是否连接成功
108
- let enableLoop = true; // 是否允许循环,用于终止adb的连接
109
- let needKill = false;
110
- const connectFn = () => __awaiter(this, void 0, void 0, function* () {
111
- while (enableLoop && !adbConnected) {
112
- if (needKill) {
113
- const adbKillCmd = `adb kill-server`;
114
- ColorConsole_1.default.log(`### Emulator ### Excuting adb cmd: ${adbKillCmd}`);
115
- yield adbMiwt.execAdbCmdAsync(adbKillCmd);
116
- }
117
- const str = yield this.connectDevice();
118
- ColorConsole_1.default.log(`### Emulator ### ${str}`);
119
- // 查询模拟器的状态是否为“device”
120
- const devices = yield adbMiwt.getAdbDevices();
121
- ColorConsole_1.default.log(`### Emulator ### adb devices: ${JSON.stringify(devices)}`);
122
- const curDev = devices.find((t) => t.sn === this.sn);
123
- if ((curDev === null || curDev === void 0 ? void 0 : curDev.status) === 'offline')
124
- needKill = true;
125
- adbConnected = devices.some((item) => item.sn === this.sn && item.status === 'device');
126
- }
127
- return Promise.resolve(adbConnected);
128
- });
129
- const res = yield Promise.race([
130
- connectFn(),
131
- new Promise((resolve) => {
132
- setTimeout(() => {
133
- if (!adbConnected) {
134
- enableLoop = false;
135
- }
136
- // 超时则认为adb没有连接成功
137
- resolve(false);
138
- }, timeout * 1000);
139
- })
140
- ]);
141
- return res;
54
+ }
55
+
56
+ /** 推送指定文件 */
57
+ async push(sourcePath, targetPath) {
58
+ // 1. adb push应用的rpk
59
+ const pushCmd = `adb -s ${this.sn} push "${sourcePath}" ${targetPath}`;
60
+ this.logger(`Excuting: ${pushCmd}`);
61
+ const res = await adbMiwt.execAdbCmdAsync(pushCmd);
62
+ this.logger(`Push result: ${res}`);
63
+ return targetPath;
64
+ }
65
+ async unzip(zipPath, targetPath) {
66
+ const unzipCmd = `adb -s ${this.sn} shell unzip -o ${zipPath} -d ${targetPath}`;
67
+ this.logger(`Excuting: ${unzipCmd}`);
68
+ const res = await adbMiwt.execAdbCmdAsync(unzipCmd);
69
+ this.logger(`Unzip result: ${res}`);
70
+ }
71
+
72
+ /** 推送指定 rpk */
73
+ async pushRpk(rpkPath, appPackageName) {
74
+ // 1. adb push应用的rpk
75
+ const targetPath = `${this.appDir}/${appPackageName}.rpk`;
76
+ const pushCmd = `adb -s ${this.sn} push "${rpkPath}" ${targetPath}`;
77
+ this.logger(`Excuting: ${pushCmd}`);
78
+ const res = await adbMiwt.execAdbCmdAsync(pushCmd);
79
+ this.logger(`Push ${this.vvdName} rpk result: ${res}`);
80
+ return targetPath;
81
+ }
82
+
83
+ /**
84
+ * 判断模拟器是否 ready
85
+ */
86
+ async isConnected() {
87
+ return (0, _utils.tryRun)(async () => {
88
+ const devices = await adbMiwt.getAdbDevices();
89
+ _ColorConsole.default.log(`adb devices: ${JSON.stringify(devices)}`);
90
+ const curDev = devices.find(t => t.sn === this.sn);
91
+ return curDev?.status === 'device';
92
+ }, 10, 500);
93
+ }
94
+
95
+ /** 安装应用,留给子类实现 */
96
+ async install(rpkPath, packageName) {}
97
+
98
+ /** 卸载应用,留给子类实现 */
99
+ async uninstall(packageName) {}
100
+
101
+ /** 启动应用,留给子类实现 */
102
+ async startApp(packageName) {
103
+ let debug = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
104
+ }
105
+ async pushAndInstall(rpkPath, appName) {
106
+ const targetPath = await this.pushRpk(rpkPath, appName);
107
+ await this.install(targetPath, appName);
108
+ }
109
+
110
+ /** 重启应用 */
111
+ async reloadApp(appName) {
112
+ let debug = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
113
+ }
114
+
115
+ /** 关闭应用 */
116
+ async closeApp(appName) {}
117
+
118
+ /** 关闭模拟器 */
119
+ poweroff() {
120
+ let timeout = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 10 * 1000;
121
+ return new Promise((resolve, reject) => {
122
+ const t = setTimeout(() => {
123
+ (0, _utils.killProcessByCmd)(this.vvdName).then(resolve).catch(e => {
124
+ reject(`poweroff ${this.vvdName} failed ${e}`);
142
125
  });
143
- }
144
- /** 杀死进程 */
145
- killProcess(currProcess) {
146
- if (currProcess && currProcess.pid && currProcess.exitCode === null) {
147
- console.log('process pid:', currProcess.pid);
148
- try {
149
- if (os_1.default.platform() === 'win32') {
150
- (0, child_process_1.execSync)(`taskkill /pid ${currProcess.pid} /T /F`);
151
- }
152
- else if (os_1.default.platform() === 'darwin') {
153
- process.kill(currProcess.pid);
154
- }
155
- else {
156
- currProcess.kill();
157
- }
158
- }
159
- catch (err) {
160
- ColorConsole_1.default.log(`### Emulator ### kill process get error :\n${err.stack}`);
161
- }
162
- }
163
- }
164
- /** 停止模拟器并释放相关资源 */
165
- stop() {
166
- return __awaiter(this, void 0, void 0, function* () {
167
- adbMiwt.execAdbCmdSync(`adb -s ${this.sn} shell poweroff`);
168
- });
169
- }
170
- /** 重启模拟器 */
171
- restart() {
172
- return __awaiter(this, void 0, void 0, function* () {
173
- yield this.stop();
174
- this.start(this.startOptions);
175
- });
176
- }
177
- /** 创建server */
178
- createWebsockeServer() {
179
- return __awaiter(this, void 0, void 0, function* () {
180
- var _a;
181
- const wsServer = new ws_1.WebSocketServer({
182
- port: (_a = this.startOptions) === null || _a === void 0 ? void 0 : _a.serverPort
183
- });
184
- wsServer.on('connection', (socket) => {
185
- ColorConsole_1.default.success(`### App Socket server ### Websocket connects to websocket server`);
186
- socket.on('error', (err) => {
187
- ColorConsole_1.default.error(`### App Socket server ### Websocket server error: ${err.message}`);
188
- });
189
- socket.on('message', (data) => {
190
- const message = JSON.parse(data.toString());
191
- ColorConsole_1.default.log(`### App Socket server ### Websocket server get data: ${data}`);
192
- if (message.type === 'restart') {
193
- this.restart();
194
- }
195
- else if (message.type === 'stop') {
196
- this.stop();
197
- }
198
- });
199
- });
200
- });
201
- }
202
- connectDevice() {
203
- return __awaiter(this, void 0, void 0, function* () {
204
- const adbConnectCmd = `adb connect ${this.sn}`;
205
- ColorConsole_1.default.log(`### Emulator ### Excuting adb cmd: ${adbConnectCmd}`);
206
- let pending = true;
207
- const p1 = adbMiwt.execAdbCmdAsync(adbConnectCmd);
208
- let timer;
209
- // 超过一定时间还没有连接成功,则 kill-server 后再试
210
- // 用于处理 adb connect 一直 pending 的情况
211
- const p2 = new Promise((resolve) => {
212
- timer = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
213
- if (pending) {
214
- const adbKillCmd = `adb kill-server`;
215
- ColorConsole_1.default.log(`### Emulator ### Excuting adb cmd: ${adbKillCmd}`);
216
- yield adbMiwt.execAdbCmdAsync(adbKillCmd);
217
- }
218
- resolve();
219
- }), 10 * 1000);
220
- });
221
- p1.then((r) => {
222
- pending = false;
223
- clearTimeout(timer);
224
- console.log(r);
225
- });
226
- return Promise.race([p1, p2]);
126
+ }, timeout);
127
+ adbMiwt.execAdbCmdAsync(`adb -s ${this.sn} shell poweroff`).then(() => {
128
+ clearTimeout(t);
129
+ resolve();
130
+ }).catch(() => {
131
+ (0, _utils.killProcessByCmd)(this.vvdName).then(() => {
132
+ clearTimeout(t);
133
+ resolve();
134
+ }).catch(e => {
135
+ reject(`poweroff ${this.vvdName} failed ${e}`);
227
136
  });
137
+ });
138
+ });
139
+ }
140
+ systemed() {
141
+ return adbMiwt.execAdbCmdAsync(`adb -s ${this.sn} shell systemd &`);
142
+ }
143
+
144
+ /** 重启模拟器 */
145
+ async reboot() {
146
+ const rebootCmd = `adb -s ${this.sn} shell reboot`;
147
+ _ColorConsole.default.log(`Excuting: ${rebootCmd}`);
148
+ await adbMiwt.execAdbCmdAsync(rebootCmd);
149
+ await this.isConnected();
150
+ if (!this.logcatProcess.exitCode !== undefined) {
151
+ // 如果 logcat 进程被杀死,则重新创建
152
+ const r = (0, _logcat.creatLogcat)(this.sn, this.onStdout, this.onErrout);
153
+ this.logcatProcess = r.logcatProcess;
154
+ this.stdoutReadline = r.stdoutReadline;
155
+ this.stderrReadline = r.stderrReadline;
156
+ this.disposeReadlines = r.dispose;
228
157
  }
158
+ }
229
159
  }
230
- exports.default = CommonInstance;
160
+ var _default = exports.default = CommonEmulatorInstance;
@@ -1,51 +1,16 @@
1
- import readline from 'readline';
2
- import { INewGoldfishInstanceParams, IStartOptions } from '../typing/Instance';
3
- import CommonInstance from './common';
4
- declare class GoldfishInstance extends CommonInstance {
1
+ import CommonEmulatorInstance from './common';
2
+ import { VelaImageType } from '../typing/Vvd';
3
+ declare class GoldfishInstance extends CommonEmulatorInstance {
4
+ imageType: VelaImageType;
5
5
  appDir: string;
6
6
  emulatorStartedFlag: string;
7
- stdoutReadline: readline.Interface;
8
- stderrReadline: readline.Interface;
9
- constructor(params: INewGoldfishInstanceParams);
10
- /**
11
- * 1. 启动模拟器
12
- * 2. 启动成功后,adb连接模拟器
13
- * 3. 连接成功后,在模拟器中启动快应用
14
- */
15
- start(options: IStartOptions): Promise<void>;
7
+ install(rpkPath: string, appPackageName?: string): Promise<void>;
16
8
  /**
17
9
  * 在模拟器中启动快应用
18
10
  * 通过vapp命令启动,调试时需额外配置--jsdebugger参数
19
11
  * @param options
20
12
  */
21
- startupQuickApp(options: IStartOptions): Promise<void>;
22
- /**
23
- * 启动模拟器
24
- * 1. 通过options生成模拟器的启动命令
25
- * 2. 执行启动命令
26
- * 3. 判断模拟器是否启动成功
27
- * 3.1 若disableNSH=true,输出流中匹配到(NSH),认为模拟器启动成功了
28
- * 3.2 若disableNSH=false,认为2s过后模拟器启动成功了
29
- * @param options
30
- * @returns
31
- */
32
- startGoldfish(options: IStartOptions): Promise<void>;
33
- /**
34
- * 将目录通过adb push到模拟器中
35
- */
36
- pushAndInstall(rpkPath?: string): Promise<void>;
37
- /** 通过命令行启动时,在watich模式下监听websocket消息,
38
- * 在文件发生变动时,重新启动应用
39
- */
40
- restart(): Promise<void>;
41
- /**
42
- * 重新推送,然后重启应用
43
- */
44
- pushAndReloadApp(): Promise<void>;
45
- /**
46
- * 重启应用
47
- */
48
- reloadApp(): Promise<void>;
49
- reboot(): Promise<boolean>;
13
+ startApp(packageName: string, debug?: boolean): Promise<void>;
14
+ reloadApp(appName: string, debug?: boolean): Promise<void>;
50
15
  }
51
16
  export default GoldfishInstance;