我正在尝试使用gulp在exe和dll文件上运行upx,但是当我运行upx任务(为此我使用npm插件upx和through2)时,它要么提早完成,要么不处理所有文件,具体取决于回调的位置。
如果我将回调放在.then()中,则在运行gulp upx时会得到以下输出
[17:26:18] Using gulpfile ~/Games/Gulp/gulpfile.js
[17:26:18] Starting 'upx'...
Bugs.exe saved 555 KB (754 KB -> 199 KB)
SetupReg.exe saved 1.67 KB (8.17 KB -> 6.5 KB)
SpideyPC.exe saved 863.5 KB (1.44 MB -> 608.5 KB)
dgVoodooCpl.exe saved 120.5 KB (294 KB -> 173.5 KB)
register.exe saved 74.5 KB (127 KB -> 52.5 KB)
sysinfo.exe saved 59.34 KB (82.5 KB -> 23.16 KB)
如您所见,它仅处理exe文件,而不显示“停止'upx”
当我将回调函数放在.finally()中时,还是将其放在任务本身而不是函数中。我得到以下输出:
[17:25:54] Using gulpfile ~/Games/Gulp/gulpfile.js
[17:25:54] Starting 'upx'...
SetupReg.exe saved 1.67 KB (8.17 KB -> 6.5 KB)
[17:25:54] Finished 'upx' after 152 ms
Getinfo.dll saved 10.5 KB (23 KB -> 12.5 KB)
sysinfo.exe saved 59.34 KB (82.5 KB -> 23.16 KB)
Sysinv.dll saved 50 KB (83 KB -> 33 KB)
register.exe saved 74.5 KB (127 KB -> 52.5 KB)
dgVoodooCpl.exe saved 120.5 KB (294 KB -> 173.5 KB)
binkw32.dll saved 172.05 KB (292.55 KB -> 120.5 KB)
Bugs.exe saved 555 KB (754 KB -> 199 KB)
hv3dfx.dll saved 645.96 KB (938.5 KB -> 292.54 KB)
SpideyPC.exe saved 863.5 KB (1.44 MB -> 608.5 KB)
您可以看到它说它在实际完成之前就已经完成了,但是至少会处理所有文件。
这是我的UPX功能
function callUPX(data, cb) {
var output =
data.cwd +
"/dist/" +
data.history[0].substring(data.history[0].lastIndexOf("/") + 1);
UPX(data.history[0])
.output(output)
.start()
.then(function(stats) {
console.log(
byteDiffCB({
savings: stats.fileSize.before - stats.fileSize.after,
startSize: stats.fileSize.before,
endSize: stats.fileSize.after,
fileName: stats.name
})
);
cb();
})
.catch(function(err) {
if (err.message.includes("AlreadyPackedException")) {
} else {
console.log(err.message);
}
})
.finally();
}
这是我的大任务
gulp.task("upx", function(cb) {
return gulp.src(conf["upx"]).pipe(
through.obj(function(chunk, enc, cb) {
callUPX(chunk, cb);
})
);
});
这是我的ByteDiffCB函数
function byteDiffCB(data) {
var saved = data.savings > 0 ? " saved " : " gained ";
var color = data.savings > 0 ? "green" : "yellow";
var start = size(data.startSize);
var end = gutil.colors[color](size(data.endSize));
var report = " (" + start + " -> " + end + ")";
savings += data.savings;
return data.fileName + saved + size(Math.abs(data.savings)) + report;
}
这是我的conf const
const conf = {
obj: ["./src/**/*.obj"],
js: ["./src/**/*.js"],
html: ["./src/**/*.html"],
css: ["./src/**/*.css"],
lua: ["./src/**/*.lua"],
svg: ["./src/**/*.svg"],
img: [
"./src/**/*.png",
"./src/**/*.jpg",
"./src/**/*.jpeg",
"./src/**/*.gif"
],
upx: ["./src/**/*.exe", "./src/**/*.dll"]
};
我希望这样的输出:
[17:25:54] Using gulpfile ~/Games/Gulp/gulpfile.js
[17:25:54] Starting 'upx'...
SetupReg.exe saved 1.67 KB (8.17 KB -> 6.5 KB)
Getinfo.dll saved 10.5 KB (23 KB -> 12.5 KB)
sysinfo.exe saved 59.34 KB (82.5 KB -> 23.16 KB)
Sysinv.dll saved 50 KB (83 KB -> 33 KB)
register.exe saved 74.5 KB (127 KB -> 52.5 KB)
dgVoodooCpl.exe saved 120.5 KB (294 KB -> 173.5 KB)
binkw32.dll saved 172.05 KB (292.55 KB -> 120.5 KB)
Bugs.exe saved 555 KB (754 KB -> 199 KB)
hv3dfx.dll saved 645.96 KB (938.5 KB -> 292.54 KB)
SpideyPC.exe saved 863.5 KB (1.44 MB -> 608.5 KB)
[17:25:54] Finished 'upx' after {howevermuch} ms