cross-7zip
Version:
Cross-platform 7-Zip for Node.js
3 lines • 5.32 kB
JavaScript
;var O=Object.create;var x=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,U=Object.prototype.hasOwnProperty;var E=(e,i,t,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let n of S(i))!U.call(e,n)&&n!==t&&x(e,n,{get:()=>i[n],enumerable:!(r=Z(i,n))||r.enumerable});return e};var $=(e,i,t)=>(t=e!=null?O(P(e)):{},E(i||!e||!e.__esModule?x(t,"default",{value:e,enumerable:!0}):t,e));var _=$(require("yargs")),y=require("yargs/helpers");var s={description:"A CLI tool for compressing and extracting files using 7-Zip.",demandMsg:"You need to specify a command: zip or unzip",unzip:{description:"Extracts files from a specified zipped file.",example:'Extracts the contents of the "archive.7z" archive into the "destination" directory. If the directory does not exist, it will be created.',args:{archive:"Specifies the path to the zipped file",destination:"Specifies the path to the output directory",password:"Password for the archive"}},zip:{description:"Compresses multiple files into a zipped file.",example:'Creates a new 7z archive named "dest.7z" containing "file1", "file2.txt", and the contents of the "folder" directory.',args:{files:"Specifies the paths to the files to add to the zipped file",destination:"Specifies the path to the output zipped file",level:"Compression level, where 1 is the fastest and 9 is the best compression",password:"Password for the archive",overwrite:"Overwrites the existing ZIP file instead of updating it"}}};var l=require("child_process");var o=require("path"),a=(0,o.join)(__dirname,"..","7zip"),p=new Map;p.set("linux_arm",(0,o.join)(a,"linux_arm_7z"));p.set("linux_arm64",(0,o.join)(a,"linux_arm64_7z"));p.set("linux_ia32",(0,o.join)(a,"linux_ia32_7z"));p.set("linux_x64",(0,o.join)(a,"linux_x64_7z"));p.set("win32_arm64",(0,o.join)(a,"win32_arm64_7z.exe"));p.set("win32_ia32",(0,o.join)(a,"win32_ia32_7z.exe"));p.set("win32_x64",(0,o.join)(a,"win32_x64_7z.exe"));p.set("darwin_arm64",(0,o.join)(a,"darwin_7z"));p.set("darwin_ia32",(0,o.join)(a,"darwin_7z"));p.set("darwin_x64",(0,o.join)(a,"darwin_7z"));var v={maxBuffer:1/0,windowsHide:!0};async function d(e,i){return new Promise((t,r)=>{(0,l.execFile)(e,i,v,n=>{if(n)return r(new Error(n.message));t()})})}function f(e,i){(0,l.execFileSync)(e,i,v)}function h(){let{platform:e,arch:i}=process;return p.get(`${e}_${i}`)}var F={archive:"",destination:""},u=class{_options;get command(){return h()}get args(){let{archive:i,destination:t,password:r}=this._options;return["x",i,`-o${t}`,r?`-p${r}`:""].filter(n=>!!n)}constructor(i){this._options={...F,...i}}setArchive(i){return this._options.archive=i,this}setDestination(i){return this._options.destination=i,this}setPassword(i){return this._options.password=i,this}toString(){return[this.command,...this.args].map(i=>`"${i}"`).join(" ")}async run(){let{command:i,args:t}=this;if(!i)throw new Error("7-Zip executable not found.");return d(i,t)}runSync(){let{command:i,args:t}=this;if(!i)throw new Error("7-Zip executable not found.");f(i,t)}};var g=require("rimraf");var M={destination:"",files:[]},m=class{_options;get command(){return h()}get args(){let{destination:i,files:t,level:r,password:n}=this._options;return["a",i,...t,r?`-mx${Math.max(1,Math.min(r,9))}`:"",n?`-p${n}`:"",n?"-mhe=on":""].filter(b=>!!b)}constructor(i){this._options={...M,...i}}setDestination(i){return this._options.destination=i,this}setFiles(i){return this._options.files=i,this}setLevel(i){return this._options.level=i,this}setOverwrite(i=!0){return this._options.overwrite=i,this}setPassword(i){return this._options.password=i,this}toString(){return[this.command,...this.args].map(i=>`"${i}"`).join(" ")}async run(){let{command:i,args:t}=this,{destination:r,overwrite:n}=this._options;if(!i)throw new Error("7-Zip executable not found.");return n?(0,g.rimraf)(r).then(()=>d(i,t)):d(i,t)}runSync(){let{command:i,args:t}=this,{destination:r,overwrite:n}=this._options;if(!i)throw new Error("7-Zip executable not found.");n&&(0,g.rimrafSync)(r),f(i,t)}};function z(e){new m(e).runSync()}function w(e){new u(e).runSync()}(0,_.default)((0,y.hideBin)(process.argv)).scriptName("seven").usage("$0 <cmd> [args]",s.description).demandCommand(1,s.demandMsg).command("unzip <archive> [destination]",s.unzip.description,e=>e.example("$0 unzip archive.7z destination",s.unzip.example).positional("archive",{demandOption:!0,describe:s.unzip.args.archive,type:"string"}).positional("destination",{describe:s.unzip.args.destination,type:"string"}).option("password",{alias:"p",describe:s.unzip.args.password,type:"string"}),e=>{e={...e,destination:e.destination??process.cwd()},w(e)}).command("zip <destination> [files...]",s.zip.description,e=>e.example("$0 zip dest.7z file1 file2.txt folder",s.zip.example).positional("destination",{demandOption:!0,describe:s.zip.args.destination,type:"string"}).positional("files",{describe:s.zip.args.files,type:"string",array:!0}).option("level",{alias:"l",describe:s.zip.args.level,type:"number",choices:[1,2,3,4,5,6,7,8,9],default:5}).option("password",{alias:"p",describe:s.zip.args.password,type:"string"}).option("overwrite",{alias:"o",describe:s.zip.args.overwrite,type:"boolean"}),e=>{z(e)}).help().strict().parseSync();
//# sourceMappingURL=cli.js.map