@aiot-toolkit/emulator 2.0.1-alpha.8 → 2.0.2-batchmanifest-beta.1
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 +14 -1
- package/lib/avd/index.d.ts +12 -0
- package/lib/avd/index.js +58 -36
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -5
- package/lib/instance/common.d.ts +42 -0
- package/lib/instance/common.js +227 -0
- package/lib/instance/dev.d.ts +35 -0
- package/lib/instance/dev.js +258 -0
- package/lib/instance/index.d.ts +13 -0
- package/lib/instance/index.js +38 -0
- package/lib/instance/miwear.d.ts +74 -0
- package/lib/instance/miwear.js +437 -0
- package/lib/instance/preDev.d.ts +51 -0
- package/lib/instance/preDev.js +285 -0
- package/lib/static/advancedFeatures.ini +1 -0
- package/lib/static/constants.d.ts +17 -0
- package/lib/static/constants.js +25 -3
- package/lib/static/debugger_ip.cfg +1 -0
- package/lib/typing/Avd.js +0 -2
- package/lib/typing/Instance.d.ts +17 -1
- package/lib/typing/Instance.js +0 -2
- package/lib/utils/index.d.ts +13 -0
- package/lib/utils/index.js +88 -0
- package/package.json +8 -4
- package/lib/avd/index.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/instance/goldfish.d.ts +0 -35
- package/lib/instance/goldfish.js +0 -312
- package/lib/instance/goldfish.js.map +0 -1
- package/lib/static/constants.js.map +0 -1
- package/lib/typing/Avd.js.map +0 -1
- package/lib/typing/Instance.js.map +0 -1
|
@@ -0,0 +1,437 @@
|
|
|
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;
|
|
17
|
+
});
|
|
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 ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
|
|
39
|
+
const adbMiwt = __importStar(require("@miwt/adb"));
|
|
40
|
+
const child_process_1 = require("child_process");
|
|
41
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
42
|
+
const os_1 = __importDefault(require("os"));
|
|
43
|
+
const path_1 = __importDefault(require("path"));
|
|
44
|
+
const ws_1 = __importStar(require("ws"));
|
|
45
|
+
const constants_1 = require("../static/constants");
|
|
46
|
+
const utils_1 = require("../utils");
|
|
47
|
+
const common_1 = __importDefault(require("./common"));
|
|
48
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
49
|
+
const MAX_RECONNECT_COUNT = 8;
|
|
50
|
+
/**
|
|
51
|
+
* MiwearInstance
|
|
52
|
+
* 针对 Vela正式版(4.0)的镜像
|
|
53
|
+
*/
|
|
54
|
+
class MiwearInstance extends common_1.default {
|
|
55
|
+
constructor(params) {
|
|
56
|
+
super(params);
|
|
57
|
+
this.params = params;
|
|
58
|
+
this.appPathInEmulator = '/data/quickapp/app';
|
|
59
|
+
this.reconnectCount = 0;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 1. 启动模拟器
|
|
63
|
+
* 2. 启动成功后,adb连接模拟器
|
|
64
|
+
* 3. 连接成功后,在模拟器中启动快应用
|
|
65
|
+
*/
|
|
66
|
+
start(options) {
|
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
this.startOptions = options;
|
|
69
|
+
this.sn = `127.0.0.1:${this.startOptions.adbPort}`;
|
|
70
|
+
// 启动模拟器
|
|
71
|
+
yield this.startGoldfish(options);
|
|
72
|
+
// adb连接模拟器
|
|
73
|
+
const connected = yield this.connectGoldfish(120);
|
|
74
|
+
if (connected) {
|
|
75
|
+
ColorConsole_1.default.info('### Emulator ### Goldfish emulator connected successfully');
|
|
76
|
+
// 如果是首次启动,创建server端监听事件
|
|
77
|
+
if (this.isFirstStart && this.startOptions.serverPort) {
|
|
78
|
+
yield this.createWebsockeServer();
|
|
79
|
+
}
|
|
80
|
+
// 在模拟器中启动快应用
|
|
81
|
+
this.startupQuickApp(options);
|
|
82
|
+
this.isFirstStart = false;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const msg = '### Emulator ### Failed to connect emulator, please check whether the adb is normal';
|
|
86
|
+
ColorConsole_1.default.throw(msg);
|
|
87
|
+
throw new Error(msg);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 启动模拟器
|
|
93
|
+
* 1. 通过options生成模拟器的启动命令
|
|
94
|
+
* 2. 执行启动命令
|
|
95
|
+
* 3. 判断模拟器是否启动成功
|
|
96
|
+
* 3.1 若disableNSH=true,输出流中匹配到/quickapp_rpk_installer_init|rpk installer init done/,认为模拟器启动成功了
|
|
97
|
+
* 3.2 若disableNSH=false,认为8s过后模拟器启动成功了
|
|
98
|
+
* @param options
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
startGoldfish(options) {
|
|
102
|
+
var _a;
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
const { avdName, devtool, origin = 'terminal' } = options;
|
|
105
|
+
// 获取emulator bin的绝对路径
|
|
106
|
+
const emulatorBin = this.getEmulatorBinPath();
|
|
107
|
+
ColorConsole_1.default.log(`### Emulator ### emulator path: ${emulatorBin}`);
|
|
108
|
+
// 获取vela镜像的绝对路径
|
|
109
|
+
const avdInfo = this.velaAvdCls.getVelaAvdInfo(avdName);
|
|
110
|
+
const { avdArch, avdImagePath } = avdInfo;
|
|
111
|
+
ColorConsole_1.default.log(`### Emulator ### adb port: ${options.adbPort}`);
|
|
112
|
+
if (!avdImagePath) {
|
|
113
|
+
return ColorConsole_1.default.throw(`### Emulator ### Unable to find vela image via avd`);
|
|
114
|
+
}
|
|
115
|
+
const nuttxBinPath = path_1.default.resolve(avdImagePath, 'nuttx');
|
|
116
|
+
ColorConsole_1.default.log(`### Emulator ### nuttx path: ${nuttxBinPath}`);
|
|
117
|
+
// 端口映射,adb端口和debug端口
|
|
118
|
+
let portMappingStr = `-network-user-mode-options hostfwd=tcp:127.0.0.1:${options.adbPort}-10.0.2.15:5555`;
|
|
119
|
+
if (devtool) {
|
|
120
|
+
portMappingStr += `,hostfwd=tcp:127.0.0.1:${options.debugPort}-10.0.2.15:101`;
|
|
121
|
+
}
|
|
122
|
+
// 设备挂载节点
|
|
123
|
+
const systemImageBin = path_1.default.resolve(avdImagePath, 'vela_resource.bin'); // 只读
|
|
124
|
+
const dataImageBin = path_1.default.resolve(avdImagePath, 'vela_data.bin'); // 可读可写
|
|
125
|
+
const coreBin = path_1.default.resolve(avdImagePath, 'coredump.core'); // 只读
|
|
126
|
+
// 复制可写文件到AVD目录下(多模拟器实例时需要)
|
|
127
|
+
const dataImageBinInAvd = path_1.default.join(this.avdHome, `${avdName}.avd`, 'vela_data.bin');
|
|
128
|
+
if (!fs_extra_1.default.existsSync(dataImageBinInAvd)) {
|
|
129
|
+
// vela_data.bin不存在时直接copy
|
|
130
|
+
fs_extra_1.default.copyFileSync(dataImageBin, dataImageBinInAvd);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
// vela_data.bin存在时,如果.export里的时间晚于avd里的时候,说明更新了镜像,需要重新copy
|
|
134
|
+
const statsInAvd = fs_extra_1.default.statSync(dataImageBinInAvd);
|
|
135
|
+
const stats = fs_extra_1.default.statSync(dataImageBin);
|
|
136
|
+
if ((0, dayjs_1.default)(stats.mtime).isAfter(statsInAvd.mtime)) {
|
|
137
|
+
fs_extra_1.default.copyFileSync(dataImageBin, dataImageBinInAvd);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// 挂载节点配置,只读文件加入read-only标识
|
|
141
|
+
const imageMountStr = `-drive index=0,id=system,if=none,format=raw,file=${systemImageBin},read-only \
|
|
142
|
+
-device virtio-blk-device,bus=virtio-mmio-bus.0,drive=system \
|
|
143
|
+
-drive index=1,id=userdata,if=none,format=raw,file=${dataImageBinInAvd} \
|
|
144
|
+
-device virtio-blk-device,bus=virtio-mmio-bus.1,drive=userdata \
|
|
145
|
+
-drive index=2,id=vendor,if=none,format=raw,file=${coreBin},read-only \
|
|
146
|
+
-device virtio-blk-device,bus=virtio-mmio-bus.4,drive=vendor \
|
|
147
|
+
-device virtio-snd,bus=virtio-mmio-bus.2 -allow-host-audio -semihosting`;
|
|
148
|
+
// vnc配置
|
|
149
|
+
let windowStr = '';
|
|
150
|
+
let vncStr = '';
|
|
151
|
+
if ((_a = this.startOptions) === null || _a === void 0 ? void 0 : _a.vncPort) {
|
|
152
|
+
windowStr = '-no-window';
|
|
153
|
+
const portSuffix = this.startOptions.vncPort - constants_1.defaultVncPort;
|
|
154
|
+
vncStr = `-vnc :${portSuffix}`;
|
|
155
|
+
}
|
|
156
|
+
// 启动模拟器的命令和参数
|
|
157
|
+
const cmd = `${emulatorBin} -nuttx -avd ${avdName} -avd-arch ${avdArch} -show-kernel -kernel ${nuttxBinPath} ${portMappingStr} ${windowStr} -qemu ${vncStr} ${imageMountStr}`;
|
|
158
|
+
const spawnArgs = cmd.split(' ');
|
|
159
|
+
const spawnBin = spawnArgs.shift();
|
|
160
|
+
ColorConsole_1.default.log(`### Emulator ### Start CMD: ${cmd}`);
|
|
161
|
+
return new Promise((resolve) => {
|
|
162
|
+
var _a, _b, _c, _d, _e;
|
|
163
|
+
this.goldfishProcess = (0, child_process_1.spawn)(spawnBin, spawnArgs, {
|
|
164
|
+
stdio: 'pipe',
|
|
165
|
+
shell: true,
|
|
166
|
+
cwd: this.sdkHome
|
|
167
|
+
});
|
|
168
|
+
// 处理origin为terminal
|
|
169
|
+
if (origin === 'terminal') {
|
|
170
|
+
process.stdout.pipe(this.goldfishProcess.stdin);
|
|
171
|
+
(_a = this.goldfishProcess.stdout) === null || _a === void 0 ? void 0 : _a.pipe(process.stdout);
|
|
172
|
+
(_b = this.goldfishProcess.stderr) === null || _b === void 0 ? void 0 : _b.pipe(process.stderr);
|
|
173
|
+
}
|
|
174
|
+
// 监听模拟器的退出事件
|
|
175
|
+
this.goldfishProcess.on('exit', (code) => {
|
|
176
|
+
ColorConsole_1.default.error(`### Emulator ### Goldfish emulator exited with code ${code}`);
|
|
177
|
+
if (options.exitCallback) {
|
|
178
|
+
options.exitCallback(code);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
// 监听错误流
|
|
182
|
+
if (origin === 'ide') {
|
|
183
|
+
(_c = this.goldfishProcess.stderr) === null || _c === void 0 ? void 0 : _c.on('data', (data) => {
|
|
184
|
+
const stderrCb = options.stderrCallback || console.log;
|
|
185
|
+
stderrCb(data.toString());
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
// 监听输出流
|
|
189
|
+
(_e = (_d = this.goldfishProcess) === null || _d === void 0 ? void 0 : _d.stdout) === null || _e === void 0 ? void 0 : _e.on('data', (data) => {
|
|
190
|
+
const msg = data.toString();
|
|
191
|
+
if (origin === 'ide') {
|
|
192
|
+
const stdoutCb = options.stdoutCallback || console.log;
|
|
193
|
+
stdoutCb(msg);
|
|
194
|
+
// 应用安装成功,则启动它
|
|
195
|
+
// if (msg.match(/InstallState_Finished|install finished/)) {
|
|
196
|
+
// ColorConsole.info(`### Emulator ### Install quickapp successfully`)
|
|
197
|
+
// this.launchQuickapp()
|
|
198
|
+
// }
|
|
199
|
+
}
|
|
200
|
+
// 匹配到,则认为模拟器启动成功
|
|
201
|
+
if (msg.match(/quickapp_rpk_installer_init|rpk installer init done/)) {
|
|
202
|
+
ColorConsole_1.default.info(`### Emulator ### Goldfish emulator starts successfully`);
|
|
203
|
+
resolve();
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* 在模拟器中启动快应用
|
|
211
|
+
* 1. 检查release目录是否有打包好的rpk
|
|
212
|
+
* 2. 检查当前是否为调试模式.
|
|
213
|
+
* 若是,将debugger_ip.cfg推到模拟器的/data/目录下
|
|
214
|
+
* 若否,则要删除模拟器中已有的data/debugger_ip.cfg
|
|
215
|
+
* 3. 调用installRpkToAppList将当前快应用安装到模拟器的应用列表中
|
|
216
|
+
* @param options
|
|
217
|
+
*/
|
|
218
|
+
startupQuickApp(options) {
|
|
219
|
+
var _a;
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
const { package: appPackageName } = this.projectInfo;
|
|
222
|
+
const releaseDir = this.isRpk
|
|
223
|
+
? path_1.default.resolve(this.projectPath, '../')
|
|
224
|
+
: path_1.default.resolve(this.projectPath, ((_a = this.params.compilerOption) === null || _a === void 0 ? void 0 : _a.releasePath) || 'dist');
|
|
225
|
+
const files = fs_extra_1.default
|
|
226
|
+
.readdirSync(releaseDir)
|
|
227
|
+
.filter((item) => item.includes(appPackageName) && item.endsWith('.rpk'));
|
|
228
|
+
if (files.length === 0) {
|
|
229
|
+
ColorConsole_1.default.error(`### Emulator the rpk does not exist`);
|
|
230
|
+
}
|
|
231
|
+
const rpkPath = path_1.default.resolve(releaseDir, files[0]);
|
|
232
|
+
// 调试模式需要push一个文件至miwear中
|
|
233
|
+
if (options.devtool) {
|
|
234
|
+
const debuggerCfgFile = path_1.default.join(__dirname, '../static/debugger_ip.cfg');
|
|
235
|
+
yield adbMiwt.execAdbCmdAsync(`adb -s ${this.sn} push ${debuggerCfgFile} /data/debugger_ip.cfg`);
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
adbMiwt.execAdbCmdAsync(`adb -s ${this.sn} shell rm /data/debugger_ip.cfg`);
|
|
239
|
+
}
|
|
240
|
+
this.installRpkToAppList(rpkPath, this.appPathInEmulator);
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 将快应用安装到模拟器的应用列表
|
|
245
|
+
* 1. 使用adb push将打包好的rpk推到模拟器的/data/quickapp/app/
|
|
246
|
+
* 2. nsh中调用pm install命令安装应用
|
|
247
|
+
* @param rpkPath rpk的绝对目录
|
|
248
|
+
* @param targetDir 要将rpk放到模拟器的哪个目录下
|
|
249
|
+
*/
|
|
250
|
+
installRpkToAppList(rpkPath, targetDir) {
|
|
251
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
252
|
+
try {
|
|
253
|
+
const { package: packageName } = this.projectInfo;
|
|
254
|
+
// 1. adb push应用的rpk
|
|
255
|
+
const targetPath = `${targetDir}/${packageName}.rpk`;
|
|
256
|
+
const pushCmd = `adb -s ${this.sn} push "${rpkPath}" ${targetPath}`;
|
|
257
|
+
ColorConsole_1.default.info(`### Emulator ### Excuting cmd: ${pushCmd}`);
|
|
258
|
+
yield adbMiwt.execAdbCmdAsync(pushCmd);
|
|
259
|
+
yield (0, utils_1.sleep)(1000);
|
|
260
|
+
// 2. 安装应用(pm install时如何应用重名会覆盖)
|
|
261
|
+
const installCmd = `adb -s ${this.sn} shell pm install ${targetPath}`;
|
|
262
|
+
ColorConsole_1.default.info(`### Emulator ### Excuting cmd: ${installCmd}`);
|
|
263
|
+
yield adbMiwt.execAdbCmdAsync(installCmd);
|
|
264
|
+
// 3. 应用安装成功后,启动应用
|
|
265
|
+
const lsCmd = `adb -s ${this.sn} shell ls ${this.appPathInEmulator}`;
|
|
266
|
+
let installed = false;
|
|
267
|
+
let currCount = 0;
|
|
268
|
+
while (!installed && currCount < 10) {
|
|
269
|
+
yield (0, utils_1.sleep)(1000);
|
|
270
|
+
const res = yield adbMiwt.execAdbCmdAsync(lsCmd);
|
|
271
|
+
// 如果没有上传的rpk了,则表示安装成功
|
|
272
|
+
installed = !res.includes(`${packageName}.rpk`);
|
|
273
|
+
currCount++;
|
|
274
|
+
}
|
|
275
|
+
if (installed) {
|
|
276
|
+
this.launchQuickapp();
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
ColorConsole_1.default.error(`### Emulator ### install ${packageName} failed`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
catch (e) {
|
|
283
|
+
ColorConsole_1.default.error(`### Emulator ### ${e.message}`);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
/** 使用am start启动快应用 */
|
|
288
|
+
launchQuickapp() {
|
|
289
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
290
|
+
const { package: packageName } = this.projectInfo;
|
|
291
|
+
const startCmd = `adb -s ${this.sn} shell am start ${packageName}`;
|
|
292
|
+
ColorConsole_1.default.info(`### Emulator ### Excuting cmd: ${startCmd}`);
|
|
293
|
+
adbMiwt.execAdbCmdAsync(startCmd);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
/** 连接模拟器中的调试服务,创建debugSocket
|
|
297
|
+
* 主要用于热更新时,通过debugSocket通知调试服务更新页面
|
|
298
|
+
* 设置了重连机制,会重复连接8次
|
|
299
|
+
*/
|
|
300
|
+
initDebugSocket() {
|
|
301
|
+
return new Promise((resolve, reject) => {
|
|
302
|
+
var _a;
|
|
303
|
+
if (this.debugSocket && this.debugSocket.OPEN) {
|
|
304
|
+
return resolve();
|
|
305
|
+
}
|
|
306
|
+
this.debugSocket = new ws_1.default(`ws://localhost:${(_a = this.startOptions) === null || _a === void 0 ? void 0 : _a.debugPort}`);
|
|
307
|
+
this.debugSocket.onopen = () => {
|
|
308
|
+
ColorConsole_1.default.info(`### Emulator debugSocket connect success`);
|
|
309
|
+
return resolve();
|
|
310
|
+
};
|
|
311
|
+
this.debugSocket.onerror = (errorEvent) => {
|
|
312
|
+
var _a;
|
|
313
|
+
// 重连机制
|
|
314
|
+
(_a = this.debugSocket) === null || _a === void 0 ? void 0 : _a.terminate();
|
|
315
|
+
if (this.reconnectCount < MAX_RECONNECT_COUNT) {
|
|
316
|
+
ColorConsole_1.default.info(`### Emulator the ${this.reconnectCount + 1}th time to reconnect debug server`);
|
|
317
|
+
this.reconnectCount++;
|
|
318
|
+
setTimeout(() => this.initDebugSocket(), 2000);
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
this.debugSocket = undefined;
|
|
322
|
+
this.reconnectCount = 0;
|
|
323
|
+
return reject(`### Emulator debugSocket connect failed, ${errorEvent.message}`);
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
this.debugSocket.onclose = (closeEvent) => {
|
|
327
|
+
this.debugSocket = undefined;
|
|
328
|
+
this.reconnectCount = 0;
|
|
329
|
+
ColorConsole_1.default.log(`### Emulator debugSocket connect close: ${closeEvent.reason}`);
|
|
330
|
+
};
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/** 通知模拟器更新
|
|
334
|
+
* 1. 确保已经成功连接模拟器中的调试服务
|
|
335
|
+
* 2. 使用adb push将build文件下的内容推到/data/quickapp/app/${packageName}
|
|
336
|
+
* 3. 发送Page.reload命令给调试服务,通知更新
|
|
337
|
+
*/
|
|
338
|
+
handleUpdate() {
|
|
339
|
+
var _a, _b;
|
|
340
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
341
|
+
try {
|
|
342
|
+
this.reconnectCount = 0;
|
|
343
|
+
yield this.initDebugSocket();
|
|
344
|
+
// 1. 将整包重新推到miwear中(TODO:增量更新)
|
|
345
|
+
yield this.pushBuild();
|
|
346
|
+
// 2. 下发CDP命令给调试服务告知刷新
|
|
347
|
+
if (((_a = this.debugSocket) === null || _a === void 0 ? void 0 : _a.readyState) === ws_1.default.OPEN) {
|
|
348
|
+
(_b = this.debugSocket) === null || _b === void 0 ? void 0 : _b.send(JSON.stringify({ id: 10000, method: 'Page.reload', params: {} }));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch (e) {
|
|
352
|
+
ColorConsole_1.default.error(`${e}`);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
/** 热更新时push目录 */
|
|
357
|
+
pushBuild() {
|
|
358
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
+
const { package: appPackageName } = this.projectInfo;
|
|
360
|
+
// windows平台adb push时无法使用通配符,需另外处理;
|
|
361
|
+
// 项目路径包含空格时,无法通过加引号来使用 * 通配符
|
|
362
|
+
if (os_1.default.platform() === 'win32' || this.projectPath.indexOf(' ') > 0) {
|
|
363
|
+
const rmCmd = `adb -s ${this.sn} shell rm -r ${this.appPathInEmulator}/${appPackageName}`;
|
|
364
|
+
yield adbMiwt.execAdbCmdAsync(rmCmd);
|
|
365
|
+
const sourcePath = path_1.default.resolve(this.projectPath, './build');
|
|
366
|
+
const pushCmd = `adb -s ${this.sn} push "${sourcePath}" ${this.appPathInEmulator}/${appPackageName}`;
|
|
367
|
+
yield adbMiwt.execAdbCmdAsync(pushCmd);
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
const sourcePath = path_1.default.resolve(this.projectPath, './build/*');
|
|
371
|
+
yield adbMiwt.execAdbCmdAsync(`adb -s ${this.sn} push ${sourcePath} ${this.appPathInEmulator}/${appPackageName}`);
|
|
372
|
+
}
|
|
373
|
+
ColorConsole_1.default.info(`### Emulator push to ${this.appPathInEmulator}/${appPackageName} successfully`);
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
/** 在模拟器中重启快应用(基于am命令,需要保证镜像中已经有am功能)
|
|
377
|
+
* 1. 使用adb push将build文件下的内容推到/data/quickapp/app/${packageName}
|
|
378
|
+
* 2. nsh中执行am stop命令退出快应用
|
|
379
|
+
* 3. nsh中执行am start命令启动快应用
|
|
380
|
+
*/
|
|
381
|
+
reloadApp() {
|
|
382
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
383
|
+
try {
|
|
384
|
+
// 1. 将整包重新推到miwear中(TODO:增量更新)
|
|
385
|
+
const { package: appPackageName } = this.projectInfo;
|
|
386
|
+
yield this.pushBuild();
|
|
387
|
+
// 2. 执行am stop和am start命令
|
|
388
|
+
const stopCmd = `adb -s ${this.sn} shell am stop ${appPackageName}`;
|
|
389
|
+
yield adbMiwt.execAdbCmdAsync(stopCmd);
|
|
390
|
+
ColorConsole_1.default.info(`### Emulator stop ${appPackageName} successfully`);
|
|
391
|
+
// 这里是为了等am stop命令清除资源等
|
|
392
|
+
yield (0, utils_1.sleep)(500);
|
|
393
|
+
const startCmd = `adb -s ${this.sn} shell am start ${appPackageName}`;
|
|
394
|
+
yield adbMiwt.execAdbCmdAsync(startCmd);
|
|
395
|
+
ColorConsole_1.default.info(`### Emulator start ${appPackageName} successfully`);
|
|
396
|
+
}
|
|
397
|
+
catch (e) {
|
|
398
|
+
ColorConsole_1.default.error(`${e}`);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* 创建server端,监听打包过程中client端发来的消息
|
|
404
|
+
*/
|
|
405
|
+
createWebsockeServer() {
|
|
406
|
+
var _a;
|
|
407
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
408
|
+
const wsServer = new ws_1.WebSocketServer({
|
|
409
|
+
port: (_a = this.startOptions) === null || _a === void 0 ? void 0 : _a.serverPort
|
|
410
|
+
});
|
|
411
|
+
wsServer.on('connection', (socket) => {
|
|
412
|
+
ColorConsole_1.default.success(`### App Socket server ### Websocket connects to websocket server`);
|
|
413
|
+
socket.on('error', (err) => {
|
|
414
|
+
ColorConsole_1.default.error(`### App Socket server ### Websocket server error: ${err.message}`);
|
|
415
|
+
});
|
|
416
|
+
// data的格式:{ type: string, data: any }
|
|
417
|
+
socket.on('message', (data) => __awaiter(this, void 0, void 0, function* () {
|
|
418
|
+
var _a;
|
|
419
|
+
const message = JSON.parse(data.toString());
|
|
420
|
+
ColorConsole_1.default.log(`### App Socket server ### Websocket server get data: ${data}`);
|
|
421
|
+
if (message.type === 'restart') {
|
|
422
|
+
// 非调试模式下无法热更新
|
|
423
|
+
if (!((_a = this.startOptions) === null || _a === void 0 ? void 0 : _a.devtool)) {
|
|
424
|
+
this.restart();
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
this.handleUpdate();
|
|
428
|
+
}
|
|
429
|
+
else if (message.type === 'stop') {
|
|
430
|
+
this.stop();
|
|
431
|
+
}
|
|
432
|
+
}));
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
exports.default = MiwearInstance;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { ChildProcess } from 'child_process';
|
|
3
|
+
import { INewGoldfishInstanceParams, IStartOptions } from '../typing/Instance';
|
|
4
|
+
import CommonInstance from './common';
|
|
5
|
+
/**
|
|
6
|
+
* OldGoldfishInstance
|
|
7
|
+
* 针对 Vela开发版(dev, 0.0.2)的镜像
|
|
8
|
+
*/
|
|
9
|
+
declare class OldGoldfishInstance extends CommonInstance {
|
|
10
|
+
private host9pPort;
|
|
11
|
+
v9fsProcess: ChildProcess | undefined;
|
|
12
|
+
constructor(params: INewGoldfishInstanceParams);
|
|
13
|
+
/**
|
|
14
|
+
* 1. 启动9p server
|
|
15
|
+
* 2. 将打包好的rpk推到host的挂载目录
|
|
16
|
+
* 3. 启动模拟器
|
|
17
|
+
* 4. 模拟器启动成功后,adb连接模拟器
|
|
18
|
+
* 5. 连接成功后,在模拟器中启动快应用
|
|
19
|
+
*/
|
|
20
|
+
start(options: IStartOptions): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* 在模拟器中启动快应用(快应用都在模拟器的/data/app目录下)
|
|
23
|
+
* 1. 是否为调试模式
|
|
24
|
+
* 若是,在模拟器终端执行vapp app/${packageName} &
|
|
25
|
+
* 若否,在模拟器终端执行vapp --jsdebugger=10.0.2.15:101 app/${packageName} &
|
|
26
|
+
*/
|
|
27
|
+
startupQuickApp(options: IStartOptions): void;
|
|
28
|
+
/** host启动9pServer
|
|
29
|
+
* 作用是将本地的quickappMountDir目录挂载到模拟器的/data目录
|
|
30
|
+
*/
|
|
31
|
+
ensure9pServerRunnning(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* 启动模拟器
|
|
34
|
+
* 1. 通过options生成模拟器的启动命令
|
|
35
|
+
* 2. 执行启动命令
|
|
36
|
+
* 3. 判断模拟器是否启动成功
|
|
37
|
+
* 3.1 若disableNSH=true,输出流中匹配到/quickapp_rpk_installer_init|rpk installer init done/,认为模拟器启动成功了
|
|
38
|
+
* 3.2 若disableNSH=false,认为8s过后模拟器启动成功了
|
|
39
|
+
* @param options
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
startGoldfish(options: IStartOptions): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* 推送文件到本地的${sdkHome}/qa/app/${packageName}目录
|
|
45
|
+
* @param sourceRoot 源目录
|
|
46
|
+
*/
|
|
47
|
+
pushRpk(sourceRoot: string): Promise<void>;
|
|
48
|
+
/** 停止模拟器并释放相关资源 */
|
|
49
|
+
stop(): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
export default OldGoldfishInstance;
|