aiot-toolkit 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.
package/lib/bin.js CHANGED
@@ -196,7 +196,14 @@ function main() {
196
196
  VelaAvdUtils_1.default.velaAvdCls.deleteVelaAvd(avdName);
197
197
  });
198
198
  })
199
- }
199
+ },
200
+ {
201
+ name: 'initEmulatorEnv',
202
+ description: 'init/reset emulator environment',
203
+ action: () => __awaiter(this, void 0, void 0, function* () {
204
+ VelaAvdUtils_1.default.initEmulatorEnv();
205
+ })
206
+ },
200
207
  ]
201
208
  };
202
209
  commander_1.Command.registeProgram(config);
@@ -1,4 +1,5 @@
1
1
  import CompileMode from '@aiot-toolkit/aiotpack/lib/compiler/enum/CompileMode';
2
+ import IJavascriptCompileOption from '@aiot-toolkit/aiotpack/lib/compiler/javascript/interface/IJavascriptCompileOption';
2
3
  import ParamType from '@aiot-toolkit/commander/lib/interface/IParam';
3
4
  import { Dictionary } from '@aiot-toolkit/shared-utils/lib/type/Type';
4
5
  import IBuilder from './IBuilder';
@@ -6,12 +7,19 @@ interface IUxBuilderOption extends Dictionary {
6
7
  watch?: boolean;
7
8
  mode: CompileMode;
8
9
  disabledJsc: boolean;
10
+ enableE2e?: boolean;
9
11
  }
10
12
  /**
11
13
  * UxBuilder
12
14
  */
13
15
  declare class UxBuilder implements IBuilder<IUxBuilderOption> {
14
16
  readonly QUICKAPP_CONFIG = "quickapp.config.js";
17
+ /**
18
+ * 获取build的编译配置
19
+ * @param projectPath 项目路径
20
+ * @param options 命令参数
21
+ */
22
+ getCompilerOption(projectPath: string, options: IUxBuilderOption): Partial<IJavascriptCompileOption>;
15
23
  /**
16
24
  * ux项目的build函数
17
25
  * @param projectPath 项目路径
@@ -40,10 +40,6 @@ class UxBuilder {
40
40
  constructor() {
41
41
  this.QUICKAPP_CONFIG = 'quickapp.config.js';
42
42
  this.params = [
43
- {
44
- name: 'enable-e2e',
45
- description: 'inject test-suite for current project'
46
- },
47
43
  {
48
44
  type: 'string',
49
45
  name: 'devtool',
@@ -60,13 +56,48 @@ class UxBuilder {
60
56
  defaultValue: false
61
57
  },
62
58
  {
59
+ type: 'confirm',
63
60
  name: 'enable-protobuf',
64
61
  description: 'enable protobuf',
62
+ defaultValue: false
63
+ },
64
+ {
65
65
  type: 'confirm',
66
+ name: 'enable-e2e',
67
+ description: 'inject test-suite for current project',
68
+ defaultValue: false
69
+ },
70
+ {
71
+ type: 'confirm',
72
+ name: 'enable-stats',
73
+ description: 'analyse time and size of webpack output files',
74
+ defaultValue: false
75
+ },
76
+ {
77
+ type: 'confirm',
78
+ name: 'optimize-css-attr',
79
+ description: 'optimize css attr',
66
80
  defaultValue: false
67
81
  }
68
82
  ];
69
83
  }
84
+ /**
85
+ * 获取build的编译配置
86
+ * @param projectPath 项目路径
87
+ * @param options 命令参数
88
+ */
89
+ getCompilerOption(projectPath, options) {
90
+ // 读取项目中文件的配置
91
+ const quickappConfig = this.readQuickAppConfig(projectPath);
92
+ const _a = quickappConfig || {}, { cli } = _a, otherConfig = __rest(_a, ["cli"]);
93
+ options = lodash_1.default.merge({}, options, cli);
94
+ // 项目配置
95
+ const uxProjectConfig = new UxConfig_1.default(projectPath);
96
+ // 编译配置
97
+ const compileMode = options.mode || CompileMode_1.default.DEVELOPMENT;
98
+ const compilerOption = lodash_1.default.merge(JavascriptDefaultCompileOption_1.default, Object.assign(Object.assign({}, options), { projectPath: path_1.default.join(projectPath, uxProjectConfig.output), mode: compileMode, enableProtobuf: options.enableProtobuf, devtool: UxBuilderUtils_1.default.getDevtoolValue(compileMode, options.devtool || false) }), otherConfig);
99
+ return compilerOption;
100
+ }
70
101
  /**
71
102
  * ux项目的build函数
72
103
  * @param projectPath 项目路径
@@ -76,21 +107,10 @@ class UxBuilder {
76
107
  build(projectPath, options) {
77
108
  return __awaiter(this, void 0, void 0, function* () {
78
109
  const watch = options.watch || false;
79
- // 读取项目中文件的配置
80
- const quickappConfig = this.readQuickAppConfig(projectPath);
81
- const _a = quickappConfig || {}, { cli } = _a, otherConfig = __rest(_a, ["cli"]);
82
- options = lodash_1.default.merge({}, options, cli);
83
110
  // 项目配置
84
111
  const uxProjectConfig = new UxConfig_1.default(projectPath);
85
112
  // 编译配置
86
- const compileMode = options.mode || CompileMode_1.default.DEVELOPMENT;
87
- const compilerOption = lodash_1.default.merge({
88
- projectPath: path_1.default.join(projectPath, uxProjectConfig.output),
89
- mode: compileMode,
90
- disabledJSC: options.disabledJsc,
91
- enableProtobuf: options.enableProtobuf,
92
- devtool: UxBuilderUtils_1.default.getDevtoolValue(compileMode, options.devtool)
93
- }, JavascriptDefaultCompileOption_1.default, otherConfig);
113
+ const compilerOption = this.getCompilerOption(projectPath, options);
94
114
  ColorConsole_1.default.info('start build: ', {
95
115
  style: ColorConsole_1.default.getStyle(shared_utils_1.LOG_LEVEL.Info),
96
116
  word: JSON.stringify({
@@ -73,7 +73,7 @@ class XtsBuilder {
73
73
  const follWorks = config.followWorks || [];
74
74
  for (let item of follWorks) {
75
75
  try {
76
- yield item(context, config, compilerOptions);
76
+ yield item.worker(context, config, compilerOptions);
77
77
  }
78
78
  catch (error) {
79
79
  // 报错 prework的item error
@@ -1,13 +1,14 @@
1
- export interface ISystemImagesVersionInfo {
2
- release: string;
3
- dev: string;
4
- }
5
- export type ISystemImagesType = keyof ISystemImagesVersionInfo;
6
1
  export interface IGoldfishVersionInfo {
7
2
  name: string;
8
3
  emulator: string;
9
- 'system-images': ISystemImagesVersionInfo;
4
+ 'system-images': string;
10
5
  qa: string;
11
6
  skins: string;
12
7
  tools: string;
8
+ modem_simulator: string;
9
+ }
10
+ export interface IVelaImageItem {
11
+ label: string;
12
+ value: string;
13
+ time: string | Date;
13
14
  }
@@ -2,9 +2,20 @@ import ParamType from '@aiot-toolkit/commander/lib/interface/IParam';
2
2
  import { IStartOptions } from '@aiot-toolkit/emulator';
3
3
  import UxBuilder from '../builder/UxBuilder';
4
4
  import IStarter from './IStarter';
5
+ /**
6
+ * UxStarter
7
+ * ux快应用启动器
8
+ */
5
9
  declare class UxStarter extends IStarter<IStartOptions> {
6
10
  builder: UxBuilder;
7
11
  params: ParamType[];
8
12
  start(projectPath: string, options: any): Promise<void>;
13
+ /**
14
+ * 检查选择的模拟器在当前环境下是否可以使用
15
+ * 下面的几种情况会导致模拟器不可用:1. 模拟器绑定的Vela镜像不存在;2. Vela4.0缺少coredump.core或者vela_data.bin文件
16
+ * @param avdName 模拟器名称
17
+ * @returns {boolean}
18
+ */
19
+ isAvailableEmulator(avdName: string): boolean;
9
20
  }
10
21
  export default UxStarter;
@@ -46,6 +46,11 @@ const portfinder_1 = __importDefault(require("portfinder"));
46
46
  const UxBuilder_1 = __importDefault(require("../builder/UxBuilder"));
47
47
  const VelaAvdUtils_1 = __importDefault(require("../utils/VelaAvdUtils"));
48
48
  const IStarter_1 = __importDefault(require("./IStarter"));
49
+ const fs_extra_1 = __importDefault(require("fs-extra"));
50
+ /**
51
+ * UxStarter
52
+ * ux快应用启动器
53
+ */
49
54
  class UxStarter extends IStarter_1.default {
50
55
  constructor() {
51
56
  super(...arguments);
@@ -74,9 +79,10 @@ class UxStarter extends IStarter_1.default {
74
79
  }
75
80
  start(projectPath, options) {
76
81
  return __awaiter(this, void 0, void 0, function* () {
82
+ // 获取已经创建的模拟器列表
77
83
  const avdList = VelaAvdUtils_1.default.velaAvdCls.getVelaAvdList();
78
84
  if (avdList.length === 0) {
79
- ColorConsole_1.default.error('### goldfish start ### No vela emulator available, please create it first.');
85
+ ColorConsole_1.default.error('no vela emulator available, please create it first.');
80
86
  VelaAvdUtils_1.default.createVelaAvdByInquire();
81
87
  return;
82
88
  }
@@ -86,6 +92,11 @@ class UxStarter extends IStarter_1.default {
86
92
  return { value: item.avdName };
87
93
  })
88
94
  });
95
+ // 检查选择的模拟器是否符合要求
96
+ if (!this.isAvailableEmulator(avdName)) {
97
+ ColorConsole_1.default.throw(`this emulator is unavailable, please create a new emulator.`);
98
+ return;
99
+ }
89
100
  let serverPort;
90
101
  // watch模型下开启server
91
102
  if (options.watch) {
@@ -95,19 +106,33 @@ class UxStarter extends IStarter_1.default {
95
106
  (0, JavascriptDefaultCompileOption_1.setServerPort)(serverPort);
96
107
  }
97
108
  // build
109
+ const compilerOption = this.builder.getCompilerOption(projectPath, options);
98
110
  yield this.builder.build(projectPath, options);
99
111
  // start
100
112
  const params = {
101
113
  sdkHome: path_1.default.resolve(os_1.default.homedir(), '.export'),
102
114
  avdHome: path_1.default.resolve(os_1.default.homedir(), '.android/avd'),
103
- projectPath
115
+ projectPath,
116
+ compilerOption
104
117
  };
118
+ // 寻找对应的模拟器instance
105
119
  const goldfishInstance = (0, emulator_1.findInstance)(avdName, params);
106
120
  if (!goldfishInstance)
107
121
  return;
108
122
  let vncPort;
123
+ // 设置vncPort
109
124
  if (options.openVNC) {
110
- vncPort = yield portfinder_1.default.getPortPromise({ port: constants_1.defaultVncPort, stopPort: constants_1.defaultVncPort + 100 });
125
+ vncPort = yield portfinder_1.default.getPortPromise({
126
+ port: constants_1.defaultVncPort,
127
+ stopPort: constants_1.defaultVncPort + 100
128
+ });
129
+ }
130
+ // 设置adbPort
131
+ const adbPort = yield portfinder_1.default.getPortPromise({ port: constants_1.defaultAdbPort });
132
+ // 设置debugPort
133
+ let debugPort;
134
+ if (options.devtool) {
135
+ debugPort = yield portfinder_1.default.getPortPromise({ port: constants_1.defaultDebugPort });
111
136
  }
112
137
  const startOptions = {
113
138
  avdName,
@@ -115,6 +140,8 @@ class UxStarter extends IStarter_1.default {
115
140
  disableNSH: options.disableNSH,
116
141
  serverPort,
117
142
  vncPort,
143
+ adbPort,
144
+ debugPort
118
145
  };
119
146
  goldfishInstance.start(startOptions);
120
147
  // waiter
@@ -135,5 +162,25 @@ class UxStarter extends IStarter_1.default {
135
162
  // startWaiter.start()
136
163
  });
137
164
  }
165
+ /**
166
+ * 检查选择的模拟器在当前环境下是否可以使用
167
+ * 下面的几种情况会导致模拟器不可用:1. 模拟器绑定的Vela镜像不存在;2. Vela4.0缺少coredump.core或者vela_data.bin文件
168
+ * @param avdName 模拟器名称
169
+ * @returns {boolean}
170
+ */
171
+ isAvailableEmulator(avdName) {
172
+ const { avdImagePath } = VelaAvdUtils_1.default.velaAvdCls.getVelaAvdInfo(avdName);
173
+ // 没有avdImagePath,即对应的configIni里没有 image.sysdir.1 字段
174
+ // 有avdImagePath,但是这个目录下没有nuttx
175
+ if (!avdImagePath || !fs_extra_1.default.existsSync(path_1.default.resolve(avdImagePath, 'nuttx'))) {
176
+ return false;
177
+ }
178
+ if (avdImagePath.includes('vela-release') &&
179
+ (!fs_extra_1.default.existsSync(path_1.default.resolve(avdImagePath, 'coredump.core')) ||
180
+ !fs_extra_1.default.existsSync(path_1.default.resolve(avdImagePath, 'vela_data.bin')))) {
181
+ return false;
182
+ }
183
+ return true;
184
+ }
138
185
  }
139
186
  exports.default = UxStarter;
@@ -7,6 +7,7 @@ declare class VelaAvdUtils {
7
7
  static emulatorBaseUrl: string;
8
8
  static systemImageBaseUrl: string;
9
9
  static velaAvdCls: VelaAvdCls;
10
+ /** 校验AVD名称 */
10
11
  static validateAvdName(avdName: string): true | "Please enter avd name" | "The avd name must starts with Vela" | "The avd already exists. Please change avd name";
11
12
  /**
12
13
  * 从CDN上下载资源文件
@@ -25,21 +26,36 @@ declare class VelaAvdUtils {
25
26
  static downloadAndUnzip(downloadUrl: string, targetDir: string): Promise<void>;
26
27
  /** 根据host获取模拟器下载地址 */
27
28
  static getEmulatorUrl(version?: string): string;
28
- /** 根据host获取vela镜像下载地址 */
29
- static getSystemImageUrl(version?: string): string;
30
- /** 根据host获取ya-vm-file-server下载地址 */
31
- static getv9fsToolUrl(version?: string): string;
32
- /** 获取资源文件下载地址 */
33
- static getDownloadUrl(dir: string, version?: string, filename?: string): string;
29
+ /** 获取模拟器平台的名称,darwin-aarch64 linux-aarch64 windows-x86_64等 */
30
+ static getEmulatorPlatform(): string;
31
+ /** 获取各项模拟器资源的根目录 */
32
+ static getEmulatorEnvHome(resourceName: string): string;
33
+ /**
34
+ * Vela镜像需要更新
35
+ * @param imageId 镜像id
36
+ * @returns {boolean}
37
+ */
38
+ static velaImageNeedUpdate(imageId: string): boolean;
34
39
  /**
35
- * 判断vela镜像是release版还是dev版
36
- * @param velaImage 镜像名称或者镜像路径
40
+ * Vela镜像是否为zip包(是否为0.0.2版本)
41
+ * 只有Vela开发版(dev, 0.0.2)这个版本的镜像不是zip包
42
+ * @param velaImage
37
43
  * @returns
38
44
  */
39
- static isReleaseVelaImage(velaImage: string): boolean;
45
+ static isZipInImageUrl(velaImage: string): boolean;
46
+ /** 获取vela镜像的下载地址 */
47
+ static getSystemImageUrl(version?: string): string;
48
+ /** 获取模拟器其他资源的下载地址 */
49
+ static getDownloadUrl(dir: string, version?: string, filename?: string): string;
50
+ /** 根据host获取ya-vm-file-server下载地址 */
51
+ static getv9fsToolUrl(version?: string): string;
52
+ /** 获取镜像构建时间 */
53
+ static getImageBuildTime(imageId: string): string | Date | undefined;
40
54
  /**
41
55
  * 命令行访问方式创建模拟器
42
56
  */
43
57
  static createVelaAvdByInquire(): Promise<void>;
58
+ /** 初始化/重置模拟器环境 */
59
+ static initEmulatorEnv(): Promise<void>;
44
60
  }
45
61
  export default VelaAvdUtils;
@@ -23,7 +23,9 @@ const cli_progress_1 = __importDefault(require("cli-progress"));
23
23
  const fs_extra_1 = __importDefault(require("fs-extra"));
24
24
  const os_1 = __importDefault(require("os"));
25
25
  const path_1 = __importDefault(require("path"));
26
+ const dayjs_1 = __importDefault(require("dayjs"));
26
27
  class VelaAvdUtils {
28
+ /** 校验AVD名称 */
27
29
  static validateAvdName(avdName) {
28
30
  if (!avdName) {
29
31
  return `Please enter avd name`;
@@ -108,13 +110,60 @@ class VelaAvdUtils {
108
110
  let hostOs = systemOs === 'win32' ? 'windows' : systemOs;
109
111
  return `${VelaAvdUtils.emulatorBaseUrl}/v${version}/${hostOs}-${hostArch}.zip`;
110
112
  }
111
- /** 根据host获取vela镜像下载地址 */
113
+ /** 获取模拟器平台的名称,darwin-aarch64 linux-aarch64 windows-x86_64等 */
114
+ static getEmulatorPlatform() {
115
+ const systemOs = os_1.default.platform();
116
+ const hostArch = (0, utils_1.getSystemArch)();
117
+ const hostOs = systemOs === 'win32' ? 'windows' : systemOs;
118
+ return `${hostOs}-${hostArch}`;
119
+ }
120
+ /** 获取各项模拟器资源的根目录 */
121
+ static getEmulatorEnvHome(resourceName) {
122
+ return path_1.default.resolve(VelaAvdUtils.sdkHome, resourceName);
123
+ }
124
+ /**
125
+ * Vela镜像需要更新
126
+ * @param imageId 镜像id
127
+ * @returns {boolean}
128
+ */
129
+ static velaImageNeedUpdate(imageId) {
130
+ // 兼容之前的version.json
131
+ if (typeof imageId !== 'string' || !imageId)
132
+ return true;
133
+ let avdImagePath = path_1.default.resolve(VelaAvdUtils.sdkHome, 'system-images', imageId, 'nuttx');
134
+ if (!fs_extra_1.default.existsSync(avdImagePath)) {
135
+ return true;
136
+ }
137
+ if (VelaAvdUtils.isZipInImageUrl(imageId)) {
138
+ const stats = fs_extra_1.default.statSync(avdImagePath);
139
+ const version = constants_1.VelaImageVersionList.find((item) => item.value === imageId);
140
+ if (version && (0, dayjs_1.default)(version.time).isAfter(stats.mtime, 'day'))
141
+ return true;
142
+ }
143
+ return false;
144
+ }
145
+ /**
146
+ * Vela镜像是否为zip包(是否为0.0.2版本)
147
+ * 只有Vela开发版(dev, 0.0.2)这个版本的镜像不是zip包
148
+ * @param velaImage
149
+ * @returns
150
+ */
151
+ static isZipInImageUrl(velaImage) {
152
+ return velaImage.indexOf('0.0.2') < 0;
153
+ }
154
+ /** 获取vela镜像的下载地址 */
112
155
  static getSystemImageUrl(version = 'vela-release-4.0') {
113
- if (this.isReleaseVelaImage(version)) {
114
- return `${VelaAvdUtils.systemImageBaseUrl}/${version}/image.zip`;
156
+ if (VelaAvdUtils.isZipInImageUrl(version)) {
157
+ const velaImage = constants_1.VelaImageVersionList.find((item) => item.value = version);
158
+ const dayOfTime = (0, dayjs_1.default)(velaImage === null || velaImage === void 0 ? void 0 : velaImage.time).format('YYYYMMDD');
159
+ return `${VelaAvdUtils.systemImageBaseUrl}/${version}/${dayOfTime}/${version}.zip`;
115
160
  }
116
161
  return `${VelaAvdUtils.systemImageBaseUrl}/${version}/nuttx`;
117
162
  }
163
+ /** 获取模拟器其他资源的下载地址 */
164
+ static getDownloadUrl(dir, version = '0.0.1', filename = dir) {
165
+ return `${VelaAvdUtils.baseUrl}/${dir}/v${version}/${filename}.zip`;
166
+ }
118
167
  /** 根据host获取ya-vm-file-server下载地址 */
119
168
  static getv9fsToolUrl(version = '0.0.1') {
120
169
  const systemOs = os_1.default.platform();
@@ -122,43 +171,64 @@ class VelaAvdUtils {
122
171
  let v9fsTool = '';
123
172
  switch (systemOs) {
124
173
  case 'linux':
125
- v9fsTool = `ya-vm-file-server-linux-${arch}`;
174
+ v9fsTool = 'ya-vm-file-server-linux-x86_64';
126
175
  break;
127
176
  case 'win32':
128
177
  v9fsTool = 'ya-vm-file-server-windows.exe';
129
178
  break;
130
179
  case 'darwin':
131
180
  v9fsTool = `ya-vm-file-server-darwin-${arch}`;
181
+ break;
132
182
  default:
133
183
  break;
134
184
  }
135
185
  return `${VelaAvdUtils.baseUrl}/tools/v${version}/${v9fsTool}`;
136
186
  }
137
- /** 获取资源文件下载地址 */
138
- static getDownloadUrl(dir, version = '0.0.1', filename = dir) {
139
- return `${VelaAvdUtils.baseUrl}/${dir}/v${version}/${filename}.zip`;
140
- }
141
- /**
142
- * 判断vela镜像是release版还是dev版
143
- * @param velaImage 镜像名称或者镜像路径
144
- * @returns
145
- */
146
- static isReleaseVelaImage(velaImage) {
147
- return velaImage.includes('release');
187
+ /** 获取镜像构建时间 */
188
+ static getImageBuildTime(imageId) {
189
+ var _a;
190
+ if (!imageId)
191
+ return;
192
+ const imagePath = path_1.default.resolve(VelaAvdUtils.sdkHome, 'system-images', imageId, 'nuttx');
193
+ if (fs_extra_1.default.existsSync(imagePath)) {
194
+ if (VelaAvdUtils.isZipInImageUrl(imageId)) {
195
+ const stats = fs_extra_1.default.statSync(imagePath);
196
+ return stats.mtime;
197
+ }
198
+ else {
199
+ const time = (_a = constants_1.VelaImageVersionList.find((item) => item.value === imageId)) === null || _a === void 0 ? void 0 : _a.time;
200
+ return time;
201
+ }
202
+ }
203
+ return;
148
204
  }
149
205
  /**
150
206
  * 命令行访问方式创建模拟器
151
207
  */
152
208
  static createVelaAvdByInquire() {
153
209
  return __awaiter(this, void 0, void 0, function* () {
210
+ const versionFile = path_1.default.resolve(VelaAvdUtils.sdkHome, 'versions.json');
211
+ const versionFileExist = fs_extra_1.default.existsSync(versionFile);
212
+ let currVersionInfo = {
213
+ name: '模拟器资源版本管理',
214
+ emulator: '',
215
+ qa: '',
216
+ skins: '',
217
+ 'system-images': '',
218
+ tools: '',
219
+ modem_simulator: ''
220
+ };
221
+ if (versionFileExist) {
222
+ currVersionInfo = fs_extra_1.default.readJSONSync(versionFile);
223
+ }
224
+ const onlineVersionInfo = constants_1.EmulatorEnvVersion;
225
+ // 模拟器各项资源(除了镜像)是否存在
226
+ const emulatorExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'emulator', VelaAvdUtils.getEmulatorPlatform()));
227
+ const qaExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'qa/font'));
228
+ const skinsExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'skins'));
229
+ const toolsExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'tools'));
230
+ const modemSimulatorExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'modem_simulator/modem_nvram.json'));
154
231
  try {
155
- const versionFile = path_1.default.resolve(VelaAvdUtils.sdkHome, 'versions.json');
156
- const versionFileExist = fs_extra_1.default.existsSync(versionFile);
157
- let versionInfo = undefined;
158
- if (versionFileExist) {
159
- versionInfo = fs_extra_1.default.readJSONSync(versionFile);
160
- }
161
- const onlineVersionInfo = constants_1.emulatorEnvVersion;
162
232
  // avdName
163
233
  const avdName = yield (0, prompts_1.input)({
164
234
  message: 'avd name starting with Vela. (eg. Vela_Virtual_Device)',
@@ -167,67 +237,67 @@ class VelaAvdUtils {
167
237
  return VelaAvdUtils.validateAvdName(value);
168
238
  }
169
239
  });
240
+ // 镜像
241
+ const velaImage = (yield (0, prompts_1.select)({
242
+ message: 'vela image.',
243
+ choices: constants_1.VelaImageVersionList.map((item) => {
244
+ return { value: item.value, name: item.label };
245
+ })
246
+ }));
170
247
  // skin或者size
171
- const needSkin = yield (0, prompts_1.confirm)({
172
- message: 'need avd skin?',
173
- default: true
174
- });
175
- const skinDir = path_1.default.resolve(VelaAvdUtils.sdkHome, 'skins');
176
- const skinExists = fs_extra_1.default.existsSync(skinDir);
177
248
  let avdSkin = '';
178
- let avdWidth = '';
179
- let avdHeight = '';
180
- if (needSkin) {
181
- if (!skinExists || (versionInfo && versionInfo.skins < onlineVersionInfo.skins)) {
182
- const skinsUrl = VelaAvdUtils.getDownloadUrl('skins', onlineVersionInfo.skins);
183
- yield VelaAvdUtils.downloadAndUnzip(skinsUrl, path_1.default.resolve(VelaAvdUtils.sdkHome, 'skins'));
184
- ColorConsole_1.default.success(`Download skins succeed.`);
185
- }
186
- const skinList = VelaAvdUtils.velaAvdCls.getVelaSkinList();
187
- avdSkin = yield (0, prompts_1.select)({
188
- message: 'avd skin.',
189
- choices: skinList.map((item) => {
190
- return {
191
- name: item,
192
- value: item,
193
- description: item
194
- };
195
- })
196
- });
197
- }
198
- else {
199
- avdWidth = yield (0, prompts_1.input)({
200
- message: 'avd width.',
201
- default: '480'
202
- });
203
- avdHeight = yield (0, prompts_1.input)({
204
- message: 'avd height.',
205
- default: '480'
249
+ let avdWidth = '466';
250
+ let avdHeight = '466';
251
+ // vela4.0不允许自定义分辨率
252
+ if (velaImage.indexOf('vela-release') < 0) {
253
+ const needSkin = yield (0, prompts_1.confirm)({
254
+ message: 'need avd skin?',
255
+ default: true
206
256
  });
257
+ if (needSkin) {
258
+ if (!skinsExist || (currVersionInfo && currVersionInfo.skins < onlineVersionInfo.skins)) {
259
+ const skinsUrl = VelaAvdUtils.getDownloadUrl('skins', onlineVersionInfo.skins);
260
+ yield VelaAvdUtils.downloadAndUnzip(skinsUrl, path_1.default.resolve(VelaAvdUtils.sdkHome, 'skins'));
261
+ currVersionInfo.skins = constants_1.EmulatorEnvVersion.skins;
262
+ ColorConsole_1.default.success(`Download skins succeed.`);
263
+ }
264
+ const skinList = VelaAvdUtils.velaAvdCls.getVelaSkinList();
265
+ avdSkin = yield (0, prompts_1.select)({
266
+ message: 'avd skin.',
267
+ choices: skinList.map((item) => {
268
+ return {
269
+ name: item,
270
+ value: item,
271
+ description: item
272
+ };
273
+ })
274
+ });
275
+ }
276
+ else {
277
+ avdWidth = yield (0, prompts_1.input)({
278
+ message: 'avd width.',
279
+ default: '466'
280
+ });
281
+ avdHeight = yield (0, prompts_1.input)({
282
+ message: 'avd height.',
283
+ default: '466'
284
+ });
285
+ }
207
286
  }
208
- // 镜像,TODO:后续需要考虑多种设备的镜像
209
- const velaImage = (yield (0, prompts_1.select)({
210
- message: 'vela image.',
211
- choices: [
212
- // { value: 'vela-release-4.0-pre', name: 'vela抢先版(4.0)' },
213
- { value: 'vela-dev-0.0.2', name: 'vela开发版(dev, 0.0.2)' }
214
- ]
215
- }));
216
287
  // 下载模拟器
217
- const emulatorDir = path_1.default.resolve(VelaAvdUtils.sdkHome, 'emulator');
218
- const emulatorExists = fs_extra_1.default.existsSync(emulatorDir);
219
- if (!emulatorExists || (versionInfo && versionInfo.emulator < onlineVersionInfo.emulator)) {
288
+ if (!emulatorExist || (currVersionInfo && currVersionInfo.emulator < onlineVersionInfo.emulator)) {
220
289
  const emulatorDownloadUrl = VelaAvdUtils.getEmulatorUrl(onlineVersionInfo.emulator);
221
- yield VelaAvdUtils.downloadAndUnzip(emulatorDownloadUrl, emulatorDir);
290
+ yield VelaAvdUtils.downloadAndUnzip(emulatorDownloadUrl, VelaAvdUtils.getEmulatorEnvHome('emulator'));
291
+ currVersionInfo.emulator = constants_1.EmulatorEnvVersion.emulator;
222
292
  ColorConsole_1.default.success(`Download emulator succeed.`);
223
293
  }
224
294
  // 下载镜像
225
- const velaImageDownloadUrl = this.getSystemImageUrl(velaImage);
226
295
  const imageDir = path_1.default.resolve(VelaAvdUtils.sdkHome, 'system-images', velaImage);
227
- const imageExists = fs_extra_1.default.existsSync(path_1.default.resolve(imageDir, 'nuttx'));
228
- if (!imageExists) {
229
- if (this.isReleaseVelaImage(velaImage)) {
296
+ if (this.velaImageNeedUpdate(velaImage)) {
297
+ const velaImageDownloadUrl = this.getSystemImageUrl(velaImage);
298
+ if (VelaAvdUtils.isZipInImageUrl(velaImage)) {
230
299
  yield VelaAvdUtils.downloadAndUnzip(velaImageDownloadUrl, imageDir);
300
+ currVersionInfo['system-images'] = constants_1.EmulatorEnvVersion['system-images'];
231
301
  }
232
302
  else {
233
303
  yield VelaAvdUtils.downloadFromCdn(velaImageDownloadUrl, imageDir, 'nuttx');
@@ -235,29 +305,27 @@ class VelaAvdUtils {
235
305
  ColorConsole_1.default.success(`Download vela image succeed.`);
236
306
  }
237
307
  // 下载快应用qa文件
238
- const qaDir = path_1.default.resolve(VelaAvdUtils.sdkHome, 'qa');
239
- const qaExists = fs_extra_1.default.existsSync(qaDir);
240
- if (!qaExists || (versionInfo && versionInfo.qa < onlineVersionInfo.qa)) {
308
+ if (!qaExist || (currVersionInfo && currVersionInfo.qa < onlineVersionInfo.qa)) {
241
309
  const qaUrl = VelaAvdUtils.getDownloadUrl('qa', onlineVersionInfo.qa);
242
- yield VelaAvdUtils.downloadAndUnzip(qaUrl, qaDir);
310
+ yield VelaAvdUtils.downloadAndUnzip(qaUrl, VelaAvdUtils.getEmulatorEnvHome('qa'));
311
+ currVersionInfo.qa = constants_1.EmulatorEnvVersion.qa;
243
312
  ColorConsole_1.default.success(`Download quickapp font succeed.`);
244
313
  }
245
314
  // 下载tools
246
- const toolsDir = path_1.default.resolve(VelaAvdUtils.sdkHome, 'tools');
247
- const toolsExists = fs_extra_1.default.existsSync(toolsDir);
248
- if (!toolsExists || (versionInfo && versionInfo.tools < onlineVersionInfo.tools)) {
315
+ if (!toolsExist || (currVersionInfo && currVersionInfo.tools < onlineVersionInfo.tools)) {
249
316
  const v9fsToolUrl = VelaAvdUtils.getv9fsToolUrl();
250
317
  const filename = os_1.default.platform() === 'win32' ? 'ya-vm-file-server.exe' : 'ya-vm-file-server';
251
- yield VelaAvdUtils.downloadFromCdn(v9fsToolUrl, toolsDir, filename);
252
- // 后续使用fatfs+adb push方式需要下载相关镜像
253
- // const imageUrl = VelaAvdUtils.getDownloadUrl('tools', onlineVersionInfo.tools, 'image')
254
- // const imageDir = path.resolve(toolsDir, 'image')
255
- // await VelaAvdUtils.downloadAndUnzip(imageUrl, imageDir)
318
+ yield VelaAvdUtils.downloadFromCdn(v9fsToolUrl, VelaAvdUtils.getEmulatorEnvHome('tools'), filename);
319
+ currVersionInfo.tools = constants_1.EmulatorEnvVersion.tools;
256
320
  ColorConsole_1.default.success(`Download tools succeed.`);
257
321
  }
258
- // 替换version.json
259
- const versionJson = JSON.stringify(onlineVersionInfo, null, 2);
260
- fs_extra_1.default.writeFileSync(versionFile, versionJson);
322
+ // 下载modem_simulator
323
+ if (!modemSimulatorExist || currVersionInfo.modem_simulator < constants_1.EmulatorEnvVersion.modem_simulator) {
324
+ const modemSimulatorUrl = VelaAvdUtils.getDownloadUrl('modem_simulator', constants_1.EmulatorEnvVersion.modem_simulator);
325
+ yield VelaAvdUtils.downloadAndUnzip(modemSimulatorUrl, VelaAvdUtils.getEmulatorEnvHome('modem_simulator'));
326
+ currVersionInfo.modem_simulator = constants_1.EmulatorEnvVersion.modem_simulator;
327
+ ColorConsole_1.default.success(`Download modem simulator succeed.`);
328
+ }
261
329
  // 创建avd文本文件
262
330
  const avdImagePath = imageDir;
263
331
  const avdParams = { avdName, avdSkin, avdWidth, avdHeight, avdArch: emulator_1.IAvdArchType.arm, avdImagePath };
@@ -267,6 +335,92 @@ class VelaAvdUtils {
267
335
  catch (e) {
268
336
  ColorConsole_1.default.throw(`Create avd failed. Error: ${e.message}`);
269
337
  }
338
+ finally {
339
+ // 写入versions.json
340
+ const versionJson = JSON.stringify(currVersionInfo, null, 2);
341
+ fs_extra_1.default.writeFileSync(versionFile, versionJson);
342
+ }
343
+ });
344
+ }
345
+ /** 初始化/重置模拟器环境 */
346
+ static initEmulatorEnv() {
347
+ return __awaiter(this, void 0, void 0, function* () {
348
+ const versionFile = path_1.default.resolve(VelaAvdUtils.sdkHome, 'versions.json');
349
+ const versionFileExist = fs_extra_1.default.existsSync(versionFile);
350
+ let currVersionInfo = {
351
+ name: '模拟器资源版本管理',
352
+ emulator: '',
353
+ qa: '',
354
+ skins: '',
355
+ 'system-images': '',
356
+ tools: '',
357
+ modem_simulator: ''
358
+ };
359
+ if (versionFileExist) {
360
+ currVersionInfo = fs_extra_1.default.readJSONSync(versionFile);
361
+ }
362
+ // 模拟器的各项资源是否存在
363
+ const emulatorExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'emulator', VelaAvdUtils.getEmulatorPlatform()));
364
+ const qaExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'qa/font'));
365
+ const skinsExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'skins'));
366
+ const toolsExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'tools'));
367
+ const modemSimulatorExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'modem_simulator/modem_nvram.json'));
368
+ const systemImageExist = fs_extra_1.default.existsSync(path_1.default.resolve(VelaAvdUtils.sdkHome, 'system-images', constants_1.VelaImageVersionList[0].value, 'nuttx'));
369
+ try {
370
+ // 下载皮肤
371
+ if (!skinsExist || currVersionInfo.skins < constants_1.EmulatorEnvVersion.skins) {
372
+ const skinsUrl = VelaAvdUtils.getDownloadUrl('skins', constants_1.EmulatorEnvVersion.skins);
373
+ yield VelaAvdUtils.downloadAndUnzip(skinsUrl, VelaAvdUtils.getEmulatorEnvHome('skins'));
374
+ currVersionInfo.skins = constants_1.EmulatorEnvVersion.skins;
375
+ ColorConsole_1.default.success(`Download skins succeed.`);
376
+ }
377
+ // 下载快应用font
378
+ if (!qaExist || currVersionInfo.qa < constants_1.EmulatorEnvVersion.qa) {
379
+ const qaUrl = VelaAvdUtils.getDownloadUrl('qa', constants_1.EmulatorEnvVersion.qa);
380
+ yield VelaAvdUtils.downloadAndUnzip(qaUrl, VelaAvdUtils.getEmulatorEnvHome('qa'));
381
+ currVersionInfo.qa = constants_1.EmulatorEnvVersion.qa;
382
+ ColorConsole_1.default.success(`Download quickapp font succeed.`);
383
+ }
384
+ // 下载工具
385
+ if (!toolsExist || currVersionInfo.tools < constants_1.EmulatorEnvVersion.tools) {
386
+ const v9fsToolUrl = VelaAvdUtils.getv9fsToolUrl(constants_1.EmulatorEnvVersion.tools);
387
+ const filename = (os_1.default.platform() === 'win32') ? 'ya-vm-file-server.exe' : 'ya-vm-file-server';
388
+ yield VelaAvdUtils.downloadFromCdn(v9fsToolUrl, VelaAvdUtils.getEmulatorEnvHome('tools'), filename);
389
+ currVersionInfo.tools = constants_1.EmulatorEnvVersion.tools;
390
+ ColorConsole_1.default.success(`Download tools succeed.`);
391
+ }
392
+ // 下载模拟器
393
+ if (!emulatorExist || currVersionInfo.emulator < constants_1.EmulatorEnvVersion.emulator) {
394
+ const emulatorUrl = VelaAvdUtils.getEmulatorUrl(constants_1.EmulatorEnvVersion.emulator);
395
+ yield VelaAvdUtils.downloadAndUnzip(emulatorUrl, VelaAvdUtils.getEmulatorEnvHome('emulator'));
396
+ currVersionInfo.emulator = constants_1.EmulatorEnvVersion.emulator;
397
+ ColorConsole_1.default.success(`Download emulator succeed.`);
398
+ }
399
+ // 下载modem_simultor
400
+ if (!modemSimulatorExist || currVersionInfo.modem_simulator < constants_1.EmulatorEnvVersion.modem_simulator) {
401
+ const modemSimulatorUrl = VelaAvdUtils.getDownloadUrl('modem_simulator', constants_1.EmulatorEnvVersion.modem_simulator);
402
+ yield VelaAvdUtils.downloadAndUnzip(modemSimulatorUrl, VelaAvdUtils.getEmulatorEnvHome('modem_simulator'));
403
+ currVersionInfo.modem_simulator = constants_1.EmulatorEnvVersion.modem_simulator;
404
+ ColorConsole_1.default.success(`Download modem simulator succeed.`);
405
+ }
406
+ // 下载vela镜像,默认只下载最新的vela正式版,dev版本需要创建模拟器的时候手动勾选
407
+ if (!systemImageExist || VelaAvdUtils.velaImageNeedUpdate(constants_1.VelaImageVersionList[0].value)) {
408
+ const systemImageUrl = VelaAvdUtils.getSystemImageUrl(constants_1.VelaImageVersionList[0].value);
409
+ const imageDir = path_1.default.resolve(VelaAvdUtils.sdkHome, 'system-images', constants_1.VelaImageVersionList[0].value);
410
+ yield VelaAvdUtils.downloadAndUnzip(systemImageUrl, imageDir);
411
+ currVersionInfo['system-images'] = constants_1.EmulatorEnvVersion['system-images'];
412
+ ColorConsole_1.default.success(`Download vela image succeed.`);
413
+ }
414
+ ColorConsole_1.default.success(`Create emulator environment succeed.`);
415
+ }
416
+ catch (e) {
417
+ ColorConsole_1.default.throw(`Create emulator environment failed. Error: ${e.message}`);
418
+ }
419
+ finally {
420
+ // 写入versions.json
421
+ const versionJson = JSON.stringify(currVersionInfo, null, 2);
422
+ fs_extra_1.default.writeFileSync(versionFile, versionJson);
423
+ }
270
424
  });
271
425
  }
272
426
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiot-toolkit",
3
- "version": "2.0.2-beta.1",
3
+ "version": "2.0.2-beta.10",
4
4
  "description": "Tools for creating, developing, and packaging aiot applications.",
5
5
  "keywords": [
6
6
  "aiot"
@@ -21,24 +21,28 @@
21
21
  "test": "node ./__tests__/aiot-toolkit.test.js"
22
22
  },
23
23
  "dependencies": {
24
- "@aiot-toolkit/commander": "2.0.2-beta.1",
25
- "@aiot-toolkit/emulator": "2.0.2-beta.1",
26
- "@aiot-toolkit/shared-utils": "2.0.2-beta.1",
24
+ "@aiot-toolkit/commander": "2.0.2-beta.10",
25
+ "@aiot-toolkit/emulator": "2.0.2-beta.10",
26
+ "@aiot-toolkit/shared-utils": "2.0.2-beta.10",
27
27
  "@miwt/adb": "^0.7.1",
28
28
  "adb-commander": "^0.1.9",
29
29
  "adm-zip": "^0.5.10",
30
30
  "axios": "^1.5.0",
31
31
  "cli-progress": "^3.12.0",
32
- "create-aiot": "2.0.2-beta.1",
32
+ "create-aiot": "2.0.2-beta.10",
33
+ "dayjs": "^1.11.10",
33
34
  "fast-glob": "^3.3.2",
34
- "file-lane": "2.0.2-beta.1",
35
+ "file-lane": "2.0.2-beta.10",
36
+ "lodash": "^4.17.21",
35
37
  "semver": "^7.6.0",
36
38
  "ws": "^8.15.1"
37
39
  },
38
40
  "devDependencies": {
39
41
  "@types/adm-zip": "^0.5.4",
42
+ "@types/fs-extra": "^11.0.4",
40
43
  "@types/semver": "^7.5.8",
41
- "@types/ws": "^8.5.10"
44
+ "@types/ws": "^8.5.10",
45
+ "fs-extra": "^11.2.0"
42
46
  },
43
- "gitHead": "9244af36e617295ea4ff11a2f4f07f0e4f9f5f59"
47
+ "gitHead": "00d23c1ad1306a6c72bb007685de9fd4a6ee61af"
44
48
  }