@ives_xxz/packages 1.0.8 → 1.0.10
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.
|
@@ -27,6 +27,7 @@ const ConfigManager = {
|
|
|
27
27
|
get() {
|
|
28
28
|
const projectPath = Editor.Project.path || Editor.projectPath,
|
|
29
29
|
configFilePath = Path.join(projectPath, configFileDir, configFileName);
|
|
30
|
+
Editor.log(`读取配置文件:${configFilePath}`);
|
|
30
31
|
let config = null;
|
|
31
32
|
if (Fs.existsSync(configFilePath)) {
|
|
32
33
|
config = JSON.parse(Fs.readFileSync(configFilePath, 'utf8'));
|
|
@@ -22,7 +22,6 @@ const EXTENSION_NAME = translate('name');
|
|
|
22
22
|
const internalPath = Path.normalize('assets/internal/');
|
|
23
23
|
|
|
24
24
|
module.exports = {
|
|
25
|
-
|
|
26
25
|
/**
|
|
27
26
|
* 项目路径
|
|
28
27
|
* @type {string}
|
|
@@ -64,7 +63,6 @@ module.exports = {
|
|
|
64
63
|
* @type {{ [key: string]: Function }}
|
|
65
64
|
*/
|
|
66
65
|
messages: {
|
|
67
|
-
|
|
68
66
|
/**
|
|
69
67
|
* 打开设置面板
|
|
70
68
|
*/
|
|
@@ -74,7 +72,7 @@ module.exports = {
|
|
|
74
72
|
|
|
75
73
|
/**
|
|
76
74
|
* 读取配置
|
|
77
|
-
* @param {any} event
|
|
75
|
+
* @param {any} event
|
|
78
76
|
*/
|
|
79
77
|
'read-config'(event) {
|
|
80
78
|
const config = ConfigManager.get();
|
|
@@ -83,15 +81,14 @@ module.exports = {
|
|
|
83
81
|
|
|
84
82
|
/**
|
|
85
83
|
* 保存配置
|
|
86
|
-
* @param {any} event
|
|
87
|
-
* @param {any} config
|
|
84
|
+
* @param {any} event
|
|
85
|
+
* @param {any} config
|
|
88
86
|
*/
|
|
89
87
|
'save-config'(event, config) {
|
|
90
88
|
const configFilePath = ConfigManager.set(config);
|
|
91
89
|
Editor.log(`[${EXTENSION_NAME}]`, translate('configSaved'), configFilePath);
|
|
92
90
|
event.reply(null, true);
|
|
93
91
|
},
|
|
94
|
-
|
|
95
92
|
},
|
|
96
93
|
|
|
97
94
|
/**
|
|
@@ -116,10 +113,10 @@ module.exports = {
|
|
|
116
113
|
},
|
|
117
114
|
|
|
118
115
|
/**
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
116
|
+
* 构建开始回调
|
|
117
|
+
* @param {BuildOptions} options
|
|
118
|
+
* @param {Function} callback
|
|
119
|
+
*/
|
|
123
120
|
onBuildStart(options, callback) {
|
|
124
121
|
const config = ConfigManager.get();
|
|
125
122
|
if (config && config.enabled) {
|
|
@@ -133,8 +130,8 @@ module.exports = {
|
|
|
133
130
|
|
|
134
131
|
/**
|
|
135
132
|
* 构建完成回调
|
|
136
|
-
* @param {BuildOptions} options
|
|
137
|
-
* @param {Function} callback
|
|
133
|
+
* @param {BuildOptions} options
|
|
134
|
+
* @param {Function} callback
|
|
138
135
|
*/
|
|
139
136
|
async onBuildFinished(options, callback) {
|
|
140
137
|
const config = ConfigManager.get();
|
|
@@ -151,7 +148,7 @@ module.exports = {
|
|
|
151
148
|
|
|
152
149
|
// 获取压缩引擎路径
|
|
153
150
|
const platform = Os.platform(),
|
|
154
|
-
pngquantPath = this.pngquantPath = Path.join(__dirname, enginePathMap[platform]);
|
|
151
|
+
pngquantPath = (this.pngquantPath = Path.join(__dirname, enginePathMap[platform]));
|
|
155
152
|
// 设置引擎文件的执行权限(仅 macOS)
|
|
156
153
|
if (pngquantPath && platform === 'darwin') {
|
|
157
154
|
if (Fs.statSync(pngquantPath).mode != 33261) {
|
|
@@ -181,16 +178,20 @@ module.exports = {
|
|
|
181
178
|
compressOptions = `${qualityParam} ${speedParam} ${skipParam} ${outputParam} ${writeParam}`;
|
|
182
179
|
|
|
183
180
|
// 需要排除的文件夹
|
|
184
|
-
this.excludeFolders = config.excludeFolders
|
|
181
|
+
this.excludeFolders = config.excludeFolders
|
|
182
|
+
? config.excludeFolders.map((value) => Path.normalize(value))
|
|
183
|
+
: [];
|
|
185
184
|
// 需要排除的文件
|
|
186
|
-
this.excludeFiles = config.excludeFiles
|
|
185
|
+
this.excludeFiles = config.excludeFiles
|
|
186
|
+
? config.excludeFiles.map((value) => Path.normalize(value))
|
|
187
|
+
: [];
|
|
187
188
|
|
|
188
189
|
// 重置日志
|
|
189
190
|
this.logger = {
|
|
190
191
|
successCount: 0,
|
|
191
192
|
failedCount: 0,
|
|
192
193
|
successInfo: '',
|
|
193
|
-
failedInfo: ''
|
|
194
|
+
failedInfo: '',
|
|
194
195
|
};
|
|
195
196
|
|
|
196
197
|
// 开始压缩
|
|
@@ -230,15 +231,17 @@ module.exports = {
|
|
|
230
231
|
return;
|
|
231
232
|
}
|
|
232
233
|
// 加入压缩队列
|
|
233
|
-
tasks.push(
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
234
|
+
tasks.push(
|
|
235
|
+
new Promise((res) => {
|
|
236
|
+
const sizeBefore = stats.size / 1024,
|
|
237
|
+
command = `"${pngquantPath}" ${options} -- "${filePath}"`;
|
|
238
|
+
// pngquant $OPTIONS -- "$FILE"
|
|
239
|
+
exec(command, (error, stdout, stderr) => {
|
|
240
|
+
this.recordResult(error, sizeBefore, filePath);
|
|
241
|
+
res();
|
|
242
|
+
});
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
242
245
|
};
|
|
243
246
|
FileUtils.map(srcPath, handler);
|
|
244
247
|
await Promise.all(tasks);
|
|
@@ -254,28 +257,28 @@ module.exports = {
|
|
|
254
257
|
return false;
|
|
255
258
|
}
|
|
256
259
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
260
|
+
// 获取资源的原始路径
|
|
261
|
+
const basename = Path.basename(path);
|
|
262
|
+
const uuid = basename.slice(0, basename.indexOf('.'));
|
|
263
|
+
const sourcePath = Editor.assetdb.uuidToFspath(uuid);
|
|
264
|
+
|
|
265
|
+
if (sourcePath) {
|
|
266
|
+
const sourceDir = Path.dirname(sourcePath);
|
|
267
|
+
const sourceBasename = Path.basename(sourcePath, '.png');
|
|
268
|
+
|
|
269
|
+
const spineFiles = [
|
|
270
|
+
Path.join(sourceDir, sourceBasename + '.json'),
|
|
271
|
+
Path.join(sourceDir, sourceBasename + '.atlas'),
|
|
272
|
+
Path.join(sourceDir, sourceBasename + '.skel'),
|
|
273
|
+
];
|
|
274
|
+
|
|
275
|
+
for (const spineFile of spineFiles) {
|
|
276
|
+
if (Fs.existsSync(spineFile)) {
|
|
277
|
+
Editor.log('找到spine文件图片:', sourceDir + sourceBasename);
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
276
280
|
}
|
|
277
281
|
}
|
|
278
|
-
}
|
|
279
282
|
|
|
280
283
|
// 排除指定文件夹和文件
|
|
281
284
|
const assetPath = this.getAssetPath(path);
|
|
@@ -301,8 +304,8 @@ module.exports = {
|
|
|
301
304
|
|
|
302
305
|
/**
|
|
303
306
|
* 获取资源源路径
|
|
304
|
-
* @param {string} filePath
|
|
305
|
-
* @return {string}
|
|
307
|
+
* @param {string} filePath
|
|
308
|
+
* @return {string}
|
|
306
309
|
*/
|
|
307
310
|
getAssetPath(filePath) {
|
|
308
311
|
// 获取源路径(图像在项目中的实际路径)
|
|
@@ -325,18 +328,22 @@ module.exports = {
|
|
|
325
328
|
*/
|
|
326
329
|
recordResult(error, sizeBefore, filePath) {
|
|
327
330
|
const log = this.logger;
|
|
331
|
+
// 获取相对路径用于显示
|
|
332
|
+
const relativePath = filePath.replace(this.projectPath, '');
|
|
333
|
+
|
|
328
334
|
if (!error) {
|
|
329
335
|
// 成功
|
|
330
336
|
const fileName = Path.basename(filePath),
|
|
331
337
|
sizeAfter = Fs.statSync(filePath).size / 1024,
|
|
332
338
|
savedSize = sizeBefore - sizeAfter,
|
|
333
|
-
savedRatio = savedSize / sizeBefore * 100;
|
|
339
|
+
savedRatio = (savedSize / sizeBefore) * 100;
|
|
334
340
|
log.successCount++;
|
|
335
|
-
|
|
341
|
+
// 添加文件路径到成功日志中
|
|
342
|
+
log.successInfo += `\n + ${'Successful'.padEnd(13, ' ')} | ${relativePath.padEnd(50, ' ')} | ${(sizeBefore.toFixed(2) + ' KB').padEnd(13, ' ')} -> ${(sizeAfter.toFixed(2) + ' KB').padEnd(13, ' ')} | ${(savedSize.toFixed(2) + ' KB').padEnd(13, ' ')} | ${(savedRatio.toFixed(2) + '%').padEnd(20, ' ')}`;
|
|
336
343
|
} else {
|
|
337
344
|
// 失败
|
|
338
345
|
log.failedCount++;
|
|
339
|
-
log.failedInfo += `\n - ${'Failed'.padEnd(13, ' ')} | ${
|
|
346
|
+
log.failedInfo += `\n - ${'Failed'.padEnd(13, ' ')} | ${relativePath}`;
|
|
340
347
|
switch (error.code) {
|
|
341
348
|
case 98:
|
|
342
349
|
log.failedInfo += `\n ${' '.repeat(10)} - 失败原因:压缩后体积增大(已经不能再压缩了)`;
|
|
@@ -359,17 +366,16 @@ module.exports = {
|
|
|
359
366
|
*/
|
|
360
367
|
printResults() {
|
|
361
368
|
const log = this.logger,
|
|
362
|
-
header = `\n # ${'Result'.padEnd(13, ' ')} | ${'
|
|
369
|
+
header = `\n # ${'Result'.padEnd(13, ' ')} | ${'File Path'.padEnd(50, ' ')} | ${'Size Before'.padEnd(13, ' ')} -> ${'Size After'.padEnd(13, ' ')} | ${'Saved Size'.padEnd(13, ' ')} | ${'Compressibility'.padEnd(20, ' ')}`;
|
|
363
370
|
Editor.log('[PAC]', `压缩完成(${log.successCount} 张成功 | ${log.failedCount} 张失败)`);
|
|
364
371
|
Editor.log('[PAC]', '压缩日志 >>>' + header + log.successInfo + log.failedInfo);
|
|
365
372
|
},
|
|
366
|
-
|
|
367
|
-
}
|
|
373
|
+
};
|
|
368
374
|
|
|
369
375
|
/** 压缩引擎路径表 */
|
|
370
376
|
const enginePathMap = {
|
|
371
377
|
/** macOS */
|
|
372
|
-
|
|
378
|
+
darwin: 'pngquant/macos/pngquant',
|
|
373
379
|
/** Windows */
|
|
374
|
-
|
|
375
|
-
}
|
|
380
|
+
win32: 'pngquant/windows/pngquant',
|
|
381
|
+
};
|