@aiot-toolkit/emulator 2.0.2-beta.1 → 2.0.2-beta.10

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.
@@ -38,47 +38,66 @@ Object.defineProperty(exports, "__esModule", { value: true });
38
38
  const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
39
39
  const adbMiwt = __importStar(require("@miwt/adb"));
40
40
  const child_process_1 = require("child_process");
41
- const path_1 = __importDefault(require("path"));
42
- const portfinder_1 = __importDefault(require("portfinder"));
41
+ const path_1 = require("path");
43
42
  const constants_1 = require("../static/constants");
44
43
  const common_1 = __importDefault(require("./common"));
44
+ const os_1 = __importDefault(require("os"));
45
+ const fs_1 = __importDefault(require("fs"));
46
+ const dayjs_1 = __importDefault(require("dayjs"));
45
47
  class GoldfishInstance extends common_1.default {
46
48
  constructor(params) {
47
49
  super(params);
48
50
  this.appRunDir = '/data/app';
49
51
  }
50
- /** 在goldfish模拟器中运行快应用 */
52
+ /**
53
+ * 1. 启动模拟器
54
+ * 2. 启动成功后,adb连接模拟器
55
+ * 3. 连接成功后,在模拟器中启动快应用
56
+ */
51
57
  start(options) {
52
58
  return __awaiter(this, void 0, void 0, function* () {
53
59
  this.startOptions = options;
60
+ this.sn = `127.0.0.1:${this.startOptions.adbPort}`;
54
61
  // 启动模拟器
55
62
  yield this.startGoldfish(options);
56
- const connected = yield this.connectGoldfish();
63
+ const connected = yield this.connectGoldfish(120);
57
64
  if (connected) {
58
65
  ColorConsole_1.default.log('### Emulator ### Goldfish emulator connected successfully');
59
66
  if (this.isFirstStart && this.startOptions.serverPort) {
60
67
  yield this.createWebsockeServer();
61
68
  }
62
69
  // adb push快应用到模拟器的/data/app目录
63
- const buildedFilesPath = this.isRpk ? this.projectPath : path_1.default.resolve(this.projectPath, './build');
70
+ // aspect应用后续要考虑各种形状表盘的推包
71
+ const buildedFilesPath = this.isRpk
72
+ ? this.projectPath
73
+ : this.isDistributedApp
74
+ ? options.dist ||
75
+ path_1.posix.resolve(this.projectPath, './build', `${this.projectInfo.package}.watch`)
76
+ : path_1.posix.resolve(this.projectPath, './build');
64
77
  yield this.pushRpk(buildedFilesPath);
65
78
  // 在模拟器中启动快应用
66
79
  this.startupQuickApp(options);
67
80
  this.isFirstStart = false;
68
81
  }
69
82
  else {
70
- ColorConsole_1.default.throw('### Emulator ### Failed to connect emulator, please check whether the adb is normal');
83
+ const msg = '### Emulator ### Failed to connect emulator, please check whether the adb is normal';
84
+ ColorConsole_1.default.throw(msg);
85
+ throw new Error(msg);
71
86
  }
72
87
  });
73
88
  }
74
- /** 在goldfish中启动快应用 */
89
+ /**
90
+ * 在模拟器中启动快应用
91
+ * 通过vapp命令启动,调试时需额外配置--jsdebugger参数
92
+ * @param options
93
+ */
75
94
  startupQuickApp(options) {
76
95
  return __awaiter(this, void 0, void 0, function* () {
77
96
  try {
78
97
  const { package: packageName } = this.projectInfo;
79
- let vappCmd = `adb -s 127.0.0.1:${this.adbPort} shell vapp app/${packageName} &`;
98
+ let vappCmd = `adb -s 127.0.0.1:${options.adbPort} shell vapp app/${packageName} &`;
80
99
  if (options.devtool) {
81
- vappCmd = `adb -s 127.0.0.1:${this.adbPort} shell vapp --jsdebugger=10.0.2.15:101 app/${packageName} &`;
100
+ vappCmd = `adb -s 127.0.0.1:${options.adbPort} shell vapp --jsdebugger=10.0.2.15:101 app/${packageName} &`;
82
101
  }
83
102
  ColorConsole_1.default.log(`### Emulator ### Excuting adb cmd: ${vappCmd}`);
84
103
  // vapp进程会一直pending,不会退出。这里必须加stdio: 'ignore',否则快应用无法运行成功
@@ -89,33 +108,82 @@ class GoldfishInstance extends common_1.default {
89
108
  }
90
109
  });
91
110
  }
92
- /** 启动goldfish模拟器 */
111
+ /**
112
+ * 启动模拟器
113
+ * 1. 通过options生成模拟器的启动命令
114
+ * 2. 执行启动命令
115
+ * 3. 判断模拟器是否启动成功
116
+ * 3.1 若disableNSH=true,输出流中匹配到(NSH),认为模拟器启动成功了
117
+ * 3.2 若disableNSH=false,认为2s过后模拟器启动成功了
118
+ * @param options
119
+ * @returns
120
+ */
93
121
  startGoldfish(options) {
94
122
  var _a;
95
123
  return __awaiter(this, void 0, void 0, function* () {
96
- const { avdName, devtool } = options;
124
+ const { avdName, devtool, origin = 'terminal' } = options;
97
125
  const emulatorBin = this.getEmulatorBinPath();
98
126
  ColorConsole_1.default.log(`### Emulator ### emulator path: ${emulatorBin}`);
99
127
  const avdInfo = this.velaAvdCls.getVelaAvdInfo(avdName);
100
128
  const { avdArch, avdImagePath } = avdInfo;
101
- this.adbPort = yield portfinder_1.default.getPortPromise({ port: this.adbPort });
102
- ColorConsole_1.default.log(`### Emulator ### adb port: ${this.adbPort}`);
129
+ ColorConsole_1.default.log(`### Emulator ### adb port: ${options.adbPort}`);
103
130
  if (!avdImagePath) {
104
131
  return ColorConsole_1.default.throw(`### Emulator ### Unable to find vela image via avd`);
105
132
  }
106
- const nuttxBinPath = path_1.default.resolve(avdImagePath, 'nuttx');
133
+ const nuttxBinPath = path_1.posix.resolve(avdImagePath, 'nuttx');
107
134
  ColorConsole_1.default.log(`### Emulator ### nuttx path: ${nuttxBinPath}`);
108
135
  // 端口映射
109
- let portMappingStr = `-network-user-mode-options hostfwd=tcp:127.0.0.1:${this.adbPort}-10.0.2.15:5555`;
136
+ let portMappingStr = `-network-user-mode-options hostfwd=tcp:127.0.0.1:${options.adbPort}-10.0.2.15:5555`;
110
137
  if (devtool) {
111
- portMappingStr += `,hostfwd=tcp:127.0.0.1:${this.debugPort}-10.0.2.15:101`;
138
+ portMappingStr += `,hostfwd=tcp:127.0.0.1:${options.debugPort}-10.0.2.15:101`;
112
139
  }
113
140
  // 文件系统配置,第一次使用fatfs镜像挂载,后续使用adb push更新应用
114
- const systemImageBin = path_1.default.resolve(this.sdkHome, 'tools/image/system.img');
115
- const dataImageBin = path_1.default.resolve(this.sdkHome, 'tools/image/data.img');
116
- const imageMountStr = `-drive index=0,id=system,if=none,format=raw,file=${systemImageBin} \
141
+ const systemImageBin = path_1.posix.resolve(avdImagePath, 'system.img');
142
+ const dataImageBin = path_1.posix.resolve(avdImagePath, 'data.img');
143
+ // 复制可写文件到AVD目录下(多模拟器实例时需要)
144
+ const dataImageBinInAvd = path_1.posix.join(this.avdHome, `${avdName}.avd`, 'data.img');
145
+ if (!fs_1.default.existsSync(dataImageBinInAvd)) {
146
+ // data.img不存在时直接copy
147
+ fs_1.default.copyFileSync(dataImageBin, dataImageBinInAvd);
148
+ }
149
+ else {
150
+ // data.img存在时,如果.export里的时间晚于avd里的时候,说明更新了镜像,需要重新copy
151
+ const statsInAvd = fs_1.default.statSync(dataImageBinInAvd);
152
+ const stats = fs_1.default.statSync(dataImageBin);
153
+ if ((0, dayjs_1.default)(stats.mtime).isAfter(statsInAvd.mtime)) {
154
+ fs_1.default.copyFileSync(dataImageBin, dataImageBinInAvd);
155
+ }
156
+ }
157
+ // 复制可写文件到AVD目录下(多模拟器实例时需要)
158
+ const systemImageBinInAvd = path_1.posix.join(this.avdHome, `${avdName}.avd`, 'system.img');
159
+ if (!fs_1.default.existsSync(systemImageBinInAvd)) {
160
+ // data.img不存在时直接copy
161
+ fs_1.default.copyFileSync(systemImageBin, systemImageBinInAvd);
162
+ }
163
+ else {
164
+ // data.img存在时,如果.export里的时间晚于avd里的时候,说明更新了镜像,需要重新copy
165
+ const statsInAvd = fs_1.default.statSync(systemImageBinInAvd);
166
+ const stats = fs_1.default.statSync(systemImageBin);
167
+ if ((0, dayjs_1.default)(stats.mtime).isAfter(statsInAvd.mtime)) {
168
+ fs_1.default.copyFileSync(systemImageBin, systemImageBinInAvd);
169
+ }
170
+ }
171
+ // 复制可写文件到AVD目录下(多模拟器实例时需要)
172
+ // const nuttxBinInAvd = path.join(this.avdHome, `${avdName}.avd`, 'nuttx')
173
+ // if (!fs.existsSync(nuttxBinInAvd)) {
174
+ // // data.img不存在时直接copy
175
+ // fs.copyFileSync(nuttxBinPath, nuttxBinInAvd)
176
+ // } else {
177
+ // // data.img存在时,如果.export里的时间晚于avd里的时候,说明更新了镜像,需要重新copy
178
+ // const statsInAvd = fs.statSync(nuttxBinInAvd)
179
+ // const stats = fs.statSync(nuttxBinPath)
180
+ // if (dayjs(stats.mtime).isAfter(statsInAvd.mtime)) {
181
+ // fs.copyFileSync(nuttxBinPath, nuttxBinInAvd)
182
+ // }
183
+ // }
184
+ const imageMountStr = `-drive index=0,id=system,if=none,format=raw,file=${systemImageBinInAvd} \
117
185
  -device virtio-blk-device,bus=virtio-mmio-bus.0,drive=system \
118
- -drive index=1,id=userdata,if=none,format=raw,file=${dataImageBin} \
186
+ -drive index=1,id=userdata,if=none,format=raw,file=${dataImageBinInAvd} \
119
187
  -device virtio-blk-device,bus=virtio-mmio-bus.1,drive=userdata \
120
188
  -device virtio-snd,bus=virtio-mmio-bus.2 -allow-host-audio -semihosting`;
121
189
  // vnc配置
@@ -126,38 +194,21 @@ class GoldfishInstance extends common_1.default {
126
194
  const portSuffix = this.startOptions.vncPort - constants_1.defaultVncPort;
127
195
  vncStr = `-vnc :${portSuffix}`;
128
196
  }
129
- const stdioType = options.disableNSH ? 'pipe' : 'inherit';
130
197
  // 启动goldfish的命令和参数
131
198
  const cmd = `${emulatorBin} -nuttx -avd ${avdName} -avd-arch ${avdArch} -show-kernel -kernel ${nuttxBinPath} ${portMappingStr} ${windowStr} -qemu ${vncStr} ${imageMountStr}`;
132
199
  const spawnArgs = cmd.split(' ');
133
200
  const spawnBin = spawnArgs.shift();
134
201
  ColorConsole_1.default.log(`### Emulator ### Start CMD: ${cmd}`);
135
202
  return new Promise((resolve) => {
136
- var _a, _b;
137
- this.goldfishProcess = (0, child_process_1.spawn)(spawnBin, spawnArgs, { stdio: stdioType, shell: true });
138
- if (options.disableNSH) {
139
- (_a = this.goldfishProcess.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
140
- if (options.stdoutCallback) {
141
- options.stdoutCallback(data);
142
- }
143
- else {
144
- console.log(data.toString());
145
- }
146
- if (data.toString().includes('(NSH)')) {
147
- ColorConsole_1.default.log(`### Emulator ### Goldfish emulator starts successfully`);
148
- resolve();
149
- }
150
- });
151
- (_b = this.goldfishProcess.stderr) === null || _b === void 0 ? void 0 : _b.on('data', (data) => {
152
- const stderrCb = options.stderrCallback || console.log;
153
- stderrCb(data.toString());
154
- });
155
- }
156
- else {
157
- setTimeout(() => {
158
- ColorConsole_1.default.log(`### Emulator ### Goldfish emulator starts successfully`);
159
- resolve();
160
- }, 2000);
203
+ var _a, _b, _c;
204
+ this.goldfishProcess = (0, child_process_1.spawn)(spawnBin, spawnArgs, { stdio: 'pipe', shell: true });
205
+ (_a = this.goldfishProcess.stderr) === null || _a === void 0 ? void 0 : _a.on('data', (data) => {
206
+ const stderrCb = options.stderrCallback || console.log;
207
+ stderrCb(data.toString());
208
+ });
209
+ if (origin === 'terminal') {
210
+ process.stdout.pipe(this.goldfishProcess.stdin);
211
+ (_b = this.goldfishProcess.stdout) === null || _b === void 0 ? void 0 : _b.pipe(process.stdout);
161
212
  }
162
213
  this.goldfishProcess.on('exit', (code) => {
163
214
  ColorConsole_1.default.error(`### Emulator ### Goldfish emulator exited with code ${code}`);
@@ -165,19 +216,42 @@ class GoldfishInstance extends common_1.default {
165
216
  options.exitCallback(code);
166
217
  }
167
218
  });
219
+ (_c = this.goldfishProcess.stdout) === null || _c === void 0 ? void 0 : _c.on('data', (data) => {
220
+ const msg = data.toString();
221
+ if (origin === 'ide') {
222
+ const stdoutCb = options.stdoutCallback || console.log;
223
+ stdoutCb(msg);
224
+ }
225
+ if (msg.includes('(NSH)')) {
226
+ ColorConsole_1.default.log(`### Emulator ### Goldfish emulator starts successfully`);
227
+ resolve();
228
+ }
229
+ });
168
230
  });
169
231
  });
170
232
  }
171
- /** 将打包后的文件推到挂载的快应用目录 */
233
+ /**
234
+ * 将目录通过adb push到模拟器中
235
+ * @param sourceRoot
236
+ */
172
237
  pushRpk(sourceRoot) {
173
238
  return __awaiter(this, void 0, void 0, function* () {
174
- const sn = `127.0.0.1:${this.adbPort}`;
239
+ const sn = `127.0.0.1:${this.startOptions.adbPort}`;
175
240
  const { package: appPackageName } = this.projectInfo;
176
- const sourcePath = path_1.default.resolve(sourceRoot, './*');
177
- ColorConsole_1.default.log(`### Emulator ### Pushing ${appPackageName} to ${this.appRunDir}`);
178
- yield adbMiwt.execAdbCmdAsync(`adb -s ${sn} shell mkdir ${this.appRunDir}/${appPackageName}`);
179
- yield adbMiwt.execAdbCmdAsync(`adb -s ${sn} push ${sourcePath} ${this.appRunDir}/${appPackageName}`);
180
- ColorConsole_1.default.log(`### Emulator ### Push ${appPackageName} to ${this.appRunDir} successfully`);
241
+ // 获取最后一层目录,比如build
242
+ const basename = path_1.posix.basename(sourceRoot);
243
+ if (os_1.default.platform() === 'win32' || this.projectPath.indexOf(' ') > 0) {
244
+ // windows系统或者项目路径有空格:1. adb push目录;2. 在模拟器中使用mv命令重命名
245
+ yield adbMiwt.execAdbCmdAsync(`adb -s ${sn} push ${sourceRoot} ${this.appRunDir}`);
246
+ yield adbMiwt.execAdbCmdAsync(`adb -s ${sn} shell mv ${this.appRunDir}/${basename} ${this.appRunDir}/${appPackageName}`);
247
+ }
248
+ else {
249
+ // 支持通配符处理: 1. 模拟器中mkdir创建目录 2. adb push ./XXXXX/* /XXX
250
+ const sourcePath = path_1.posix.resolve(sourceRoot, './*');
251
+ yield adbMiwt.execAdbCmdAsync(`adb -s ${sn} shell mkdir ${this.appRunDir}/${appPackageName}`);
252
+ yield adbMiwt.execAdbCmdAsync(`adb -s ${sn} push ${sourcePath} ${this.appRunDir}/${appPackageName}`);
253
+ }
254
+ ColorConsole_1.default.info(`### Emulator push to ${this.appRunDir}/${appPackageName} successfully`);
181
255
  });
182
256
  }
183
257
  }
@@ -3,5 +3,11 @@ import CommonInstance from './common';
3
3
  import GoldfishInstance from './dev';
4
4
  import MiwearInstance from "./miwear";
5
5
  import OldGoldfishInstance from './preDev';
6
+ /**
7
+ * 根据镜像决定使用哪个instance
8
+ * Vela正式版(4.0) -> MiwearInstance
9
+ * Vela开发版(dev, 0.0.2) -> OldGoldfishInstance
10
+ * Vela开发版(dev),除0.0.2的其他版本 -> GoldfishInstance
11
+ */
6
12
  declare function findInstance(avdName: string, params: INewGoldfishInstanceParams): GoldfishInstance | MiwearInstance | OldGoldfishInstance | undefined;
7
13
  export { CommonInstance, GoldfishInstance, MiwearInstance, OldGoldfishInstance, findInstance };
@@ -14,6 +14,12 @@ const miwear_1 = __importDefault(require("./miwear"));
14
14
  exports.MiwearInstance = miwear_1.default;
15
15
  const preDev_1 = __importDefault(require("./preDev"));
16
16
  exports.OldGoldfishInstance = preDev_1.default;
17
+ /**
18
+ * 根据镜像决定使用哪个instance
19
+ * Vela正式版(4.0) -> MiwearInstance
20
+ * Vela开发版(dev, 0.0.2) -> OldGoldfishInstance
21
+ * Vela开发版(dev),除0.0.2的其他版本 -> GoldfishInstance
22
+ */
17
23
  function findInstance(avdName, params) {
18
24
  const { sdkHome, avdHome } = params;
19
25
  const { avdImagePath } = new avd_1.default({ sdkHome, avdHome }).getVelaAvdInfo(avdName);
@@ -2,27 +2,73 @@ import { INewGoldfishInstanceParams, IStartOptions } from '../typing/Instance';
2
2
  import CommonInstance from './common';
3
3
  /**
4
4
  * MiwearInstance
5
- * 针对 vela4.0 的镜像
5
+ * 针对 Vela正式版(4.0)的镜像
6
6
  */
7
7
  declare class MiwearInstance extends CommonInstance {
8
+ private params;
8
9
  private appPathInEmulator;
9
10
  private debugSocket?;
10
11
  private reconnectCount;
11
12
  constructor(params: INewGoldfishInstanceParams);
12
- /** 在goldfish模拟器中运行快应用 */
13
+ /**
14
+ * 1. 启动模拟器
15
+ * 2. 启动成功后,adb连接模拟器
16
+ * 3. 连接成功后,在模拟器中启动快应用
17
+ */
13
18
  start(options: IStartOptions): Promise<void>;
14
- /** 启动goldfish模拟器 */
19
+ /**
20
+ * 启动模拟器
21
+ * 1. 通过options生成模拟器的启动命令
22
+ * 2. 执行启动命令
23
+ * 3. 判断模拟器是否启动成功
24
+ * 3.1 若disableNSH=true,输出流中匹配到/quickapp_rpk_installer_init|rpk installer init done/,认为模拟器启动成功了
25
+ * 3.2 若disableNSH=false,认为8s过后模拟器启动成功了
26
+ * @param options
27
+ * @returns
28
+ */
15
29
  startGoldfish(options: IStartOptions): Promise<void>;
16
- /** 通过adb连接模拟器 */
17
- connectGoldfish(): Promise<boolean>;
18
- /** 在goldfish中启动快应用 */
30
+ /**
31
+ * 在模拟器中启动快应用
32
+ * 1. 检查release目录是否有打包好的rpk
33
+ * 2. 检查当前是否为调试模式.
34
+ * 若是,将debugger_ip.cfg推到模拟器的/data/目录下
35
+ * 若否,则要删除模拟器中已有的data/debugger_ip.cfg
36
+ * 3. 调用installRpkToAppList将当前快应用安装到模拟器的应用列表中
37
+ * @param options
38
+ */
19
39
  startupQuickApp(options: IStartOptions): Promise<void>;
20
- /** 将快应用安装到应用列表 */
40
+ /**
41
+ * 将快应用安装到模拟器的应用列表
42
+ * 1. 使用adb push将打包好的rpk推到模拟器的/data/quickapp/app/
43
+ * 2. nsh中调用pm install命令安装应用
44
+ * @param rpkPath rpk的绝对目录
45
+ * @param targetDir 要将rpk放到模拟器的哪个目录下
46
+ */
21
47
  installRpkToAppList(rpkPath: string, targetDir: string): Promise<void>;
22
- initDebugSocket(): Promise<void> | undefined;
23
- /** 通知模拟器更新 */
48
+ /** 使用am start启动快应用 */
49
+ launchQuickapp(): Promise<void>;
50
+ /** 连接模拟器中的调试服务,创建debugSocket
51
+ * 主要用于热更新时,通过debugSocket通知调试服务更新页面
52
+ * 设置了重连机制,会重复连接8次
53
+ */
54
+ initDebugSocket(): Promise<void>;
55
+ /** 通知模拟器更新
56
+ * 1. 确保已经成功连接模拟器中的调试服务
57
+ * 2. 使用adb push将build文件下的内容推到/data/quickapp/app/${packageName}
58
+ * 3. 发送Page.reload命令给调试服务,通知更新
59
+ */
24
60
  handleUpdate(): Promise<void>;
25
- /** 创建server */
61
+ /** 热更新时push目录 */
62
+ pushBuild(): Promise<void>;
63
+ /** 在模拟器中重启快应用(基于am命令,需要保证镜像中已经有am功能)
64
+ * 1. 使用adb push将build文件下的内容推到/data/quickapp/app/${packageName}
65
+ * 2. nsh中执行am stop命令退出快应用
66
+ * 3. nsh中执行am start命令启动快应用
67
+ */
68
+ reloadApp(): Promise<void>;
69
+ /**
70
+ * 创建server端,监听打包过程中client端发来的消息
71
+ */
26
72
  createWebsockeServer(): Promise<void>;
27
73
  }
28
74
  export default MiwearInstance;