Localbinarywrapper主要是为了解决在不同的环境下使用不同的本地二进制文件的需求。为什么不根据当前环境在线下载呢?因为网络下载会有被墙、编译等不可抗力问题。所以自己下载下来放好,然后通过wrapper使用。constpath=require('路径');constfs=require('fs-extra');classLocal_Bin_Wrapper{constructor(){this.map=newMap();}/***配置包路径*@paramurl本地二进制包的绝对路径*@paramplatfrom包平台darwin->macOswin32->windows*@returnsthis*/src(url:string,platfrom:"darwin"|"win32"):this{this.map.set(platfrom,path.resolve(url));归还这个;}//返回匹配当前平台路径的二进制地址():string{returnthis.map.get(process.platform);}//只提供一个调用方法asyncrun(cmd=["--version"]):Promise>{constpath=this.path();try{//验证是否存在//不存在则报错trycatchcapturefs.statSync(path);constresult_1=awaitexeca(path,cmd);console.log("命令:",result_1.command);如果(result_1.stderr.length>0){console.log(result_1.stderr);}if(result_1.stdout.length>0){console.log(result_1.stdout);}返回结果_1;}catch(e){console.log(`无法访问文件${path}`,e);返回Promise.reject(e);}}}调用示例import*asexecafrom"execa";consturl=path.resolve(__dirname,'../bin');constbin=newLocal_Bin_Wrapper().src(`${url}/mac/pngquant/pngquant`,'darwin').src(`${url}/win/pngquant/pngquant.exe`,'win32');//这样方便自己封装constpngquantBin=bin.path();execa(pngquantBin,['--version']);