UNPKG

cross-7zip

Version:

Cross-platform 7-Zip for Node.js

3 lines (2 loc) 5.86 kB
#!/usr/bin/env node "use strict";var M=Object.create;var y=Object.defineProperty,C=Object.defineProperties,F=Object.getOwnPropertyDescriptor,A=Object.getOwnPropertyDescriptors,D=Object.getOwnPropertyNames,b=Object.getOwnPropertySymbols,N=Object.getPrototypeOf,Z=Object.prototype.hasOwnProperty,T=Object.prototype.propertyIsEnumerable;var O=(e,i,t)=>i in e?y(e,i,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[i]=t,c=(e,i)=>{for(var t in i||(i={}))Z.call(i,t)&&O(e,t,i[t]);if(b)for(var t of b(i))T.call(i,t)&&O(e,t,i[t]);return e},S=(e,i)=>C(e,A(i));var L=(e,i,t,s)=>{if(i&&typeof i=="object"||typeof i=="function")for(let n of D(i))!Z.call(e,n)&&n!==t&&y(e,n,{get:()=>i[n],enumerable:!(s=F(i,n))||s.enumerable});return e};var j=(e,i,t)=>(t=e!=null?M(N(e)):{},L(i||!e||!e.__esModule?y(t,"default",{value:e,enumerable:!0}):t,e));var u=(e,i,t)=>new Promise((s,n)=>{var z=d=>{try{w(t.next(d))}catch(_){n(_)}},I=d=>{try{w(t.throw(d))}catch(_){n(_)}},w=d=>d.done?s(d.value):Promise.resolve(d.value).then(z,I);w((t=t.apply(e,i)).next())});var $=j(require("yargs"));var o={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 h=require("child_process");var r=require("path"),a=(0,r.join)(__dirname,"..","7zip"),p=new Map;p.set("linux_arm",(0,r.join)(a,"linux_arm_7z"));p.set("linux_arm64",(0,r.join)(a,"linux_arm64_7z"));p.set("linux_ia32",(0,r.join)(a,"linux_ia32_7z"));p.set("linux_x64",(0,r.join)(a,"linux_x64_7z"));p.set("win32_arm64",(0,r.join)(a,"win32_arm64_7z.exe"));p.set("win32_ia32",(0,r.join)(a,"win32_ia32_7z.exe"));p.set("win32_x64",(0,r.join)(a,"win32_x64_7z.exe"));p.set("darwin_arm64",(0,r.join)(a,"darwin_7z"));p.set("darwin_ia32",(0,r.join)(a,"darwin_7z"));p.set("darwin_x64",(0,r.join)(a,"darwin_7z"));var P={maxBuffer:1/0,windowsHide:!0};function m(e,i){return u(this,null,function*(){return new Promise((t,s)=>{(0,h.execFile)(e,i,P,n=>{if(n)return s(new Error(n.message));t()})})})}function x(e,i){(0,h.execFileSync)(e,i,P)}function v(){let{platform:e,arch:i}=process;return p.get(`${e}_${i}`)}var B={archive:"",destination:""},l=class{get command(){return v()}get args(){let{archive:i,destination:t,password:s}=this._options;return["x",i,`-o${t}`,s?`-p${s}`:""].filter(n=>!!n)}constructor(i){this._options=c(c({},B),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(" ")}run(){return u(this,null,function*(){let{command:i,args:t}=this;if(!i)throw new Error("7-Zip executable not found.");return m(i,t)})}runSync(){let{command:i,args:t}=this;if(!i)throw new Error("7-Zip executable not found.");x(i,t)}};var g=require("rimraf");var H={destination:"",files:[]},f=class{get command(){return v()}get args(){let{destination:i,files:t,level:s,password:n}=this._options;return["a",i,...t,s?`-mx${Math.max(1,Math.min(s,9))}`:"",n?`-p${n}`:"",n?"-mhe=on":""].filter(z=>!!z)}constructor(i){this._options=c(c({},H),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(" ")}run(){return u(this,null,function*(){let{command:i,args:t}=this,{destination:s,overwrite:n}=this._options;if(!i)throw new Error("7-Zip executable not found.");return n?(0,g.rimraf)(s).then(()=>m(i,t)):m(i,t)})}runSync(){let{command:i,args:t}=this,{destination:s,overwrite:n}=this._options;if(!i)throw new Error("7-Zip executable not found.");n&&(0,g.rimrafSync)(s),x(i,t)}};function U(e){new f(e).runSync()}function E(e){new l(e).runSync()}$.default.scriptName("seven").usage("$0 <cmd> [args]",o.description).demandCommand(1,o.demandMsg).command("unzip <archive> [destination]",o.unzip.description,e=>e.example("$0 unzip archive.7z destination",o.unzip.example).positional("archive",{demandOption:!0,describe:o.unzip.args.archive,type:"string"}).positional("destination",{describe:o.unzip.args.destination,type:"string"}).option("password",{alias:"p",describe:o.unzip.args.password,type:"string"}),e=>{var i;e=S(c({},e),{destination:(i=e.destination)!=null?i:process.cwd()}),E(e)}).command("zip <destination> [files...]",o.zip.description,e=>e.example("$0 zip dest.7z file1 file2.txt folder",o.zip.example).positional("destination",{demandOption:!0,describe:o.zip.args.destination,type:"string"}).positional("files",{describe:o.zip.args.files,type:"string",array:!0}).option("level",{alias:"l",describe:o.zip.args.level,type:"number",choices:[1,2,3,4,5,6,7,8,9],default:5}).option("password",{alias:"p",describe:o.zip.args.password,type:"string"}).option("overwrite",{alias:"o",describe:o.zip.args.overwrite,type:"boolean"}),e=>{U(e)}).help().strict().parseSync();