1 line
2.0 KiB
JavaScript
1 line
2.0 KiB
JavaScript
|
|
import EventEmitter from"node:events";import path from"node:path";import fs from"node:fs";import Files from"../files/files.mjs";import{PACKAGE_TYPE_MAPPING}from"./_module.mjs";export default class PackageInstaller extends EventEmitter{constructor(t,s,i,e,a){super(),this._type=t,this._baseDir=s,this._id=i,this._localZip=e,this._auth=a}_id;_localZip;_auth;_type;_baseDir;#t={step:null,pct:0};async install(){this.#t={step:null,pct:0};try{return(await this.#s()).vend()}catch(t){this.emit("error",t)}finally{fs.unlinkSync(this._localZip)}}async#s(){if(this._id.startsWith(".."))throw new Error("You are not allowed to install packages outside of the designated directory path");const t=path.join(this._baseDir,this._id);let s="",i=await Files.summarizeArchive(this._localZip,{manifestPath:`${this._type}.json`});if(null===i.manifest){const t=i.contents.find((t=>t.endsWith(`${this._type}.json`)));s=`${path.dirname(t)}/`,i=await Files.summarizeArchive(this._localZip,{manifestPath:`${s}${this._type}.json`})}if(!i.manifest)throw fs.unlinkSync(this._localZip),new Error(`The downloaded package ${this._id} did not contain the expected ${this._type}.json manifest file.`);const e=new(0,PACKAGE_TYPE_MAPPING[this._type])(JSON.parse(i.manifest),{installed:!1});if(e.persistentStorage&&fs.existsSync(path.join(t,"storage"))){const s=fs.readdirSync(t);for(let i of s){if("storage"===i)continue;const s=path.join(t,i);await fs.promises.rm(s,{recursive:!0})}}else await fs.promises.rm(t,{force:!0,recursive:!0});if(await Files.extractArchive(this._localZip,t,{removeRoot:s,onProgress:this.#i.bind(this)}),e.persistentStorage){const s=path.join(t,"storage");fs.existsSync(s)||fs.mkdirSync(s)}if(this._auth){const s=path.join(t,"signature.json"),i="signatureV2"in this._auth?{key:this._auth.key,package:this._id,signature:this._auth.signatureV2}:{key:this._auth.key,signature:this._auth.signature};fs.writeFileSync(s,JSON.stringify(i,null,2))}return e}#i(t,s,i){return this.emit("progress",CONST.SETUP_PACKAGE_PROGRESS.STEPS.INSTALL,s,i)}}
|