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