ee-bin 1.6.0 → 1.7.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
@@ -120,4 +120,17 @@ program
120
120
  serve.exec(this.opts());
121
121
  });
122
122
 
123
+ /**
124
+ * updater
125
+ */
126
+ program
127
+ .command('updater')
128
+ .description('updater commands')
129
+ .option('--config <folder>', 'config file', './electron/config/bin.js')
130
+ .option('--asar-file <file>', 'asar file path')
131
+ .action(function() {
132
+ const updater = require('./tools/incrUpdater');
133
+ updater.run(this.opts());
134
+ });
135
+
123
136
  program.parse();
package/lib/utils.js CHANGED
@@ -6,6 +6,7 @@ const chalk = require('chalk');
6
6
  const is = require('is-type-of');
7
7
  const { loadTsConfig } = require('config-file-ts');
8
8
  const JsonLib = require('json5');
9
+ const mkdirp = require('mkdirp');
9
10
 
10
11
  const _basePath = process.cwd();
11
12
 
@@ -111,11 +112,63 @@ function isWindows(prop) {
111
112
  return process.platform === 'win32'
112
113
  }
113
114
 
115
+ /**
116
+ * Delete a file or folder
117
+ */
118
+ function rm(name) {
119
+ // check
120
+ if (!fs.existsSync(name)) {
121
+ return
122
+ }
123
+
124
+ const nodeVersion = (process.versions && process.versions.node) || null;
125
+ if (nodeVersion && compareVersion(nodeVersion, '14.14.0') == 1) {
126
+ fs.rmSync(name, {recursive: true});
127
+ } else {
128
+ fs.rmdirSync(name, {recursive: true});
129
+ }
130
+ }
131
+
132
+ /**
133
+ * 获取项目根目录package.json
134
+ */
135
+ function getPackage () {
136
+ const homeDir = process.cwd();
137
+ const content = readJsonSync(path.join(homeDir, 'package.json'));
138
+
139
+ return content;
140
+ }
141
+
142
+ function readJsonSync (filepath) {
143
+ if (!fs.existsSync(filepath)) {
144
+ throw new Error(filepath + ' is not found');
145
+ }
146
+ return JSON.parse(fs.readFileSync(filepath));
147
+ }
148
+
149
+ function writeJsonSync (filepath, str, options) {
150
+ options = options || {};
151
+ if (!('space' in options)) {
152
+ options.space = 2;
153
+ }
154
+
155
+ mkdirp.sync(path.dirname(filepath));
156
+ if (typeof str === 'object') {
157
+ str = JSON.stringify(str, options.replacer, options.space) + '\n';
158
+ }
159
+
160
+ fs.writeFileSync(filepath, str);
161
+ };
162
+
114
163
  module.exports = {
115
164
  loadConfig,
116
165
  checkConfig,
117
166
  loadEncryptConfig,
118
167
  getElectronProgram,
119
168
  compareVersion,
120
- isWindows
169
+ isWindows,
170
+ rm,
171
+ getPackage,
172
+ readJsonSync,
173
+ writeJsonSync
121
174
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-bin",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "ee bin",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,6 +21,8 @@
21
21
  "globby": "^10.0.0",
22
22
  "is-type-of": "^1.2.1",
23
23
  "javascript-obfuscator": "^4.0.2",
24
+ "mkdirp": "^2.1.3",
25
+ "adm-zip": "^0.4.11",
24
26
  "json5": "^2.2.3"
25
27
  }
26
28
  }
@@ -0,0 +1,133 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const crypto = require('crypto')
6
+ const chalk = require('chalk');
7
+ const Utils = require('../lib/utils');
8
+ const admZip = require('adm-zip')
9
+
10
+ /**
11
+ * 增量升级
12
+ * @class
13
+ */
14
+
15
+ module.exports = {
16
+
17
+ /**
18
+ * 执行
19
+ */
20
+ run(options = {}) {
21
+ console.log('[ee-bin] [updater] Start');
22
+ const { config, asarFile } = options;
23
+ const binCfg = Utils.loadConfig(config);
24
+ const cfg = binCfg.updater;
25
+
26
+ if (!cfg) {
27
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${cfg} config does not exist`));
28
+ return;
29
+ }
30
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.green('config:'), cfg);
31
+
32
+ this.generateFile(cfg, asarFile);
33
+
34
+ console.log('[ee-bin] [updater] End');
35
+ },
36
+
37
+ generateFile(cfg, asarFile) {
38
+ const latestVersionInfo = {}
39
+ const homeDir = process.cwd();
40
+
41
+ let asarFilePath = "";
42
+ if (asarFile) {
43
+ asarFilePath = path.normalize(path.join(homeDir, asarFile));
44
+ } else if (Array.isArray(cfg.asarFile)) {
45
+ // 检查文件列表,如果存在就跳出
46
+ for (const filep of cfg.asarFile) {
47
+ asarFilePath = path.normalize(path.join(homeDir, filep));
48
+ if (fs.existsSync(asarFilePath)) {
49
+ break;
50
+ }
51
+ }
52
+ } else {
53
+ asarFilePath = path.normalize(path.join(homeDir, cfg.asarFile));
54
+ }
55
+
56
+ if (!fs.existsSync(asarFilePath)) {
57
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${asarFilePath} does not exist`));
58
+ return;
59
+ }
60
+
61
+ // 生成 zip
62
+ const asarZipPath = path.join(homeDir, cfg.output.directory, cfg.output.zip);
63
+ if (fs.existsSync(asarZipPath) && cfg.cleanCache) {
64
+ Utils.rm(asarZipPath);
65
+ }
66
+ const zip = new admZip();
67
+ zip.addLocalFile(asarFilePath);
68
+ zip.writeZip(asarZipPath, (err) => {
69
+ if (err) {
70
+ console.log(chalk.blue('[ee-bin] [updater] create zip ') + chalk.red(`Error: ${err}`));
71
+ }
72
+ });
73
+
74
+ const sha1 = this.generateSha1(asarFilePath);
75
+ const packageJson = Utils.getPackage();
76
+ const version = packageJson.version;
77
+ const date = this._getFormattedDate();
78
+ const fileStat = fs.statSync(asarFilePath);
79
+
80
+ for (const item of cfg.platform) {
81
+ latestVersionInfo[item] = {
82
+ version: version,
83
+ file: cfg.output.zip,
84
+ size: fileStat.size,
85
+ sha1: sha1,
86
+ releaseDate: date,
87
+ };
88
+ }
89
+
90
+ const updaterJsonFilePath = path.join(homeDir, cfg.output.directory, cfg.output.file);
91
+ Utils.writeJsonSync(updaterJsonFilePath, latestVersionInfo);
92
+
93
+ // 删除缓存文件,防止生成的 zip 是旧版本
94
+ if (cfg.cleanCache) {
95
+ Utils.rm(path.join(homeDir, cfg.output.directory, 'mac'));
96
+ Utils.rm(path.join(homeDir, cfg.output.directory, 'win-unpacked'));
97
+ }
98
+ },
99
+
100
+ generateSha1(filepath = "") {
101
+ let sha1 = '';
102
+ if (filepath.length == 0) {
103
+ return sha1;
104
+ }
105
+
106
+ if (!fs.existsSync(filepath)) {
107
+ return sha1;
108
+ }
109
+
110
+ console.log(chalk.blue('[ee-bin] [updater] ') + `generate sha1 for filepath:${filepath}`);
111
+ try {
112
+ const buffer = fs.readFileSync(filepath);
113
+ const fsHash = crypto.createHash('sha1');
114
+ fsHash.update(buffer);
115
+ sha1 = fsHash.digest('hex');
116
+ return sha1;
117
+
118
+ } catch (error) {
119
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: generate sha1 error!`));
120
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${error}`));
121
+ }
122
+ return sha1;
123
+ },
124
+
125
+ _getFormattedDate() {
126
+ const date = new Date(); // 获取当前日期
127
+ const year = date.getFullYear(); // 获取年份
128
+ const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 获取月份,月份从0开始计数
129
+ const day = date.getDate().toString().padStart(2, '0'); // 获取日
130
+
131
+ return `${year}-${month}-${day}`;
132
+ }
133
+ }