fs-syn
Version:
Lightweight purely synchronous file operation utility for Node.js, built on native fs module with zero dependencies
2 lines (1 loc) • 5 kB
JavaScript
import*as r from"fs";import*as i from"path";import{createHash as $}from"crypto";var w="utf-8";function a(t){try{return r.accessSync(t),!0}catch{return!1}}function d(t){try{return r.statSync(t)}catch(n){throw new Error(`\u83B7\u53D6\u8DEF\u5F84\u72B6\u6001\u5931\u8D25\uFF1A${n.message}\uFF08\u8DEF\u5F84\uFF1A${t}\uFF09`)}}function g(t){if(!a(t)){g(i.dirname(t));try{r.mkdirSync(t)}catch(n){throw new Error(`\u521B\u5EFA\u76EE\u5F55\u5931\u8D25\uFF1A${n.message}\uFF08\u8DEF\u5F84\uFF1A${t}\uFF09`)}}}function E(t){return i.isAbsolute(t)?t:i.resolve(process.cwd(),t)}function j(t,n){let e=t.replace(/\\/g,"/"),s=n.replace(/\\/g,"/");if(s.includes("**")){let f=s.split("**");if(f.length>2)return!1;let[u,l]=f,p=u===""||e.startsWith(u),h=l===""||e.endsWith(l);return p&&h}let o=s.replace(/\./g,"\\.").replace(/\*/g,"[^/]*").replace(/\?/g,".");return new RegExp(`^${o}$`).test(e)}function F(t,n="",e=!1){let s=n?i.join(t,n):t,o=r.readdirSync(s,{withFileTypes:!0}),c=[];for(let f of o){if(!e&&f.name.startsWith("."))continue;let u=n?i.join(n,f.name):f.name;if(f.isDirectory()){c.push(u);let l=F(t,u,e);c.push(...l)}else c.push(u)}return c}function x(t,n,e={}){let{force:s=!1,preserveTimestamps:o=!1}=e;if(!a(t))throw new Error(`\u6E90\u8DEF\u5F84\u4E0D\u5B58\u5728\uFF1A${t}`);let c=d(t),f=c.isFile(),u=c.isDirectory();if(a(n)){if(!s)throw new Error(`\u76EE\u6807\u5DF2\u5B58\u5728\uFF08\u8BBE force: true \u8986\u76D6\uFF09\uFF1A${n}`);m(n)}if(f){g(i.dirname(n)),r.copyFileSync(t,n,o?r.constants.COPYFILE_FICLONE:0);return}if(u){g(n);let l=r.readdirSync(t);for(let p of l)x(i.join(t,p),i.join(n,p),e);o&&r.utimesSync(n,c.atime,c.mtime)}}function D(t,n,e={}){let{force:s=!1}=e;if(!a(t))throw new Error(`\u6E90\u8DEF\u5F84\u4E0D\u5B58\u5728\uFF1A${t}`);if(a(n)&&!s)throw new Error(`\u76EE\u6807\u5DF2\u5B58\u5728\uFF1A${n}`);g(i.dirname(n));try{r.renameSync(t,n)}catch(o){if(o.code==="EXDEV")x(t,n,{force:!0,preserveTimestamps:!0}),m(t);else throw new Error(`\u79FB\u52A8\u5931\u8D25\uFF1A${o.message}`)}}function k(t){g(t)}function P(t,n={}){let{cwd:e=process.cwd(),dot:s=!1,onlyFiles:o=!1,onlyDirs:c=!1}=n;if(o&&c)throw new Error("onlyFiles \u548C onlyDirs \u4E0D\u80FD\u540C\u65F6\u4E3A true");let f=Array.isArray(t)?t:[t],u=F(e,"",s),l=[];for(let p of u)f.some(h=>j(p,h))&&l.push(p);if(o||c){let p=[];for(let h of l){let b=i.join(e,h);(y(b)&&c||S(b)&&o)&&p.push(h)}return p}return l}function O(t,n,e={}){let s=Object.assign({encoding:w},e);g(i.dirname(t)),r.writeFileSync(t,n,s)}function v(t,n={}){if(!a(t))throw new Error(`\u6587\u4EF6\u4E0D\u5B58\u5728\uFF1A${t}`);if(y(t))throw new Error(`\u8DEF\u5F84\u662F\u76EE\u5F55\uFF1A${t}`);let e=Object.assign({encoding:w},n);return r.readFileSync(t,e)}function T(t,n={}){let e=v(t,n),s=typeof e=="string"?e:e.toString();try{return JSON.parse(s)}catch(o){throw new Error(`JSON\u89E3\u6790\u5931\u8D25\uFF1A${o.message}`)}}function W(t,n,e=2){O(t,JSON.stringify(n,null,e),{encoding:"utf8"})}function m(t){if(!a(t))return;let n=d(t);if(n.isFile()||n.isSymbolicLink()){r.unlinkSync(t);return}if(n.isDirectory()){let e=r.readdirSync(t);for(let s of e)m(i.join(t,s));r.rmdirSync(t)}}function A(...t){if(t.length===0)throw new Error("\u8BF7\u4F20\u5165\u8DEF\u5F84\u53C2\u6570");return a(i.join(...t))}function y(t){return a(t)&&d(t).isDirectory()}function S(t){return a(t)&&d(t).isFile()}function B(t){return a(t)&&d(t).isSymbolicLink()}function L(t){return i.isAbsolute(t)}function N(t,...n){let e=E(t),s=i.join(e,i.sep);for(let o of n){let c=E(o);if(!c.startsWith(s)&&c!==e)return!1}return!0}function C(t,n){if(!y(t))throw new Error(`\u4E0D\u662F\u76EE\u5F55\uFF1A${t}`);return n?.withFileTypes?r.readdirSync(t,{withFileTypes:!0}):r.readdirSync(t)}function J(t){return d(t)}function M(t,n){r.chmodSync(t,n)}function I(t,n,e={}){g(i.dirname(n)),r.symlinkSync(t,n,e.type)}function z(t){return r.realpathSync(t)}function R(t){if(!y(t))throw new Error(`\u4E0D\u662F\u76EE\u5F55\uFF1A${t}`);let n=r.readdirSync(t);for(let e of n)m(i.join(t,e))}function H(t){if(a(t)){if(y(t))throw new Error(`\u8DEF\u5F84\u662F\u76EE\u5F55\uFF1A${t}`);return}g(i.dirname(t)),r.writeFileSync(t,"","utf8")}function V(t,n,e={}){let s=Object.assign({encoding:w},e);g(i.dirname(t)),r.appendFileSync(t,n,s)}function X(t,n="sha256"){if(!S(t))throw new Error(`\u4E0D\u662F\u6587\u4EF6\uFF1A${t}`);return $(n).update(r.readFileSync(t)).digest("hex")}var _={defaultEncoding:w,copy:x,move:D,mkdir:k,expand:P,write:O,read:v,readJSON:T,writeJSON:W,remove:m,exists:A,isDir:y,isFile:S,isLink:B,isPathAbsolute:L,doesPathContain:N,readDir:C,stat:J,chmod:M,createSymlink:I,realpath:z,emptyDir:R,ensureFile:H,appendFile:V,hashFile:X};export{V as appendFile,M as chmod,x as copy,I as createSymlink,_ as default,w as defaultEncoding,N as doesPathContain,R as emptyDir,H as ensureFile,A as exists,P as expand,X as hashFile,y as isDir,S as isFile,B as isLink,L as isPathAbsolute,k as mkdir,D as move,v as read,C as readDir,T as readJSON,z as realpath,m as remove,J as stat,O as write,W as writeJSON};