@alwatr/node-fs
Version:
Enhanced file system operations in Node.js with asynchronous queue to prevent parallel writes.
6 lines (4 loc) • 2.81 kB
JavaScript
/* 📦 @alwatr/node-fs v10.0.0 */
import{readFileSync as $}from"node:fs";import{readFile as C}from"node:fs/promises";import{flatString as K}from"@alwatr/flat-string";import{AsyncQueue as Y}from"@alwatr/async-queue";import{createLogger as Z}from"@alwatr/logger";var z=Z("@alwatr/node-fs"),B=new Y;function N(v){try{return K($(v,{encoding:"utf-8",flag:"r"}))}catch(L){throw z.error("readFileSync","read_file_failed",{path:v},L),Error("read_file_failed",{cause:L.cause})}}function O(v){return B.push(v,async()=>{try{return K(await C(v,{encoding:"utf-8",flag:"r"}))}catch(L){throw z.error("readFile","read_file_failed",{path:v},L),Error("read_file_failed",{cause:L.cause})}})}import{writeFileSync as H,existsSync as D,mkdirSync as M,renameSync as P}from"node:fs";import{mkdir as W,rename as Q,writeFile as u}from"node:fs/promises";import{dirname as R}from"node:path";function T(v,L){try{let q=D(v);if(!q){let A=R(v);if(!D(A))M(A,{recursive:!0})}if(H(v+".tmp",L,{encoding:"utf-8",flag:"w"}),q)P(v,v+".bak");P(v+".tmp",v),z.logOther?.("writeFileSync success","..."+v.slice(-32))}catch(q){throw z.error("writeFileSync","write_file_failed",{path:v},q),Error("write_file_failed",{cause:q.cause})}}function U(v,L){return B.push(v,async()=>{try{z.logOther?.("writeFile start","..."+v.slice(-32));let q=D(v);if(!q){let A=R(v);if(!D(A))await W(A,{recursive:!0})}if(await u(v+".tmp",L,{encoding:"utf-8",flag:"w"}),q)await Q(v,v+".bak");await Q(v+".tmp",v),z.logOther?.("writeFile success","..."+v.slice(-32))}catch(q){throw z.error("writeFile","write_file_failed",{path:v},q),Error("write_file_failed",{cause:q.cause})}})}function G(v){try{return JSON.parse(v)}catch(L){throw z.error("parseJson","invalid_json",L),Error("invalid_json",{cause:L.cause})}}function V(v){try{return JSON.stringify(v)}catch(L){throw z.error("jsonStringify","stringify_failed",L),Error("stringify_failed",{cause:L.cause})}}function n(v,L=!1){if(L===!0)return G(N(v));else return O(v).then((q)=>G(q))}import{flatString as I}from"@alwatr/flat-string";function Lv(v,L,q=!1){let A=I(V(L));return q===!0?T(v,A):U(v,A)}import{existsSync as X}from"node:fs";import{mkdir as _,open as k}from"node:fs/promises";import{dirname as J}from"node:path";async function Gv(v){try{if(!X(v)){let q=J(v);if(!X(q))await _(q,{recursive:!0})}await(await k(v,"w")).close()}catch(L){throw z.error("makeEmptyFile","make_file_failed",{path:v},L),Error("make_file_failed",{cause:L.cause})}}import{resolve as x}from"node:path";import{existsSync as E}from"node:fs";import{unlink as b}from"node:fs/promises";var Qv=x,Rv=E,Tv=b;export{Lv as writeJson,T as writeFileSync,U as writeFile,Tv as unlink,Qv as resolve,n as readJson,N as readFileSync,O as readFile,Gv as makeEmptyFile,Rv as existsSync};
//# debugId=202A458C7E5D669864756E2164756E21
//# sourceMappingURL=main.js.map