hunter-open-sdk 0.0.9 → 0.0.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.
@@ -10,6 +10,8 @@ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"))
10
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
11
  var _utils = require("../../utils/utils");
12
12
  var _utils2 = require("../../utils/utils.command");
13
+ var _request = _interopRequireDefault(require("request"));
14
+ var _fsExtra = _interopRequireDefault(require("fs-extra"));
13
15
  // import { app } from 'electron'
14
16
 
15
17
  var sudo = require('sudo-prompt');
@@ -26,7 +28,6 @@ var msiStr = 'AppleMobileDeviceService';
26
28
  var cmdStr = process.platform === 'win32' ? 'tasklist' : 'ps aux';
27
29
  var CACHE_PATH = (0, _utils.isDev)() ? path.join(process.cwd(), './cache') : path.join(process.resourcesPath, './cache');
28
30
  var filePath = path.join(CACHE_PATH, './iTunesOL_Lite_64_12.10.0.7.zip');
29
- var mainWindowObj;
30
31
  var checkProcess = function checkProcess() {
31
32
  return new Promise(function (resolve, reject) {
32
33
  exec(cmdStr, function (err, stdout, stderr) {
@@ -83,7 +84,7 @@ var checkProcessTimer = function checkProcessTimer() {
83
84
  _context.next = 6;
84
85
  break;
85
86
  }
86
- mainWindowObj.webContents.send("driverDone");
87
+ clearTimeout(timer);
87
88
  return _context.abrupt("return");
88
89
  case 6:
89
90
  checkProcessTimer();
@@ -100,16 +101,23 @@ var installDriver = function installDriver() {
100
101
  var AppleMobileDeviceSupport64 = path.join(CACHE_PATH, './AppleMobileDeviceSupport64.msi');
101
102
  var Bonjour64 = path.join(CACHE_PATH, './Bonjour64.msi');
102
103
  var cmdStr = "start /i /wait ".concat(AppleApplicationSupport, " /qn && start /i /wait ").concat(AppleApplicationSupport64, " /qn && start /i /wait ").concat(AppleMobileDeviceSupport64, " /qn && start /i /wait ").concat(Bonjour64, " /qn");
103
- sudo.exec(cmdStr, options, function (error, stdout, stderr) {
104
- console.log('安装 error>>', error);
105
- console.log('安装 stdout>>>', stdout);
106
- console.log('安装 stderr>>>', stderr);
107
- if ((0, _utils.isDev)()) {
108
- (0, _utils2.command)(path.join(process.cwd(), './extraResources/ightools.exe'));
109
- } else {
110
- (0, _utils2.command)(path.join(process.resourcesPath, './extraResources/ightools.exe'));
111
- }
112
- checkProcessTimer();
104
+ return new Promise(function (resolve, reject) {
105
+ sudo.exec(cmdStr, options, function (error, stdout, stderr) {
106
+ console.log('安装 error>>', error);
107
+ console.log('安装 stdout>>>', stdout);
108
+ console.log('安装 stderr>>>', stderr);
109
+ if ((0, _utils.isDev)()) {
110
+ (0, _utils2.command)(path.join(process.cwd(), './extraResources/ightools.exe'));
111
+ } else {
112
+ (0, _utils2.command)(path.join(process.resourcesPath, './extraResources/ightools.exe'));
113
+ }
114
+ checkProcessTimer();
115
+ if (error) {
116
+ reject(error); // 安装失败
117
+ } else {
118
+ resolve(); // 安装成功
119
+ }
120
+ });
113
121
  });
114
122
  };
115
123
  // 解压文件
@@ -123,46 +131,60 @@ var uncopressingFile = function uncopressingFile(filePath) {
123
131
  });
124
132
  });
125
133
  };
126
- function downloadDriver(mainWindow) {
127
- mainWindowObj = mainWindow;
128
- mainWindow.webContents.downloadURL('http://ljtools.zhuanstatic.com/download/iTunesDriver/iTunesOL_Lite_64_12.10.0.7.zip');
129
- mainWindow.webContents.session.on('will-download', function (e, item) {
130
- // 文件总大小
131
- var totalBytes = item.getTotalBytes();
132
- // 设置保存路径
133
- var rootPath = path.join(CACHE_PATH, item.getFilename());
134
- item.setSavePath(rootPath);
135
- item.on('updated', function () {
136
- try {
137
- // 获取进度,设置进度条
138
- var _process = item.getReceivedBytes() / totalBytes;
139
- _process = Math.round(_process * 100);
140
- mainWindow.webContents.send('downloadProcess', _process);
141
- } catch (e) {
142
- console.log('驱动下载失败');
134
+
135
+ // Promise 版本的 downloadDriver,onProgress 作为参数,成功 resolve,失败 reject
136
+ function downloadDriver(onProgress) {
137
+ return new Promise(function (resolve, reject) {
138
+ var url = 'http://ljtools.zhuanstatic.com/download/iTunesDriver/iTunesOL_Lite_64_12.10.0.7.zip';
139
+ var dest = filePath;
140
+ var fileStream = _fsExtra.default.createWriteStream(dest);
141
+ var received = 0;
142
+ var total = 0;
143
+ (0, _request.default)(url).on('response', function (response) {
144
+ if (response.statusCode !== 200) {
145
+ reject(new Error("\u4E0B\u8F7D\u5931\u8D25: ".concat(response.statusMessage)));
146
+ return;
143
147
  }
144
- });
145
- // 下载完成
146
- item.on('done', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
148
+ total = parseInt(response.headers['content-length'], 10);
149
+ }).on('data', function (chunk) {
150
+ received += chunk.length;
151
+ if (onProgress && total) {
152
+ onProgress(Math.round(received / total * 100));
153
+ }
154
+ }).on('error', function (err) {
155
+ console.error('驱动下载失败', err);
156
+ reject(err);
157
+ }).pipe(fileStream).on('finish', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
147
158
  var uncopressing;
148
159
  return _regenerator.default.wrap(function _callee2$(_context2) {
149
160
  while (1) switch (_context2.prev = _context2.next) {
150
161
  case 0:
151
- _context2.next = 2;
162
+ _context2.prev = 0;
163
+ _context2.next = 3;
152
164
  return uncopressingFile(filePath);
153
- case 2:
165
+ case 3:
154
166
  uncopressing = _context2.sent;
155
- if (uncopressing) installDriver();
156
- mainWindow.webContents.send('downloadDriverDone');
157
- case 5:
167
+ if (uncopressing) {
168
+ installDriver().then(function () {
169
+ resolve();
170
+ }).catch(function (err) {
171
+ reject(err);
172
+ });
173
+ } else {
174
+ reject(new Error('解压失败'));
175
+ }
176
+ _context2.next = 10;
177
+ break;
178
+ case 7:
179
+ _context2.prev = 7;
180
+ _context2.t0 = _context2["catch"](0);
181
+ reject(_context2.t0);
182
+ case 10:
158
183
  case "end":
159
184
  return _context2.stop();
160
185
  }
161
- }, _callee2);
186
+ }, _callee2, null, [[0, 7]]);
162
187
  })));
163
- item.on('error', function () {
164
- console.log('驱动下载失败');
165
- });
166
188
  });
167
189
  }
168
190
  function checkHasDriver() {
@@ -24,12 +24,18 @@ var GetAndroidSn = function GetAndroidSn(params, callBack) {
24
24
  return customWebUSB.getDevices();
25
25
  case 4:
26
26
  deviceList = _context.sent;
27
+ if (attachDeviceId) {
28
+ _context.next = 7;
29
+ break;
30
+ }
31
+ return _context.abrupt("return");
32
+ case 7:
27
33
  // 删选出当前的设备信息 为获取到serialNumber
28
34
  currentDevice = deviceList.filter(function (item) {
29
35
  return item.device.deviceAddress == attachDeviceId;
30
36
  });
31
37
  if (currentDevice.length) {
32
- _context.next = 9;
38
+ _context.next = 11;
33
39
  break;
34
40
  }
35
41
  callBack({
@@ -38,7 +44,7 @@ var GetAndroidSn = function GetAndroidSn(params, callBack) {
38
44
  message: '获取SN失败'
39
45
  });
40
46
  return _context.abrupt("return");
41
- case 9:
47
+ case 11:
42
48
  callBack({
43
49
  code: 0,
44
50
  data: {
@@ -46,7 +52,7 @@ var GetAndroidSn = function GetAndroidSn(params, callBack) {
46
52
  },
47
53
  message: '获取SN成功'
48
54
  });
49
- case 10:
55
+ case 12:
50
56
  case "end":
51
57
  return _context.stop();
52
58
  }
@@ -21,6 +21,19 @@ var saasCallBack;
21
21
  // 初始化dll
22
22
  (0, _utils2.HunterInspectionKit)();
23
23
 
24
+ /**
25
+ * 获取资源文件的基础路径
26
+ */
27
+ var getResourcePath = function getResourcePath() {
28
+ if ((0, _utils.isDev)()) {
29
+ // 开发环境:使用源码目录
30
+ return path.resolve(__dirname, '../resources');
31
+ } else {
32
+ // 生产环境:使用 Electron 的资源路径
33
+ return path.resolve(process.resourcesPath, 'resources');
34
+ }
35
+ };
36
+
24
37
  /**
25
38
  * 配对
26
39
  * @param {String} udid 设备id
@@ -85,7 +98,8 @@ var DevicePair = /*#__PURE__*/function () {
85
98
 
86
99
  // 写入dll文件version
87
100
  var writeDllVersion = function writeDllVersion(version) {
88
- var versionPath = path.resolve(__dirname, '../resources/version.online');
101
+ var resourcePath = getResourcePath();
102
+ var versionPath = path.resolve(resourcePath, 'version.online');
89
103
  fs.writeFile(versionPath, version, function (err) {
90
104
  if (err) {
91
105
  saasCallBack({
@@ -112,12 +126,13 @@ var writeDllVersion = function writeDllVersion(version) {
112
126
  var updateDll = function updateDll(packageUrl, targetVersion) {
113
127
  // 若需要更新 则下载完成后 覆盖本地dll文件
114
128
  var dllOutPath;
129
+ var resourcePath = getResourcePath();
115
130
  if ((0, _utils.isMac)()) {
116
131
  if ((0, _utils.isArm64)()) {
117
- dllOutPath = path.resolve(__dirname, "../resources/mac/libinspectionkit_arm64_".concat(targetVersion, ".dylib"));
132
+ dllOutPath = path.resolve(resourcePath, "mac/libinspectionkit_arm64_".concat(targetVersion, ".dylib"));
118
133
  }
119
134
  } else {
120
- dllOutPath = path.resolve(__dirname, "../resources/dll/libinspectionkit_x64_".concat(targetVersion, ".dll"));
135
+ dllOutPath = path.resolve(resourcePath, "dll/libinspectionkit_x64_".concat(targetVersion, ".dll"));
121
136
  }
122
137
  var dllStream = fs.createWriteStream(dllOutPath);
123
138
  request(packageUrl).pipe(dllStream).on("close", function (err) {
@@ -209,7 +224,38 @@ var RegisterEvent = function RegisterEvent(params, callBack) {
209
224
  // 有驱动,直接运行脚本
210
225
  DevicePair(udid, params);
211
226
  } else {
212
- (0, _winDriver.downloadDriver)();
227
+ // 没有驱动,则下载驱动
228
+ (0, _winDriver.downloadDriver)(function (percent) {
229
+ saasCallBack({
230
+ data: {
231
+ eventName: 'type_need_install_driver',
232
+ eventCode: 0,
233
+ eventContent: JSON.stringify({
234
+ percent: percent // 下载进度
235
+ })
236
+ }
237
+ });
238
+ }).then(function () {
239
+ // 成功
240
+ console.log('驱动下载并安装成功');
241
+ saasCallBack({
242
+ data: {
243
+ eventName: 'type_install_driver_result',
244
+ eventCode: 0 // 0 成功 -1 失败
245
+ }
246
+ });
247
+ // 成功后进行配对
248
+ DevicePair(udid, params);
249
+ }).catch(function (err) {
250
+ // 失败
251
+ console.error('驱动下载或安装失败', err);
252
+ saasCallBack({
253
+ data: {
254
+ eventName: 'type_install_driver_result',
255
+ eventCode: -1 // 0 成功 -1 失败
256
+ }
257
+ });
258
+ });
213
259
  }
214
260
  });
215
261
  } else {
@@ -14,7 +14,26 @@ var isLinux = exports.isLinux = function isLinux() {
14
14
  return process.platform === 'linux';
15
15
  };
16
16
  var isDev = exports.isDev = function isDev() {
17
- return process.env.WEBPACK_DEV_SERVER === 'true';
17
+ // 检查 NODE_ENV 是否为 development
18
+ if (process.env.NODE_ENV === 'development') {
19
+ return true;
20
+ }
21
+
22
+ // 检查是否在 Electron 开发模式下运行
23
+ if (process.env.ELECTRON_IS_DEV) {
24
+ return true;
25
+ }
26
+
27
+ // 检查是否在 webpack dev server 模式下运行
28
+ if (process.env.WEBPACK_DEV_SERVER === 'true') {
29
+ return true;
30
+ }
31
+
32
+ // 检查是否在 Electron 主进程中,且没有打包
33
+ if (process.type === 'browser' && !process.resourcesPath.includes('app.asar')) {
34
+ return true;
35
+ }
36
+ return false;
18
37
  };
19
38
  var isProd = exports.isProd = function isProd() {
20
39
  return process.env.NODE_ENV === 'production';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hunter-open-sdk",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "采货侠SaaS版本桌面端sdk",
5
5
  "scripts": {
6
6
  "start": "zz dev",
@@ -29,6 +29,7 @@
29
29
  "core-js": "3.6.5",
30
30
  "ffi-napi": "^4.0.3",
31
31
  "fs": "^0.0.1-security",
32
+ "fs-extra": "^11.3.0",
32
33
  "lodash": "4.17.21",
33
34
  "request": "^2.88.2",
34
35
  "sudo-prompt": "^9.2.1",