aiot-toolkit 2.0.2-beta.1 → 2.0.2-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.
package/lib/bin.js CHANGED
@@ -13,17 +13,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  return (mod && mod.__esModule) ? mod : { "default": mod };
14
14
  };
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
+ const shared_utils_1 = require("@aiot-toolkit/shared-utils");
16
17
  const semver_1 = __importDefault(require("semver"));
17
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
18
18
  // 支持的最低node版本
19
19
  const NODE_MINIMUM_VERSION = '16.0.0';
20
20
  checkVersion();
21
- const CompileMode_1 = __importDefault(require("@aiot-toolkit/aiotpack/lib/compiler/enum/CompileMode"));
21
+ const aiotpack_1 = require("@aiot-toolkit/aiotpack");
22
22
  const commander_1 = require("@aiot-toolkit/commander");
23
- const fs_1 = __importDefault(require("fs"));
24
- const path_1 = __importDefault(require("path"));
23
+ const shared_utils_2 = require("@aiot-toolkit/shared-utils");
25
24
  const UxBuilder_1 = __importDefault(require("./builder/UxBuilder"));
26
25
  const XtsBuilder_1 = __importDefault(require("./builder/XtsBuilder"));
26
+ const AndroidUxStart_1 = __importDefault(require("./starter/AndroidUxStart"));
27
27
  const UxStarter_1 = __importDefault(require("./starter/UxStarter"));
28
28
  const XtsStarter_1 = __importDefault(require("./starter/XtsStarter"));
29
29
  const DeviceUtil_1 = __importDefault(require("./utils/DeviceUtil"));
@@ -32,23 +32,27 @@ const VelaAvdUtils_1 = __importDefault(require("./utils/VelaAvdUtils"));
32
32
  function checkVersion() {
33
33
  const currentVersion = process.versions.node;
34
34
  if (semver_1.default.lt(currentVersion, NODE_MINIMUM_VERSION)) {
35
- ColorConsole_1.default.throw('It is detected that the current NodeJS version ', { word: currentVersion }, ' is too low, please upgrade to NodeJS version higher than ', { word: NODE_MINIMUM_VERSION });
35
+ shared_utils_1.ColorConsole.throw('It is detected that the current NodeJS version ', { word: currentVersion }, ' is too low, please upgrade to NodeJS version higher than ', { word: NODE_MINIMUM_VERSION });
36
36
  process.exit();
37
37
  }
38
38
  }
39
39
  // 配置支持的 builder 类型,新增的项目类型,需在此处加上类型
40
40
  const projectMapper = {
41
- ux: {
41
+ [shared_utils_2.ProjectType.VELA_UX]: {
42
42
  builder: UxBuilder_1.default,
43
43
  starter: UxStarter_1.default
44
44
  },
45
- xts: {
45
+ [shared_utils_2.ProjectType.VELA_XTS]: {
46
46
  builder: XtsBuilder_1.default,
47
47
  starter: XtsStarter_1.default
48
+ },
49
+ [shared_utils_2.ProjectType.ANDDROID_UX]: {
50
+ builder: UxBuilder_1.default,
51
+ starter: AndroidUxStart_1.default
48
52
  }
49
53
  };
50
54
  function findBuilder() {
51
- const projectType = getProjectType();
55
+ const projectType = shared_utils_2.ProjectType.getProjectType(process.cwd());
52
56
  return new projectMapper[projectType].builder();
53
57
  }
54
58
  function build(command, description) {
@@ -60,26 +64,14 @@ function build(command, description) {
60
64
  paramList,
61
65
  action: (option) => __awaiter(this, void 0, void 0, function* () {
62
66
  // 获取对应的 build;如果存在,执行 build 函数,不存在,提示
63
- option.mode = command === 'release' ? CompileMode_1.default.PRODUCTION : CompileMode_1.default.DEVELOPMENT;
67
+ option.mode = command === 'release' ? aiotpack_1.CompileMode.PRODUCTION : aiotpack_1.CompileMode.DEVELOPMENT;
64
68
  const projectPath = process.cwd();
65
69
  yield builder.build(projectPath, option);
66
70
  })
67
71
  };
68
72
  }
69
- function getProjectType() {
70
- const projectPath = process.cwd();
71
- const isXts = fs_1.default.existsSync(path_1.default.resolve(projectPath, 'app/app.xts'));
72
- const isUx = fs_1.default.existsSync(path_1.default.resolve(projectPath, 'src/app.ux'));
73
- if (isUx) {
74
- return "ux" /* ProjectType.UX */;
75
- }
76
- else if (isXts) {
77
- return "xts" /* ProjectType.XTS */;
78
- }
79
- return "ux" /* ProjectType.UX */;
80
- }
81
73
  function findStarter(command, description) {
82
- const projectType = getProjectType();
74
+ const projectType = shared_utils_2.ProjectType.getProjectType(process.cwd());
83
75
  return new projectMapper[projectType].starter(command, description);
84
76
  }
85
77
  function main() {
@@ -100,8 +92,6 @@ function main() {
100
92
  const builder = findBuilder();
101
93
  option.watch = true;
102
94
  yield builder.build(projectPath, option);
103
- // waiter.describe()
104
- // waiter.start()
105
95
  })
106
96
  },
107
97
  {
@@ -110,10 +100,10 @@ function main() {
110
100
  action: () => __awaiter(this, void 0, void 0, function* () {
111
101
  try {
112
102
  const connectedDevices = yield DeviceUtil_1.default.getAllConnectedDevices();
113
- ColorConsole_1.default.info(`The connected devices are: ${connectedDevices ? connectedDevices.join(', ') : 'null'}`);
103
+ shared_utils_1.ColorConsole.info(`The connected devices are: ${connectedDevices ? connectedDevices.join(', ') : 'null'}`);
114
104
  }
115
105
  catch (error) {
116
- ColorConsole_1.default.throw(`Error: getConnectedDevices failed`);
106
+ shared_utils_1.ColorConsole.throw(`Error: getConnectedDevices failed`);
117
107
  }
118
108
  })
119
109
  },
@@ -162,10 +152,12 @@ function main() {
162
152
  action: (option) => __awaiter(this, void 0, void 0, function* () {
163
153
  try {
164
154
  const successMessage = yield DeviceUtil_1.default.installDbgAndMkp(option);
165
- ColorConsole_1.default.success(`${successMessage}`);
155
+ shared_utils_1.ColorConsole.success(`${successMessage}`);
166
156
  }
167
157
  catch (error) {
168
- ColorConsole_1.default.throw(`installDbgAndMkp failed, errorMessage: ${(error === null || error === void 0 ? void 0 : error.toString()) || 'unknown error'}'}`);
158
+ shared_utils_1.ColorConsole.throw(`installDbgAndMkp failed, errorMessage:'}`, {
159
+ word: (error === null || error === void 0 ? void 0 : error.toString()) || 'unknown error'
160
+ });
169
161
  }
170
162
  })
171
163
  },
@@ -196,6 +188,13 @@ function main() {
196
188
  VelaAvdUtils_1.default.velaAvdCls.deleteVelaAvd(avdName);
197
189
  });
198
190
  })
191
+ },
192
+ {
193
+ name: 'initEmulatorEnv',
194
+ description: 'init/reset emulator environment',
195
+ action: () => __awaiter(this, void 0, void 0, function* () {
196
+ VelaAvdUtils_1.default.initEmulatorEnv();
197
+ })
199
198
  }
200
199
  ]
201
200
  };
@@ -1,17 +1,32 @@
1
- import CompileMode from '@aiot-toolkit/aiotpack/lib/compiler/enum/CompileMode';
2
- import ParamType from '@aiot-toolkit/commander/lib/interface/IParam';
3
- import { Dictionary } from '@aiot-toolkit/shared-utils/lib/type/Type';
1
+ import { CompileMode, IJavascriptCompileOption } from '@aiot-toolkit/aiotpack';
2
+ import { IParam } from '@aiot-toolkit/commander';
3
+ import { Dictionary } from '@aiot-toolkit/shared-utils';
4
4
  import IBuilder from './IBuilder';
5
5
  interface IUxBuilderOption extends Dictionary {
6
6
  watch?: boolean;
7
7
  mode: CompileMode;
8
8
  disabledJsc: boolean;
9
+ enableE2e?: boolean;
10
+ /**
11
+ * 是否自动补全 manifest.json 中的 features 配置
12
+ */
13
+ completeFeature?: boolean;
9
14
  }
10
15
  /**
11
16
  * UxBuilder
12
17
  */
13
18
  declare class UxBuilder implements IBuilder<IUxBuilderOption> {
19
+ private readonly events?;
14
20
  readonly QUICKAPP_CONFIG = "quickapp.config.js";
21
+ constructor(events?: {
22
+ onBuildSuccess?: (() => void) | undefined;
23
+ } | undefined);
24
+ /**
25
+ * 获取build的编译配置
26
+ * @param projectPath 项目路径
27
+ * @param options 命令参数
28
+ */
29
+ getCompilerOption(projectPath: string, options: IUxBuilderOption): Partial<IJavascriptCompileOption>;
15
30
  /**
16
31
  * ux项目的build函数
17
32
  * @param projectPath 项目路径
@@ -21,7 +36,7 @@ declare class UxBuilder implements IBuilder<IUxBuilderOption> {
21
36
  build(projectPath: string, options: IUxBuilderOption): Promise<void>;
22
37
  private readQuickAppConfig;
23
38
  static readonly PROJECT_TYPE = "ux quick app";
24
- params: ParamType[];
39
+ params: IParam[];
25
40
  match(projectPath: string): boolean;
26
41
  }
27
42
  export default UxBuilder;
@@ -23,27 +23,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
23
23
  return (mod && mod.__esModule) ? mod : { "default": mod };
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- const CompileMode_1 = __importDefault(require("@aiot-toolkit/aiotpack/lib/compiler/enum/CompileMode"));
27
- const JavascriptDefaultCompileOption_1 = __importDefault(require("@aiot-toolkit/aiotpack/lib/compiler/javascript/JavascriptDefaultCompileOption"));
28
- const UxConfig_1 = __importDefault(require("@aiot-toolkit/aiotpack/lib/config/UxConfig"));
26
+ const aiotpack_1 = require("@aiot-toolkit/aiotpack");
29
27
  const shared_utils_1 = require("@aiot-toolkit/shared-utils");
30
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
28
+ const shared_utils_2 = require("@aiot-toolkit/shared-utils");
31
29
  const file_lane_1 = require("file-lane");
32
30
  const fs_extra_1 = __importDefault(require("fs-extra"));
33
31
  const lodash_1 = __importDefault(require("lodash"));
34
32
  const path_1 = __importDefault(require("path"));
35
- const UxBuilderUtils_1 = __importDefault(require("../utils/UxBuilderUtils"));
33
+ const fs_1 = __importDefault(require("fs"));
34
+ const path_2 = __importDefault(require("path"));
36
35
  /**
37
36
  * UxBuilder
38
37
  */
39
38
  class UxBuilder {
40
- constructor() {
39
+ constructor(events) {
40
+ this.events = events;
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,54 @@ 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',
80
+ defaultValue: false
81
+ },
82
+ {
83
+ type: 'confirm',
84
+ name: 'complete-feature',
85
+ description: 'auto complete the features are used in the code into the features field of the manifest.json file',
66
86
  defaultValue: false
67
87
  }
68
88
  ];
69
89
  }
90
+ /**
91
+ * 获取build的编译配置
92
+ * @param projectPath 项目路径
93
+ * @param options 命令参数
94
+ */
95
+ getCompilerOption(projectPath, options) {
96
+ // 读取项目中文件的配置
97
+ const quickappConfig = this.readQuickAppConfig(projectPath);
98
+ const _a = quickappConfig || {}, { cli } = _a, otherConfig = __rest(_a, ["cli"]);
99
+ options = lodash_1.default.merge({}, options, cli);
100
+ // 项目配置
101
+ const uxProjectConfig = new aiotpack_1.UxConfig(projectPath);
102
+ // 编译配置
103
+ const compileMode = options.mode || aiotpack_1.CompileMode.DEVELOPMENT;
104
+ const compilerOption = lodash_1.default.merge(aiotpack_1.JavascriptDefaultCompileOption, Object.assign(Object.assign({}, options), { projectPath: path_1.default.join(projectPath, uxProjectConfig.output), mode: compileMode, enableProtobuf: options.enableProtobuf }), otherConfig);
105
+ return compilerOption;
106
+ }
70
107
  /**
71
108
  * ux项目的build函数
72
109
  * @param projectPath 项目路径
@@ -76,23 +113,12 @@ class UxBuilder {
76
113
  build(projectPath, options) {
77
114
  return __awaiter(this, void 0, void 0, function* () {
78
115
  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
116
  // 项目配置
84
- const uxProjectConfig = new UxConfig_1.default(projectPath);
117
+ const uxProjectConfig = new aiotpack_1.UxConfig(projectPath);
85
118
  // 编译配置
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);
94
- ColorConsole_1.default.info('start build: ', {
95
- style: ColorConsole_1.default.getStyle(shared_utils_1.LOG_LEVEL.Info),
119
+ const compilerOption = this.getCompilerOption(projectPath, options);
120
+ shared_utils_2.ColorConsole.info('start build: ', {
121
+ style: shared_utils_2.ColorConsole.getStyle(shared_utils_1.Loglevel.INFO),
96
122
  word: JSON.stringify({
97
123
  projectPath,
98
124
  options,
@@ -104,7 +130,7 @@ class UxBuilder {
104
130
  }, null, 2)
105
131
  });
106
132
  // 开始编译项目
107
- yield new file_lane_1.FileLane(uxProjectConfig, projectPath, compilerOption).start({ watch });
133
+ yield new file_lane_1.FileLane(uxProjectConfig, projectPath, compilerOption, this.events).start({ watch });
108
134
  });
109
135
  }
110
136
  readQuickAppConfig(projectPath) {
@@ -115,13 +141,14 @@ class UxBuilder {
115
141
  return data;
116
142
  }
117
143
  catch (error) {
118
- ColorConsole_1.default.throw((error === null || error === void 0 ? void 0 : error.toString()) || '');
144
+ shared_utils_2.ColorConsole.throw((error === null || error === void 0 ? void 0 : error.toString()) || '');
119
145
  }
120
146
  }
121
147
  return;
122
148
  }
123
149
  match(projectPath) {
124
- return projectPath.includes('ux');
150
+ // src/app.ux 存在视为ux项目
151
+ return fs_1.default.existsSync(path_2.default.resolve(projectPath, 'src/app.ux'));
125
152
  }
126
153
  }
127
154
  UxBuilder.PROJECT_TYPE = 'ux quick app';
@@ -13,12 +13,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const aiotpack_1 = require("@aiot-toolkit/aiotpack");
16
- const ICompileOptions_1 = require("@aiot-toolkit/aiotpack/lib/interface/ICompileOptions");
16
+ const aiotpack_2 = require("@aiot-toolkit/aiotpack");
17
17
  const shared_utils_1 = require("@aiot-toolkit/shared-utils");
18
- const ColorConsole_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/ColorConsole"));
19
- const StringUtil_1 = __importDefault(require("@aiot-toolkit/shared-utils/lib/utils/StringUtil"));
20
18
  const file_lane_1 = require("file-lane");
21
- const FileLaneUtil_1 = __importDefault(require("file-lane/lib/utils/FileLaneUtil"));
22
19
  const fs_1 = __importDefault(require("fs"));
23
20
  const path_1 = __importDefault(require("path"));
24
21
  /**
@@ -30,15 +27,15 @@ class XtsBuilder {
30
27
  {
31
28
  name: 'skip',
32
29
  type: 'string',
33
- description: `Can configure skip steps, comma separated, optional values: ${ICompileOptions_1.skipList.join(',')}`,
30
+ description: `Can configure skip steps, comma separated, optional values: ${aiotpack_2.skipList.join(',')}`,
34
31
  validate(value) {
35
32
  // TODO: validate 不起作用
36
- const res = StringUtil_1.default.string2arrayByComma(value);
33
+ const res = shared_utils_1.StringUtil.string2arrayByComma(value);
37
34
  const unValid = res.find((r) => {
38
- return !ICompileOptions_1.skipList.includes(r);
35
+ return !aiotpack_2.skipList.includes(r);
39
36
  });
40
37
  if (unValid) {
41
- return `${unValid} is unvalidate, validate value are ${ICompileOptions_1.skipList.join(',')}, Multiple values separated by commas`;
38
+ return `${unValid} is unvalidate, validate value are ${aiotpack_2.skipList.join(',')}, Multiple values separated by commas`;
42
39
  }
43
40
  return true;
44
41
  }
@@ -53,14 +50,14 @@ class XtsBuilder {
53
50
  var _a;
54
51
  return __awaiter(this, void 0, void 0, function* () {
55
52
  const watch = options.watch || false;
56
- ColorConsole_1.default.success('Start build...\n', { word: 'ProjectPath: ', style: ColorConsole_1.default.getStyle(shared_utils_1.LOG_LEVEL.Success) }, projectPath, '\n', { word: 'buildOptions: ', style: ColorConsole_1.default.getStyle(shared_utils_1.LOG_LEVEL.Success) }, JSON.stringify(options));
53
+ shared_utils_1.ColorConsole.success('Start build...\n', { word: 'ProjectPath: ', style: shared_utils_1.ColorConsole.getStyle(shared_utils_1.Loglevel.SUCCESS) }, projectPath, '\n', { word: 'buildOptions: ', style: shared_utils_1.ColorConsole.getStyle(shared_utils_1.Loglevel.SUCCESS) }, JSON.stringify(options));
57
54
  const compilerOptions = {
58
- skip: StringUtil_1.default.string2arrayByComma(options.skip)
55
+ skip: shared_utils_1.StringUtil.string2arrayByComma(options.skip)
59
56
  };
60
57
  const config = new aiotpack_1.XtsConfig();
61
58
  if ((_a = compilerOptions.skip) === null || _a === void 0 ? void 0 : _a.includes('xts2ts')) {
62
- ColorConsole_1.default.info("### skip compile xts to ts due to --skip ${compilerOptions?.skip.join(',')}");
63
- const context = FileLaneUtil_1.default.createContext(config.output, projectPath);
59
+ shared_utils_1.ColorConsole.info("### skip compile xts to ts due to --skip ${compilerOptions?.skip.join(',')}");
60
+ const context = file_lane_1.FileLaneUtil.createContext(config.output, projectPath);
64
61
  const preWorks = config.preWorks || [];
65
62
  for (let item of preWorks) {
66
63
  try {
@@ -73,7 +70,7 @@ class XtsBuilder {
73
70
  const follWorks = config.followWorks || [];
74
71
  for (let item of follWorks) {
75
72
  try {
76
- yield item(context, config, compilerOptions);
73
+ yield item.worker(context, config, compilerOptions);
77
74
  }
78
75
  catch (error) {
79
76
  // 报错 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
  }
@@ -0,0 +1,39 @@
1
+ import IStarter from './IStarter';
2
+ import { PersistentCommand } from '@aiot-toolkit/commander';
3
+ /**
4
+ * AndroidUxStart
5
+ *
6
+ * 1. 启动本机开发者http服务器,并提供二维码,以下载 rpk
7
+ * 2. 打包 rpk,并监听文件变化,重新打包
8
+ */
9
+ declare class AndroidUxStart extends IStarter {
10
+ private server?;
11
+ private port?;
12
+ private builder?;
13
+ private projectPath;
14
+ get waiter(): PersistentCommand;
15
+ /**
16
+ * 启动
17
+ * 1. build 项目
18
+ * 2. build 成功,则创建http服务器
19
+ */
20
+ start(projectPath: string, options: any): Promise<void>;
21
+ /**
22
+ * 创建服务器
23
+ *
24
+ * 1. 使用 koa 创建服务器
25
+ * 2. 显示服务器地址和二维码
26
+ * @returns
27
+ */
28
+ private createServer;
29
+ private showAddress;
30
+ private build;
31
+ dispose(): void;
32
+ /**
33
+ * 通知已知的设备,应用有更新
34
+ * @param projectPath
35
+ * @param options
36
+ */
37
+ private noticeDeviceListUpdate;
38
+ }
39
+ export default AndroidUxStart;
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const aiotpack_1 = require("@aiot-toolkit/aiotpack");
16
+ const shared_utils_1 = require("@aiot-toolkit/shared-utils");
17
+ const fs_extra_1 = __importDefault(require("fs-extra"));
18
+ const http_1 = __importDefault(require("http"));
19
+ const path_1 = __importDefault(require("path"));
20
+ const UxBuilder_1 = __importDefault(require("../builder/UxBuilder"));
21
+ const IStarter_1 = __importDefault(require("./IStarter"));
22
+ const PackageRouter_1 = __importDefault(require("./androidRouter/PackageRouter"));
23
+ const commander_1 = require("@aiot-toolkit/commander");
24
+ /**
25
+ * AndroidUxStart
26
+ *
27
+ * 1. 启动本机开发者http服务器,并提供二维码,以下载 rpk
28
+ * 2. 打包 rpk,并监听文件变化,重新打包
29
+ */
30
+ class AndroidUxStart extends IStarter_1.default {
31
+ constructor() {
32
+ super(...arguments);
33
+ this.projectPath = '';
34
+ }
35
+ get waiter() {
36
+ return new commander_1.PersistentCommand({
37
+ description: 'you can press follow keys to do something',
38
+ options: [
39
+ {
40
+ key: 'q',
41
+ description: 'show qrcode',
42
+ action: () => {
43
+ this.showAddress();
44
+ }
45
+ },
46
+ {
47
+ key: '?',
48
+ description: 'show waiter desc',
49
+ action: () => {
50
+ this.waiter.clearLog();
51
+ this.waiter.describe();
52
+ }
53
+ }
54
+ ]
55
+ });
56
+ }
57
+ /**
58
+ * 启动
59
+ * 1. build 项目
60
+ * 2. build 成功,则创建http服务器
61
+ */
62
+ start(projectPath, options) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ this.projectPath = projectPath;
65
+ const buildOption = Object.assign(Object.assign({}, aiotpack_1.JavascriptDefaultCompileOption), options);
66
+ yield this.build(projectPath, buildOption);
67
+ yield this.createServer(buildOption);
68
+ });
69
+ }
70
+ /**
71
+ * 创建服务器
72
+ *
73
+ * 1. 使用 koa 创建服务器
74
+ * 2. 显示服务器地址和二维码
75
+ * @returns
76
+ */
77
+ createServer(options) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
80
+ const routeConfigList = [
81
+ new PackageRouter_1.default({
82
+ projectPath: this.projectPath,
83
+ options
84
+ })
85
+ ];
86
+ const data = yield shared_utils_1.NetworkUtil.createHttpServer({
87
+ wantPort: options.server.port,
88
+ routeConfigList,
89
+ staticFolder: path_1.default.join(__dirname, './androidRouter/h5'),
90
+ events: {
91
+ onSuccess: () => {
92
+ this.server = data.server;
93
+ this.port = data.port;
94
+ this.showAddress();
95
+ resolve(data);
96
+ }
97
+ }
98
+ });
99
+ }));
100
+ });
101
+ }
102
+ showAddress() {
103
+ const ip = shared_utils_1.NetworkUtil.getIPv4IPAddress();
104
+ const { port } = this;
105
+ if (!ip) {
106
+ const localUrl = `http://localhost:${port}`;
107
+ shared_utils_1.ColorConsole.warn(`devServer`, localUrl);
108
+ }
109
+ else {
110
+ const lanUrl = `http://${ip}:${port}`;
111
+ // 显示二维码
112
+ shared_utils_1.ColorConsole.info(`devServer`, lanUrl);
113
+ shared_utils_1.CommonUtil.outputQRCodeOnTerminal(lanUrl);
114
+ }
115
+ }
116
+ build(projectPath, options) {
117
+ return __awaiter(this, void 0, void 0, function* () {
118
+ if (!this.builder) {
119
+ this.builder = new UxBuilder_1.default({
120
+ onBuildSuccess: () => {
121
+ this.noticeDeviceListUpdate(projectPath, options);
122
+ }
123
+ });
124
+ }
125
+ yield this.builder.build(projectPath, Object.assign(Object.assign({}, options), { watch: true }));
126
+ });
127
+ }
128
+ dispose() {
129
+ var _a;
130
+ (_a = this.server) === null || _a === void 0 ? void 0 : _a.close();
131
+ }
132
+ /**
133
+ * 通知已知的设备,应用有更新
134
+ * @param projectPath
135
+ * @param options
136
+ */
137
+ noticeDeviceListUpdate(projectPath, options) {
138
+ var _a;
139
+ const { clientRecordPath } = options;
140
+ const json = fs_extra_1.default.readJSONSync(clientRecordPath, { throws: false });
141
+ const deviceList = (_a = json === null || json === void 0 ? void 0 : json.records) === null || _a === void 0 ? void 0 : _a[projectPath];
142
+ if (deviceList) {
143
+ deviceList.forEach((item) => {
144
+ const { ip, port } = item;
145
+ if (!ip) {
146
+ return;
147
+ }
148
+ const url = `http://${ip}:${port}/update`;
149
+ const requestOption = { host: ip, port, path: '/update', timeout: 3000 };
150
+ http_1.default
151
+ .request(requestOption, () => {
152
+ shared_utils_1.ColorConsole.success(`Notify the phone to update the rpk file success`, {
153
+ word: url
154
+ });
155
+ })
156
+ .on('error', (error) => {
157
+ shared_utils_1.ColorConsole.warn(`Notify the phone to update the rpk file`, { word: 'error: ' }, {
158
+ word: error.message
159
+ });
160
+ })
161
+ .on('timeout', () => {
162
+ shared_utils_1.ColorConsole.warn(`Notify the phone to update the rpk file`, { word: 'timeout: ' }, {
163
+ word: url
164
+ });
165
+ });
166
+ });
167
+ }
168
+ }
169
+ }
170
+ exports.default = AndroidUxStart;
@@ -1,4 +1,4 @@
1
- import { ICommand, IParam } from '@aiot-toolkit/commander';
1
+ import { ICommand, IParam, PersistentCommand } from '@aiot-toolkit/commander';
2
2
  /**
3
3
  * IStarter
4
4
  */
@@ -10,6 +10,7 @@ export default abstract class IStarter<O = any> {
10
10
  */
11
11
  readonly params: IParam[];
12
12
  constructor(name: string, description: string);
13
+ get waiter(): PersistentCommand | undefined;
13
14
  /**
14
15
  * start 的命令
15
16
  */
@@ -21,6 +21,9 @@ class IStarter {
21
21
  */
22
22
  this.params = [];
23
23
  }
24
+ get waiter() {
25
+ return;
26
+ }
24
27
  /**
25
28
  * start 的命令
26
29
  */
@@ -29,9 +32,10 @@ class IStarter {
29
32
  name: this.name,
30
33
  description: this.description,
31
34
  paramList: this.params,
35
+ waiter: this.waiter,
32
36
  action: (option) => __awaiter(this, void 0, void 0, function* () {
33
37
  const projectPath = process.cwd();
34
- this.start(projectPath, option);
38
+ return this.start(projectPath, option);
35
39
  })
36
40
  };
37
41
  }