ee-core 2.3.0-beta.1 → 2.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.3.0-beta.1",
3
+ "version": "2.3.0",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/tools/iconGen.js CHANGED
@@ -1,162 +1,183 @@
1
+ 'use strict';
2
+
1
3
  const fs = require("fs");
2
4
  const path = require("path");
3
5
  const icongen = require("icon-gen");
4
6
 
5
- // ---> 处理参数
6
- const args = process.argv.splice(3);
7
- let params = {
8
- input: "/public/images/logo.png",
9
- output: "/build/icons/",
10
- size: "16,32,64,256,512",
11
- clear: false,
12
- imagesDir: "/public/images/",
13
- };
14
- try {
15
- const len = args.length;
16
- for (let i = 0; i < len; i++) {
17
- const arg = args[i];
18
- if (arg.match(/^-i/) || arg.match(/^-input/)) {
19
- params["input"] = args[i + 1];
20
- i++;
21
- continue;
22
- }
23
- if (arg.match(/^-o/) || arg.match(/^-output/)) {
24
- params["output"] = args[i + 1];
25
- i++;
26
- continue;
7
+ class IconGen {
8
+ constructor() {
9
+ this._init();
10
+ }
11
+
12
+ /**
13
+ * _init
14
+ */
15
+ _init() {
16
+ // ---> 处理参数
17
+ const args = process.argv.splice(3);
18
+ let params = {
19
+ input: "/public/images/logo.png",
20
+ output: "/build/icons/",
21
+ size: "16,32,64,256,512",
22
+ clear: false,
23
+ imagesDir: "/public/images/",
24
+ };
25
+ try {
26
+ const len = args.length;
27
+ for (let i = 0; i < len; i++) {
28
+ const arg = args[i];
29
+ if (arg.match(/^-i/) || arg.match(/^-input/)) {
30
+ params["input"] = args[i + 1];
31
+ i++;
32
+ continue;
33
+ }
34
+ if (arg.match(/^-o/) || arg.match(/^-output/)) {
35
+ params["output"] = args[i + 1];
36
+ i++;
37
+ continue;
38
+ }
39
+ if (arg.match(/^-s/) || arg.match(/^-size/)) {
40
+ params["size"] = args[i + 1];
41
+ i++;
42
+ continue;
43
+ }
44
+ if (arg.match(/^-c/) || arg.match(/^-clear/)) {
45
+ params["clear"] = true;
46
+ continue;
47
+ }
48
+ if (arg.match(/^-img/) || arg.match(/^-images/)) {
49
+ params["imagesDir"] = args[i + 1];
50
+ i++;
51
+ continue;
52
+ }
53
+ }
54
+ } catch (e) {
55
+ console.error("[ee-core] [tools/iconGen] args: ", args);
56
+ console.error("[ee-core] [tools/iconGen] ERROR: ", e);
57
+ throw new Error("参数错误!!");
27
58
  }
28
- if (arg.match(/^-s/) || arg.match(/^-size/)) {
29
- params["size"] = args[i + 1];
30
- i++;
31
- continue;
59
+ this.params = params;
60
+
61
+ // ---> 组装参数
62
+ console.log("[ee-core] [tools/iconGen] icon 当前路径: ", process.cwd());
63
+ this.input = path.join(process.cwd(), params.input);
64
+ this.output = path.join(process.cwd(), params.output);
65
+ this.imagesDir = path.join(process.cwd(), params.imagesDir);
66
+
67
+ const sizeList = params.size.split(",").map((item) => parseInt(item));
68
+ this.iconOptions = {
69
+ report: false,
70
+ ico: {
71
+ name: "icon",
72
+ sizes: [256],
73
+ },
74
+ favicon: {
75
+ name: "logo-",
76
+ pngSizes: sizeList,
77
+ },
78
+ };
79
+ }
80
+
81
+ /**
82
+ * 生成图标
83
+ */
84
+ generateIcons() {
85
+ console.log("[ee-core] [tools/iconGen] iconGen 开始处理生成logo图片");
86
+ if (!fs.existsSync(this.input)) {
87
+ console.error("[ee-core] [tools/iconGen] input: ", this.input);
88
+ throw new Error("输入的图片不存在或路径错误");
32
89
  }
33
- if (arg.match(/^-c/) || arg.match(/^-clear/)) {
34
- params["clear"] = true;
35
- continue;
90
+ if (!fs.existsSync(this.output)) {
91
+ fs.mkdirSync(this.output, { recursive: true });
92
+ } else {
93
+ // 清空目录
94
+ this.params.clear && this.deleteGenFile(this.output);
36
95
  }
37
- if (arg.match(/^-img/) || arg.match(/^-images/)) {
38
- params["imagesDir"] = args[i + 1];
39
- i++;
40
- continue;
96
+ if (!fs.existsSync(this.imagesDir)) {
97
+ fs.mkdirSync(this.imagesDir, { recursive: true });
41
98
  }
99
+ icongen(this.input, this.output, this.iconOptions)
100
+ .then((results) => {
101
+ console.log("[ee-core] [tools/iconGen] iconGen 已生成下方图片资源");
102
+ console.log(results);
103
+ this._renameForEE(results);
104
+ })
105
+ .catch((err) => {
106
+ console.error(err);
107
+ throw new Error("[ee-core] [tools/iconGen] iconGen 生成失败!");
108
+ });
42
109
  }
43
- } catch (e) {
44
- console.error("[ee-core] [tools/iconGen] args: ", args);
45
- console.error("[ee-core] [tools/iconGen] ERROR: ", e);
46
- throw new Error("参数错误!!");
47
- }
48
-
49
- // ---> 组装参数
50
- console.log("[ee-core] [tools/iconGen] icon 当前路径: ", process.cwd());
51
- const input = path.join(process.cwd(), params.input);
52
- const output = path.join(process.cwd(), params.output);
53
- const imagesDir = path.join(process.cwd(), params.imagesDir);
54
- const sizeList = params.size.split(",").map((item) => parseInt(item));
55
- const iconOptions = {
56
- report: false,
57
- ico: {
58
- name: "icon",
59
- sizes: [256],
60
- },
61
- favicon: {
62
- name: "logo-",
63
- pngSizes: sizeList,
64
- },
65
- };
66
110
 
67
- // 删除生成的文件(.ico .png)
68
- const deleteGenFile = (dirPath) => {
69
- if (fs.existsSync(dirPath)) {
70
- // 读取文件夹下的文件目录
71
- const files = fs.readdirSync(dirPath);
72
- files.forEach((file) => {
73
- const curPath = path.join(dirPath, file);
74
- // 判断是不是文件夹,如果是,继续递归
75
- if (fs.lstatSync(curPath).isDirectory()) {
76
- deleteGenFile(curPath);
77
- } else {
78
- // 删除文件
79
- if ([".ico", ".png"].includes(path.extname(curPath))) {
80
- fs.unlinkSync(curPath);
111
+ /**
112
+ * 删除生成的文件(.ico .png)
113
+ */
114
+ deleteGenFile(dirPath) {
115
+ if (fs.existsSync(dirPath)) {
116
+ // 读取文件夹下的文件目录
117
+ const files = fs.readdirSync(dirPath);
118
+ files.forEach((file) => {
119
+ const curPath = path.join(dirPath, file);
120
+ // 判断是不是文件夹,如果是,继续递归
121
+ if (fs.lstatSync(curPath).isDirectory()) {
122
+ this.deleteGenFile(curPath);
123
+ } else {
124
+ // 删除文件
125
+ if ([".ico", ".png"].includes(path.extname(curPath))) {
126
+ fs.unlinkSync(curPath);
127
+ }
81
128
  }
82
- }
83
- });
129
+ });
130
+ }
84
131
  }
85
- };
86
132
 
87
- // 为生成的资源重命名 (logo-32.png -> 32x32.png)
88
- function renameForEE(filesPath) {
89
- console.log("[ee-core] [tools/iconGen] iconGen 开始重新命名logo图片资源");
90
- try {
91
- const len = filesPath.length;
92
- for (let i = 0; i < len; i++) {
93
- const filePath = filesPath[i];
94
- const extname = path.extname(filePath);
95
- if ([".png"].includes(extname)) {
96
- const filename = path.basename(filePath, extname);
97
- const basename = filename.split("-")[1];
98
- const dirname = path.dirname(filePath);
99
- // 处理 tray 图标 --> 复制到 public/images 目录下
100
- if ("16" === basename) {
101
- const newName = "tray-logo" + extname;
102
- fs.copyFileSync(filePath, path.join(imagesDir, newName));
103
- console.log(`${filename}${extname} --> ${params.imagesDir}/${newName} 复制成功!`);
104
- fs.unlinkSync(filePath);
105
- continue;
106
- }
107
- // 处理 win 窗口图标 --> 复制到 public/images 目录下
108
- if ("32" === basename) {
109
- const newName = filename + extname;
110
- fs.copyFileSync(filePath, path.join(imagesDir, newName));
111
- console.log(`${filename}${extname} --> ${params.imagesDir}/${newName} 复制成功!`);
133
+ /**
134
+ * 为生成的资源重命名 (logo-32.png -> 32x32.png)
135
+ */
136
+ _renameForEE(filesPath) {
137
+ console.log("[ee-core] [tools/iconGen] iconGen 开始重新命名logo图片资源");
138
+ try {
139
+ const len = filesPath.length;
140
+ for (let i = 0; i < len; i++) {
141
+ const filePath = filesPath[i];
142
+ const extname = path.extname(filePath);
143
+ if ([".png"].includes(extname)) {
144
+ const filename = path.basename(filePath, extname);
145
+ const basename = filename.split("-")[1];
146
+ const dirname = path.dirname(filePath);
147
+ // 处理 tray 图标 --> 复制到 public/images 目录下
148
+ if ("16" === basename) {
149
+ const newName = "tray" + extname;
150
+ fs.copyFileSync(filePath, path.join(this.imagesDir, newName));
151
+ console.log(`${filename}${extname} --> ${this.params.imagesDir}/${newName} 复制成功!`);
152
+ fs.unlinkSync(filePath);
153
+ continue;
154
+ }
155
+ // 处理 win 窗口图标 --> 复制到 public/images 目录下
156
+ if ("32" === basename) {
157
+ const newName = filename + extname;
158
+ fs.copyFileSync(filePath, path.join(this.imagesDir, newName));
159
+ console.log(`${filename}${extname} --> ${this.params.imagesDir}/${newName} 复制成功!`);
160
+ }
161
+ // 重命名 --> 32x32.png
162
+ const newName = basename + "x" + basename + extname;
163
+ const newPath = path.join(dirname, newName);
164
+ fs.renameSync(filePath, newPath);
165
+ console.log(`${filename}${extname} --> ${newName} 重命名成功!`);
112
166
  }
113
- // 重命名 --> 32x32.png
114
- const newName = basename + "x" + basename + extname;
115
- const newPath = path.join(dirname, newName);
116
- fs.renameSync(filePath, newPath);
117
- console.log(`${filename}${extname} --> ${newName} 重命名成功!`);
118
167
  }
168
+ console.log("[ee-core] [tools/iconGen] iconGen 资源处理完成!");
169
+ } catch (e) {
170
+ console.error("[ee-core] [tools/iconGen] ERROR: ", e);
171
+ throw new Error("重命名logo图片资源失败!!");
119
172
  }
120
- console.log("[ee-core] [tools/iconGen] iconGen 资源处理完成!");
121
- } catch (e) {
122
- console.error("[ee-core] [tools/iconGen] ERROR: ", e);
123
- throw new Error("重命名logo图片资源失败!!");
124
173
  }
125
174
  }
126
175
 
127
- // ---> 生成图标
128
- const generateIcons = () => {
129
- console.log("[ee-core] [tools/iconGen] iconGen 开始处理生成logo图片");
130
- if (!fs.existsSync(input)) {
131
- console.error("[ee-core] [tools/iconGen] input: ", input);
132
- throw new Error("输入的图片不存在或路径错误");
133
- }
134
- if (!fs.existsSync(output)) {
135
- fs.mkdirSync(output, { recursive: true });
136
- } else {
137
- // 清空目录
138
- params.clear && deleteGenFile(output);
139
- }
140
- if (!fs.existsSync(imagesDir)) {
141
- fs.mkdirSync(imagesDir, { recursive: true });
142
- }
143
- icongen(input, output, iconOptions)
144
- .then((results) => {
145
- console.log("[ee-core] [tools/iconGen] iconGen 已生成下方图片资源");
146
- console.log(results);
147
- renameForEE(results);
148
- })
149
- .catch((err) => {
150
- console.error(err);
151
- throw new Error("[ee-core] [tools/iconGen] iconGen 生成失败!");
152
- });
153
- };
154
-
155
- // ---> 执行
156
176
  const run = () => {
157
- generateIcons();
158
- };
177
+ const i = new IconGen();
178
+ i.generateIcons();
179
+ }
159
180
 
160
181
  module.exports = {
161
182
  run,
162
- };
183
+ };
@@ -13,7 +13,7 @@ const fsPro = require('fs-extra');
13
13
  /**
14
14
  * 执行
15
15
  */
16
- run () {
16
+ run() {
17
17
  console.log('[ee-core] [tools/rd] 开始移动资源');
18
18
  const homeDir = process.cwd();
19
19
 
@@ -43,19 +43,57 @@ const fsPro = require('fs-extra');
43
43
  return
44
44
  }
45
45
 
46
- // 复制到ee资源目录
46
+ // 清空历史资源 并 复制到ee资源目录
47
47
  const eeResourceDir = path.join(homeDir, 'public', 'dist');
48
-
49
- // 清空历史资源
50
- fs.rmdirSync(eeResourceDir, {recursive: true});
48
+ if (!fs.existsSync(eeResourceDir)) {
49
+ fs.mkdirSync(eeResourceDir, {recursive: true, mode: 0o777});
50
+ }
51
+ this._rmFolder(eeResourceDir);
51
52
  console.log('[ee-core] [tools/rd] 清空历史资源:', eeResourceDir);
52
53
 
53
54
  fsPro.copySync(sourceDir, eeResourceDir);
54
55
  console.log('[ee-core] [tools/rd] 复制资源到:', eeResourceDir);
55
-
56
56
  console.log('[ee-core] [tools/rd] 结束');
57
- }
58
- }
57
+ },
59
58
 
59
+ /**
60
+ * 删除文件夹
61
+ */
62
+ _rmFolder(folder) {
63
+ const nodeVersion = (process.versions && process.versions.node) || null;
64
+ if (nodeVersion && this._compareVersion(nodeVersion, '14.14.0') == 1) {
65
+ fs.rmSync(folder, {recursive: true});
66
+ } else {
67
+ fs.rmdirSync(folder, {recursive: true});
68
+ }
69
+ },
60
70
 
71
+ /**
72
+ * 版本号比较
73
+ */
74
+ _compareVersion(v1, v2) {
75
+ v1 = v1.split('.')
76
+ v2 = v2.split('.')
77
+ const len = Math.max(v1.length, v2.length)
61
78
 
79
+ while (v1.length < len) {
80
+ v1.push('0')
81
+ }
82
+ while (v2.length < len) {
83
+ v2.push('0')
84
+ }
85
+
86
+ for (let i = 0; i < len; i++) {
87
+ const num1 = parseInt(v1[i])
88
+ const num2 = parseInt(v2[i])
89
+
90
+ if (num1 > num2) {
91
+ return 1
92
+ } else if (num1 < num2) {
93
+ return -1
94
+ }
95
+ }
96
+
97
+ return 0
98
+ }
99
+ }