UNPKG

@alwatr/node-fs

Version:

Enhanced file system operations in Node.js with asynchronous queue to prevent parallel writes.

5 lines (4 loc) 4.05 kB
/** 📦 @alwatr/node-fs v5.5.27 */ __dev_mode__: console.debug("📦 @alwatr/node-fs v5.5.27"); import{readFileSync as readFileSync_}from"node:fs";import{readFile as readFile_}from"node:fs/promises";import{flatString}from"@alwatr/flat-string";import{AsyncQueue}from"@alwatr/async-queue";import{createLogger}from"@alwatr/logger";var logger=createLogger("@alwatr/node-fs");var asyncQueue=new AsyncQueue;function readFileSync(path){logger.logMethodArgs?.("readFileSync","..."+path.slice(-32));try{return flatString(readFileSync_(path,{encoding:"utf-8",flag:"r"}))}catch(err){logger.error("readFileSync","read_file_failed",{path},err);throw new Error("read_file_failed",{cause:err.cause})}}function readFile(path){logger.logMethodArgs?.("readFile","..."+path.slice(-32));return asyncQueue.push(path,async()=>{try{return flatString(await readFile_(path,{encoding:"utf-8",flag:"r"}))}catch(err){logger.error("readFile","read_file_failed",{path},err);throw new Error("read_file_failed",{cause:err.cause})}})}import{writeFileSync as writeFileSync_,existsSync,mkdirSync,renameSync}from"node:fs";import{mkdir,rename,writeFile as writeFile_}from"node:fs/promises";import{dirname}from"node:path";function writeFileSync(path,content){logger.logMethodArgs?.("writeFileSync","..."+path.slice(-32));try{const pathExists=existsSync(path);if(!pathExists){const dir=dirname(path);if(!existsSync(dir)){mkdirSync(dir,{recursive:true})}}writeFileSync_(path+".tmp",content,{encoding:"utf-8",flag:"w"});if(pathExists){renameSync(path,path+".bak")}renameSync(path+".tmp",path);logger.logOther?.("writeFileSync success","..."+path.slice(-32))}catch(err){logger.error("writeFileSync","write_file_failed",{path},err);throw new Error("write_file_failed",{cause:err.cause})}}function writeFile(path,content){logger.logMethodArgs?.("writeFile","..."+path.slice(-32));return asyncQueue.push(path,async()=>{try{logger.logOther?.("writeFile start","..."+path.slice(-32));const pathExists=existsSync(path);if(!pathExists){const dir=dirname(path);if(!existsSync(dir)){await mkdir(dir,{recursive:true})}}await writeFile_(path+".tmp",content,{encoding:"utf-8",flag:"w"});if(pathExists){await rename(path,path+".bak")}await rename(path+".tmp",path);logger.logOther?.("writeFile success","..."+path.slice(-32))}catch(err){logger.error("writeFile","write_file_failed",{path},err);throw new Error("write_file_failed",{cause:err.cause})}})}function parseJson(content){try{return JSON.parse(content)}catch(err){logger.error("parseJson","invalid_json",err);throw new Error("invalid_json",{cause:err.cause})}}function jsonStringify(data){try{return JSON.stringify(data)}catch(err){logger.error("jsonStringify","stringify_failed",err);throw new Error("stringify_failed",{cause:err.cause})}}function readJson(path,sync=false){logger.logMethodArgs?.("readJson",{path:path.slice(-32),sync});if(sync===true){return parseJson(readFileSync(path))}else{return readFile(path).then(content=>parseJson(content))}}import{flatString as flatString2}from"@alwatr/flat-string";function writeJson(path,data,sync=false){logger.logMethodArgs?.("writeJson","..."+path.slice(-32));const content=flatString2(jsonStringify(data));return sync===true?writeFileSync(path,content):writeFile(path,content)}import{existsSync as existsSync2}from"node:fs";import{mkdir as mkdir2,open}from"node:fs/promises";import{dirname as dirname2}from"node:path";async function makeEmptyFile(path){logger.logMethodArgs?.("makeEmptyFile","..."+path.slice(-32));try{const pathExists=existsSync2(path);if(!pathExists){const dir=dirname2(path);if(!existsSync2(dir)){await mkdir2(dir,{recursive:true})}}await(await open(path,"w")).close()}catch(err){logger.error("makeEmptyFile","make_file_failed",{path},err);throw new Error("make_file_failed",{cause:err.cause})}}import{resolve}from"node:path";import{existsSync as existsSync3}from"node:fs";import{unlink}from"node:fs/promises";export{existsSync3 as existsSync,makeEmptyFile,readFile,readFileSync,readJson,resolve,unlink,writeFile,writeFileSync,writeJson}; //# sourceMappingURL=main.mjs.map