ee-bin 4.1.10 → 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.
@@ -1,196 +1,226 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const fs = require('fs');
5
- const fsPro = require('fs-extra');
6
- const crypto = require('crypto');
7
- const chalk = require('chalk');
8
- const { loadConfig, getPackage, writeJsonSync } = require('../lib/utils');
9
- const admZip = require('adm-zip');
10
- const globby = require('globby');
11
-
12
-
13
- /**
14
- * 增量升级
15
- * @class
16
- */
17
-
18
- class IncrUpdater {
19
-
20
- constructor() {
21
- this.tmpAppDirs = [
22
- 'mac',
23
- 'mac-arm64',
24
- 'win-unpacked',
25
- 'win-ia32-unpacked',
26
- 'linux-unpacked'
27
- ];
28
- this.nodeModulesString = 'node_modules';
29
- this.asarUnpackedString = 'app.asar.unpacked';
30
- }
31
-
32
- /**
33
- * 执行
34
- */
35
- run(options = {}) {
36
- console.log('[ee-bin] [updater] Start');
37
- const { config, asarFile, platform } = options;
38
- const binCfg = loadConfig(config);
39
- const cfg = binCfg.updater;
40
-
41
- if (!cfg) {
42
- console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${cfg} config does not exist`));
43
- return;
44
- }
45
-
46
- this.generateFile(cfg, asarFile, platform);
47
-
48
- console.log('[ee-bin] [updater] End');
49
- }
50
-
51
- /**
52
- * 生成增量升级文件
53
- */
54
- generateFile(config, asarFile, platform) {
55
- const cfg = config[platform];
56
- if (!cfg) {
57
- console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${platform} config does not exist`));
58
- return;
59
- }
60
-
61
- let latestVersionInfo = {}
62
- const homeDir = process.cwd();
63
- console.log(chalk.blue('[ee-bin] [updater] ') + chalk.green(`${platform} config:`), cfg);
64
-
65
- let asarFilePath = "";
66
- if (asarFile) {
67
- asarFilePath = path.normalize(path.join(homeDir, asarFile));
68
- } else {
69
- asarFilePath = path.normalize(path.join(homeDir, cfg.asarFile));
70
- }
71
-
72
- if (!fs.existsSync(asarFilePath)) {
73
- console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${asarFilePath} does not exist`));
74
- return;
75
- }
76
-
77
- const packageJson = getPackage();
78
- const version = packageJson.version;
79
- let platformForFilename = platform;
80
- if (platform.indexOf("_") !== -1) {
81
- const platformArr = platform.split("_");
82
- platformForFilename = platformArr.join("-");
83
- }
84
-
85
- // 生成 zip
86
- let zipName = "";
87
- zipName = path.basename(cfg.output.zip, '.zip') + `-${platformForFilename}-${version}.zip`;
88
- const asarZipPath = path.join(homeDir, cfg.output.directory, zipName);
89
- if (fs.existsSync(asarZipPath)) {
90
- fsPro.removeSync(asarZipPath);
91
- }
92
- const zip = new admZip();
93
- // 添加 asar 文件
94
- zip.addLocalFile(asarFilePath);
95
- // 添加 extraResources
96
- if (cfg.extraResources && cfg.extraResources.length > 0) {
97
- const files = globby.sync(cfg.extraResources, { cwd: homeDir });
98
- for (const extraRes of files) {
99
- const extraResPath = path.normalize(path.join(homeDir, extraRes));
100
- if (!fs.existsSync(extraResPath)) {
101
- continue;
102
- }
103
- const extraResDir = path.dirname(extraResPath);
104
- const index = extraResDir.indexOf('extraResources');
105
- const zipFileDir = extraResDir.substring(index);
106
- // 资源路径 extraResPath: D:\www\gofile\src\ee\ee-demo\build\extraResources\hello\c.txt
107
- // 文件在zip中的路径 zipFileDir: extraResources/hello
108
- zip.addLocalFile(extraResPath, zipFileDir);
109
- }
110
- }
111
- // 添加 asarUnpacked
112
- if (cfg.asarUnpacked && cfg.asarUnpacked.length > 0) {
113
- const modules = cfg.asarUnpacked;
114
- for (const moduleItem of modules) {
115
- const modulePath = path.normalize(path.join(homeDir, moduleItem));
116
- if (!fs.existsSync(modulePath)) {
117
- throw new Error(`${modulePath} is not exists!`);
118
- }
119
-
120
- const zipDir = path.join(this.asarUnpackedString, moduleItem);
121
- // console.log('modulePath', modulePath);
122
- // console.log('zipDir', zipDir);
123
- zip.addLocalFolder(modulePath, zipDir);
124
- }
125
- }
126
-
127
- zip.writeZip(asarZipPath, (err) => {
128
- if (err) {
129
- console.log(chalk.blue('[ee-bin] [updater] create zip ') + chalk.red(`Error: ${err}`));
130
- }
131
- });
132
-
133
- // 生成 latest.json
134
- const sha1 = this.generateSha1(asarFilePath);
135
- const date = this._getFormattedDate();
136
- const fileStat = fs.statSync(asarFilePath);
137
- const item = {
138
- version: version,
139
- file: zipName,
140
- size: fileStat.size,
141
- sha1: sha1,
142
- releaseDate: date,
143
- };
144
- const jsonName = path.basename(cfg.output.file, '.json') + `-${platformForFilename}.json`;
145
- latestVersionInfo = item;
146
- const updaterJsonFilePath = path.join(homeDir, cfg.output.directory, jsonName);
147
- writeJsonSync(updaterJsonFilePath, latestVersionInfo);
148
-
149
- // 删除缓存文件,防止生成的 zip 是旧版本
150
- if (cfg.cleanCache) {
151
- this.tmpAppDirs.forEach(dir => {
152
- const dirPath = path.join(homeDir, cfg.output.directory, dir);
153
- fsPro.removeSync(dirPath);
154
- });
155
- }
156
- }
157
-
158
- generateSha1(filepath = "") {
159
- let sha1 = '';
160
- if (filepath.length == 0) {
161
- return sha1;
162
- }
163
-
164
- if (!fs.existsSync(filepath)) {
165
- return sha1;
166
- }
167
-
168
- console.log(chalk.blue('[ee-bin] [updater] ') + `generate sha1 for filepath:${filepath}`);
169
- try {
170
- const buffer = fs.readFileSync(filepath);
171
- const fsHash = crypto.createHash('sha1');
172
- fsHash.update(buffer);
173
- sha1 = fsHash.digest('hex');
174
- return sha1;
175
-
176
- } catch (error) {
177
- console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: generate sha1 error!`));
178
- console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${error}`));
179
- }
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}`;
190
- }
191
- }
192
-
193
- module.exports = {
194
- IncrUpdater,
195
- incrUpdater: new IncrUpdater()
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const fsPro = require('fs-extra');
6
+ const crypto = require('crypto');
7
+ const chalk = require('chalk');
8
+ const admZip = require('adm-zip');
9
+ const globby = require('globby');
10
+ const yaml = require('js-yaml');
11
+ const { loadConfig, writeJsonSync } = require('../lib/utils');
12
+
13
+ /**
14
+ * Updater
15
+ * @class
16
+ */
17
+
18
+ class IncrUpdater {
19
+
20
+ constructor() {
21
+ this.tmpAppDirs = [
22
+ 'mac',
23
+ 'mac-arm64',
24
+ 'win-unpacked',
25
+ 'win-ia32-unpacked',
26
+ 'linux-unpacked'
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';
33
+ this.nodeModulesString = 'node_modules';
34
+ this.asarUnpackedString = 'app.asar.unpacked';
35
+ }
36
+
37
+ /**
38
+ * run
39
+ */
40
+ run(options = {}) {
41
+ console.log('[ee-bin] [updater] Start');
42
+ const { config, asarFile, platform, force } = options;
43
+ const binCfg = loadConfig(config);
44
+ const cfg = binCfg.updater;
45
+ const forceUpdate = force == 'true' ? true : false;
46
+
47
+ if (!cfg) {
48
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${cfg} config does not exist`));
49
+ return;
50
+ }
51
+
52
+ this.generateFile(cfg, asarFile, platform, forceUpdate);
53
+
54
+ console.log('[ee-bin] [updater] End');
55
+ }
56
+
57
+ /**
58
+ * generate json file
59
+ */
60
+ generateFile(config, asarFile, platform, force = false) {
61
+ const cfg = config[platform];
62
+ if (!cfg) {
63
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${platform} config does not exist`));
64
+ return;
65
+ }
66
+
67
+
68
+ let latestVersionInfo = {}
69
+ const homeDir = process.cwd();
70
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.green(`${platform} config:`), cfg);
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
+
79
+ let asarFilePath = "";
80
+ if (asarFile) {
81
+ asarFilePath = path.normalize(path.join(homeDir, asarFile));
82
+ } else {
83
+ asarFilePath = path.normalize(path.join(homeDir, cfg.asarFile));
84
+ }
85
+
86
+ if (!fs.existsSync(asarFilePath)) {
87
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${asarFilePath} does not exist`));
88
+ return;
89
+ }
90
+
91
+ const version = metadataObj.version;
92
+ let platformForFilename = platform;
93
+ if (platform.indexOf("_") !== -1) {
94
+ const platformArr = platform.split("_");
95
+ // windows_32 -> windows-32
96
+ platformForFilename = platformArr.join("-");
97
+ }
98
+
99
+ // zip
100
+ let zipName = "";
101
+ const extZip = '.zip';
102
+ zipName = path.basename(cfg.output.zip, extZip) + `-${platformForFilename}-${version}${extZip}`;
103
+ const asarZipPath = path.join(homeDir, cfg.output.directory, zipName);
104
+ if (fs.existsSync(asarZipPath)) {
105
+ fsPro.removeSync(asarZipPath);
106
+ }
107
+ const zip = new admZip();
108
+ // add asar
109
+ zip.addLocalFile(asarFilePath);
110
+ // add extraResources
111
+ if (cfg.extraResources && cfg.extraResources.length > 0) {
112
+ const files = globby.sync(cfg.extraResources, { cwd: homeDir });
113
+ for (const extraRes of files) {
114
+ const extraResPath = path.normalize(path.join(homeDir, extraRes));
115
+ if (!fs.existsSync(extraResPath)) {
116
+ continue;
117
+ }
118
+ const extraResDir = path.dirname(extraResPath);
119
+ const index = extraResDir.indexOf('extraResources');
120
+ const zipFileDir = extraResDir.substring(index);
121
+ // 资源路径 extraResPath: D:\www\gofile\src\ee\ee-demo\build\extraResources\hello\c.txt
122
+ // 文件在zip中的路径 zipFileDir: extraResources/hello
123
+ zip.addLocalFile(extraResPath, zipFileDir);
124
+ }
125
+ }
126
+ // add asarUnpacked
127
+ if (cfg.asarUnpacked && cfg.asarUnpacked.length > 0) {
128
+ const modules = cfg.asarUnpacked;
129
+ for (const moduleItem of modules) {
130
+ const modulePath = path.normalize(path.join(homeDir, moduleItem));
131
+ if (!fs.existsSync(modulePath)) {
132
+ throw new Error(`${modulePath} is not exists!`);
133
+ }
134
+
135
+ const zipDir = path.join(this.asarUnpackedString, moduleItem);
136
+ // console.log('modulePath', modulePath);
137
+ // console.log('zipDir', zipDir);
138
+ zip.addLocalFolder(modulePath, zipDir);
139
+ }
140
+ }
141
+
142
+ zip.writeZip(asarZipPath, (err) => {
143
+ if (err) {
144
+ console.log(chalk.blue('[ee-bin] [updater] create zip ') + chalk.red(`Error: ${err}`));
145
+ }
146
+ });
147
+
148
+ // generate latest.json
149
+ // incr file
150
+ const zipSha1 = this.generateHash(asarZipPath, 'sha1', 'hex');
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
+
170
+ const item = {
171
+ version: version,
172
+ file: zipName,
173
+ size: fileStat.size,
174
+ sha1: zipSha1,
175
+ fullFile: {
176
+ fileName: fullFileInfo.url,
177
+ size: fullFileInfo.size,
178
+ sha512: generateSha512,
179
+ },
180
+ force,
181
+ releaseDate: metadataObj.releaseDate,
182
+ };
183
+ const extJson = '.json';
184
+ const jsonName = path.basename(cfg.output.file, extJson) + `-${platformForFilename}${extJson}`;
185
+ latestVersionInfo = item;
186
+ const updaterJsonFilePath = path.join(homeDir, cfg.output.directory, jsonName);
187
+ writeJsonSync(updaterJsonFilePath, latestVersionInfo);
188
+
189
+ // Delete cache files to prevent generated zip files from being outdated versions
190
+ if (cfg.cleanCache) {
191
+ this.tmpAppDirs.forEach(dir => {
192
+ const dirPath = path.join(homeDir, cfg.output.directory, dir);
193
+ fsPro.removeSync(dirPath);
194
+ });
195
+ }
196
+ }
197
+
198
+ generateHash(filepath = "", algorithm = "sha512", encoding = "base64") {
199
+ let data = '';
200
+ if (filepath.length == 0) {
201
+ return data;
202
+ }
203
+
204
+ if (!fs.existsSync(filepath)) {
205
+ return data;
206
+ }
207
+
208
+ console.log(chalk.blue('[ee-bin] [updater] ') + `generate ${algorithm} for filepath:${filepath}`);
209
+ try {
210
+ const buffer = fs.readFileSync(filepath);
211
+ const fsHash = crypto.createHash(algorithm);
212
+ fsHash.update(buffer);
213
+ data = fsHash.digest(encoding);
214
+ return data;
215
+ } catch (error) {
216
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: generate ${algorithm} error!`));
217
+ console.log(chalk.blue('[ee-bin] [updater] ') + chalk.red(`Error: ${error}`));
218
+ }
219
+ return data;
220
+ }
221
+ }
222
+
223
+ module.exports = {
224
+ IncrUpdater,
225
+ incrUpdater: new IncrUpdater()
196
226
  }
package/tools/move.js CHANGED
@@ -1,69 +1,69 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const fs = require('fs');
5
- const fsPro = require('fs-extra');
6
- const chalk = require('chalk');
7
- const { loadConfig, rm } = require('../lib/utils');
8
-
9
- const homeDir = process.cwd();
10
-
11
- // 移动资源
12
- function move(options = {}) {
13
- console.log('[ee-bin] [move] Start moving resources');
14
- const { config, flag } = options;
15
- const binCfg = loadConfig(config);
16
- const moveConfig = binCfg.move;
17
-
18
- let flags;
19
- const flagString = flag.trim();
20
- if (flagString.indexOf(',') !== -1) {
21
- flags = flagString.split(',');
22
- } else {
23
- flags = [flagString];
24
- }
25
-
26
- for (let i = 0; i < flags.length; i++) {
27
- let f = flags[i];
28
- let cfg = moveConfig[f];
29
-
30
- if (!cfg) {
31
- console.log(chalk.blue('[ee-bin] [move] ') + chalk.red(`Error: ${f} config does not exist` ));
32
- return;
33
- }
34
-
35
- const { src, dest, dist, target } = cfg;
36
- const source = dist ? dist : src;
37
- const destination = target ? target : dest;
38
-
39
- console.log(chalk.blue('[ee-bin] [move] ') + chalk.green(`Move flag: ${f}`));
40
- console.log(chalk.blue('[ee-bin] [move] ') + chalk.green('config:'), cfg);
41
-
42
- const srcResource = path.join(homeDir, source);
43
- if (!fs.existsSync(srcResource)) {
44
- const errorTips = chalk.bgRed('Error') + ` ${source} resource does not exist !`;
45
- console.error(errorTips);
46
- return
47
- }
48
-
49
- // clear the historical resource and copy it to the ee resource directory
50
- const destResource = path.join(homeDir, destination);
51
- if (fs.statSync(srcResource).isDirectory() && !fs.existsSync(destResource)) {
52
- fs.mkdirSync(destResource, {recursive: true, mode: 0o777});
53
- } else {
54
- rm(destResource);
55
- console.log('[ee-bin] [move] Clear history resources:', destResource);
56
- }
57
-
58
- fsPro.copySync(srcResource, destResource);
59
-
60
- // [todo] go project, special treatment of package.json, reserved only necessary
61
- console.log(`[ee-bin] [move] Copy ${srcResource} to ${destResource}`);
62
- }
63
-
64
- console.log('[ee-bin] [move] End');
65
- }
66
-
67
- module.exports = {
68
- move
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const fsPro = require('fs-extra');
6
+ const chalk = require('chalk');
7
+ const { loadConfig, rm } = require('../lib/utils');
8
+
9
+ const homeDir = process.cwd();
10
+
11
+ // 移动资源
12
+ function move(options = {}) {
13
+ console.log('[ee-bin] [move] Start moving resources');
14
+ const { config, flag } = options;
15
+ const binCfg = loadConfig(config);
16
+ const moveConfig = binCfg.move;
17
+
18
+ let flags;
19
+ const flagString = flag.trim();
20
+ if (flagString.indexOf(',') !== -1) {
21
+ flags = flagString.split(',');
22
+ } else {
23
+ flags = [flagString];
24
+ }
25
+
26
+ for (let i = 0; i < flags.length; i++) {
27
+ let f = flags[i];
28
+ let cfg = moveConfig[f];
29
+
30
+ if (!cfg) {
31
+ console.log(chalk.blue('[ee-bin] [move] ') + chalk.red(`Error: ${f} config does not exist` ));
32
+ return;
33
+ }
34
+
35
+ const { src, dest, dist, target } = cfg;
36
+ const source = dist ? dist : src;
37
+ const destination = target ? target : dest;
38
+
39
+ console.log(chalk.blue('[ee-bin] [move] ') + chalk.green(`Move flag: ${f}`));
40
+ console.log(chalk.blue('[ee-bin] [move] ') + chalk.green('config:'), cfg);
41
+
42
+ const srcResource = path.join(homeDir, source);
43
+ if (!fs.existsSync(srcResource)) {
44
+ const errorTips = chalk.bgRed('Error') + ` ${source} resource does not exist !`;
45
+ console.error(errorTips);
46
+ return
47
+ }
48
+
49
+ // clear the historical resource and copy it to the ee resource directory
50
+ const destResource = path.join(homeDir, destination);
51
+ if (fs.statSync(srcResource).isDirectory() && !fs.existsSync(destResource)) {
52
+ fs.mkdirSync(destResource, {recursive: true, mode: 0o777});
53
+ } else {
54
+ rm(destResource);
55
+ console.log('[ee-bin] [move] Clear history resources:', destResource);
56
+ }
57
+
58
+ fsPro.copySync(srcResource, destResource);
59
+
60
+ // [todo] go project, special treatment of package.json, reserved only necessary
61
+ console.log(`[ee-bin] [move] Copy ${srcResource} to ${destResource}`);
62
+ }
63
+
64
+ console.log('[ee-bin] [move] End');
65
+ }
66
+
67
+ module.exports = {
68
+ move
69
69
  }