ee-bin 4.1.11 → 4.2.0

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/index.js CHANGED
@@ -114,6 +114,7 @@ program
114
114
  .option('--config <folder>', 'config file')
115
115
  .option('--asar-file <file>', 'asar file path')
116
116
  .option('--platform <flag>', 'platform')
117
+ .option('--force <flag>', 'force update full')
117
118
  .action(function() {
118
119
  incrUpdater.run(this.opts());
119
120
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-bin",
3
- "version": "4.1.11",
3
+ "version": "4.2.0",
4
4
  "description": "ee bin",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,6 +27,7 @@
27
27
  "javascript-obfuscator": "^4.0.2",
28
28
  "json5": "^2.2.3",
29
29
  "mkdirp": "^2.1.3",
30
+ "js-yaml": "^4.1.0",
30
31
  "tree-kill": "^1.2.2"
31
32
  }
32
33
  }
@@ -5,13 +5,13 @@ const fs = require('fs');
5
5
  const fsPro = require('fs-extra');
6
6
  const crypto = require('crypto');
7
7
  const chalk = require('chalk');
8
- const { loadConfig, getPackage, writeJsonSync } = require('../lib/utils');
9
8
  const admZip = require('adm-zip');
10
9
  const globby = require('globby');
11
-
10
+ const yaml = require('js-yaml');
11
+ const { loadConfig, writeJsonSync } = require('../lib/utils');
12
12
 
13
13
  /**
14
- * 增量升级
14
+ * Updater
15
15
  * @class
16
16
  */
17
17
 
@@ -25,43 +25,57 @@ class IncrUpdater {
25
25
  'win-ia32-unpacked',
26
26
  'linux-unpacked'
27
27
  ];
28
+ this.windows32Platform = 'windows_32';
29
+ this.windows64Platform = 'windows_64';
30
+ this.macosIntelPlatform = 'macos_intel';
31
+ this.macosApplePlatform = 'macos_apple';
32
+ this.linuxPlatform = 'linux';
28
33
  this.nodeModulesString = 'node_modules';
29
34
  this.asarUnpackedString = 'app.asar.unpacked';
30
35
  }
31
36
 
32
37
  /**
33
- * 执行
38
+ * run
34
39
  */
35
40
  run(options = {}) {
36
41
  console.log('[ee-bin] [updater] Start');
37
- const { config, asarFile, platform } = options;
42
+ const { config, asarFile, platform, force } = options;
38
43
  const binCfg = loadConfig(config);
39
44
  const cfg = binCfg.updater;
45
+ const forceUpdate = force == 'true' ? true : false;
40
46
 
41
47
  if (!cfg) {
42
48
  console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${cfg} config does not exist`));
43
49
  return;
44
50
  }
45
51
 
46
- this.generateFile(cfg, asarFile, platform);
52
+ this.generateFile(cfg, asarFile, platform, forceUpdate);
47
53
 
48
54
  console.log('[ee-bin] [updater] End');
49
55
  }
50
56
 
51
57
  /**
52
- * 生成增量升级文件
58
+ * generate json file
53
59
  */
54
- generateFile(config, asarFile, platform) {
60
+ generateFile(config, asarFile, platform, force = false) {
55
61
  const cfg = config[platform];
56
62
  if (!cfg) {
57
63
  console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${platform} config does not exist`));
58
64
  return;
59
65
  }
60
66
 
67
+
61
68
  let latestVersionInfo = {}
62
69
  const homeDir = process.cwd();
63
70
  console.log(chalk.blue('[ee-bin] [updater] ') + chalk.green(`${platform} config:`), cfg);
64
71
 
72
+ const metadataPath = path.join(homeDir, cfg.metadata);
73
+ if (!fs.existsSync(metadataPath)) {
74
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${metadataPath} does not exist!`));
75
+ return;
76
+ }
77
+ const metadataObj = yaml.load(fs.readFileSync(metadataPath, 'utf8'));
78
+
65
79
  let asarFilePath = "";
66
80
  if (asarFile) {
67
81
  asarFilePath = path.normalize(path.join(homeDir, asarFile));
@@ -74,25 +88,26 @@ class IncrUpdater {
74
88
  return;
75
89
  }
76
90
 
77
- const packageJson = getPackage();
78
- const version = packageJson.version;
91
+ const version = metadataObj.version;
79
92
  let platformForFilename = platform;
80
93
  if (platform.indexOf("_") !== -1) {
81
94
  const platformArr = platform.split("_");
95
+ // windows_32 -> windows-32
82
96
  platformForFilename = platformArr.join("-");
83
97
  }
84
98
 
85
- // 生成 zip
99
+ // zip
86
100
  let zipName = "";
87
- zipName = path.basename(cfg.output.zip, '.zip') + `-${platformForFilename}-${version}.zip`;
101
+ const extZip = '.zip';
102
+ zipName = path.basename(cfg.output.zip, extZip) + `-${platformForFilename}-${version}${extZip}`;
88
103
  const asarZipPath = path.join(homeDir, cfg.output.directory, zipName);
89
104
  if (fs.existsSync(asarZipPath)) {
90
105
  fsPro.removeSync(asarZipPath);
91
106
  }
92
107
  const zip = new admZip();
93
- // 添加 asar 文件
108
+ // add asar
94
109
  zip.addLocalFile(asarFilePath);
95
- // 添加 extraResources
110
+ // add extraResources
96
111
  if (cfg.extraResources && cfg.extraResources.length > 0) {
97
112
  const files = globby.sync(cfg.extraResources, { cwd: homeDir });
98
113
  for (const extraRes of files) {
@@ -108,7 +123,7 @@ class IncrUpdater {
108
123
  zip.addLocalFile(extraResPath, zipFileDir);
109
124
  }
110
125
  }
111
- // 添加 asarUnpacked
126
+ // add asarUnpacked
112
127
  if (cfg.asarUnpacked && cfg.asarUnpacked.length > 0) {
113
128
  const modules = cfg.asarUnpacked;
114
129
  for (const moduleItem of modules) {
@@ -130,23 +145,48 @@ class IncrUpdater {
130
145
  }
131
146
  });
132
147
 
133
- // 生成 latest.json
134
- const sha1 = this.generateSha1(asarZipPath);
135
- const date = this._getFormattedDate();
148
+ // generate latest.json
149
+ // incr file
150
+ const zipSha1 = this.generateHash(asarZipPath, 'sha1', 'hex');
136
151
  const fileStat = fs.statSync(asarZipPath);
152
+ // full file
153
+ let fullFileInfo;
154
+ for (const item of metadataObj.files) {
155
+ if (item.url.indexOf('.zip') !== -1) {
156
+ continue;
157
+ }
158
+ fullFileInfo = item;
159
+ }
160
+ const fullFileName = fullFileInfo.url;
161
+ const fullFilePath = path.normalize(path.join(homeDir, cfg.output.directory, fullFileName));
162
+ const generateSha512 = this.generateHash(fullFilePath, 'sha512');
163
+ if (fullFileInfo.sha512 != generateSha512) {
164
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: metadata sha512 is not equal to generateSha512 !
165
+ at metadata sha512: ${fullFileInfo.sha512}
166
+ at generate Sha512: ${generateSha512}`));
167
+ return;
168
+ }
169
+
137
170
  const item = {
138
171
  version: version,
139
172
  file: zipName,
140
173
  size: fileStat.size,
141
- sha1: sha1,
142
- releaseDate: date,
174
+ sha1: zipSha1,
175
+ fullFile: {
176
+ fileName: fullFileInfo.url,
177
+ size: fullFileInfo.size,
178
+ sha512: generateSha512,
179
+ },
180
+ force,
181
+ releaseDate: metadataObj.releaseDate,
143
182
  };
144
- const jsonName = path.basename(cfg.output.file, '.json') + `-${platformForFilename}.json`;
183
+ const extJson = '.json';
184
+ const jsonName = path.basename(cfg.output.file, extJson) + `-${platformForFilename}${extJson}`;
145
185
  latestVersionInfo = item;
146
186
  const updaterJsonFilePath = path.join(homeDir, cfg.output.directory, jsonName);
147
187
  writeJsonSync(updaterJsonFilePath, latestVersionInfo);
148
188
 
149
- // 删除缓存文件,防止生成的 zip 是旧版本
189
+ // Delete cache files to prevent generated zip files from being outdated versions
150
190
  if (cfg.cleanCache) {
151
191
  this.tmpAppDirs.forEach(dir => {
152
192
  const dirPath = path.join(homeDir, cfg.output.directory, dir);
@@ -155,38 +195,28 @@ class IncrUpdater {
155
195
  }
156
196
  }
157
197
 
158
- generateSha1(filepath = "") {
159
- let sha1 = '';
198
+ generateHash(filepath = "", algorithm = "sha512", encoding = "base64") {
199
+ let data = '';
160
200
  if (filepath.length == 0) {
161
- return sha1;
201
+ return data;
162
202
  }
163
203
 
164
204
  if (!fs.existsSync(filepath)) {
165
- return sha1;
205
+ return data;
166
206
  }
167
207
 
168
- console.log(chalk.blue('[ee-bin] [updater] ') + `generate sha1 for filepath:${filepath}`);
208
+ console.log(chalk.blue('[ee-bin] [updater] ') + `generate ${algorithm} for filepath:${filepath}`);
169
209
  try {
170
210
  const buffer = fs.readFileSync(filepath);
171
- const fsHash = crypto.createHash('sha1');
211
+ const fsHash = crypto.createHash(algorithm);
172
212
  fsHash.update(buffer);
173
- sha1 = fsHash.digest('hex');
174
- return sha1;
175
-
213
+ data = fsHash.digest(encoding);
214
+ return data;
176
215
  } catch (error) {
177
- console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: generate sha1 error!`));
216
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: generate ${algorithm} error!`));
178
217
  console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${error}`));
179
218
  }
180
- return sha1;
181
- }
182
-
183
- _getFormattedDate() {
184
- const date = new Date(); // 获取当前日期
185
- const year = date.getFullYear(); // 获取年份
186
- const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 获取月份,月份从0开始计数
187
- const day = date.getDate().toString().padStart(2, '0'); // 获取日
188
-
189
- return `${year}-${month}-${day}`;
219
+ return data;
190
220
  }
191
221
  }
192
222