-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
1 lines (1 loc) · 3.12 KB
/
index.mjs
1
import fs from"fs";import path from"path";class Storage{constructor(store_prefix,isAsync){this.isAsync=isAsync;this.store_prefix=path.join(process.cwd(),store_prefix);if(this.isAsync){(async()=>{try{const stats=await fs.promises.stat(this.store_prefix);if(!stats.isDirectory())throw new Error(`${this.store_prefix} is not a directory!`)}catch(error){if(error.code==="ENOENT")await fs.promises.mkdir(this.store_prefix,{recursive:true});else throw error}})()}else{try{const stats=fs.statSync(this.store_prefix);if(!stats.isDirectory())throw new Error(`${this.store_prefix} is not a directory!`)}catch(error){if(error.code==="ENOENT"){fs.mkdirSync(this.store_prefix,{recursive:true})}else throw error}}}clear(){if(this.isAsync){(async()=>{const clearDirectory=async dir=>{for(const file of await fs.promises.readdir(dir)){const currentPath=path.join(dir,file);if((await fs.promises.stat(currentPath)).isDirectory()){await clearDirectory(currentPath);await fs.promises.rmdir(currentPath)}else{await fs.promises.unlink(currentPath)}}};try{await fs.promises.access(this.store_prefix);await clearDirectory(this.store_prefix)}catch(error){if(error.code!=="ENOENT")throw error}})()}else{(function clearDirectorySync(dir){for(const file of fs.readdirSync(dir)){const currentPath=path.join(dir,file);if(fs.statSync(currentPath).isDirectory()){clearDirectorySync(currentPath);fs.rmdirSync(currentPath)}else{fs.unlinkSync(currentPath)}}})(this.store_prefix)}}removeItem(key){const keypath=path.join(this.store_prefix,key);if(this.isAsync){(async()=>{try{await fs.promises.access(keypath);await fs.promises.unlink(keypath)}catch(error){if(error.code!=="ENOENT")throw error}})()}else{if(fs.existsSync(keypath))fs.unlinkSync(keypath)}}setItem(key,value){const keypath=path.join(this.store_prefix,key);if(this.isAsync)return fs.promises.writeFile(keypath,value,"utf8");else fs.writeFileSync(keypath,value,"utf8")}getItem(key){const keypath=path.join(this.store_prefix,key);if(this.isAsync){return(async()=>{try{await fs.promises.access(keypath);return await fs.promises.readFile(keypath,"utf8")}catch(error){if(error.code==="ENOENT")return null;throw error}})()}else{try{return fs.readFileSync(keypath,"utf8")}catch(error){if(error.code==="ENOENT")return null;throw error}}}get length(){let fileCount=0;const walk=this.isAsync?async dir=>{for(const file of await fs.promises.readdir(dir)){const filePath=path.join(dir,file);if((await fs.promises.stat(filePath)).isDirectory())await walk(filePath);else fileCount++}}:dir=>{for(const file of fs.readdirSync(dir)){const filePath=path.join(dir,file);if(fs.statSync(filePath).isDirectory())walk(filePath);else fileCount++}};if(this.isAsync){return(async()=>{await walk(this.store_prefix);return fileCount})()}else{walk(this.store_prefix);return fileCount}}}export default(store_prefix,isAsync=false)=>{return new Proxy(new Storage(store_prefix,isAsync),{get(target,prop){if(prop in target){return target[prop]}else{return target.getItem(prop)}},set(target,prop,value){if(prop in target){target[prop]=value}else{target.setItem(prop,value)}return true},deleteProperty(target,prop){if(prop in target){delete target[prop]}else{target.removeItem(prop)}return true}})};