import{existsSync as c,readdirSync as l,lstatSync as f,mkdirSync as u,copyFileSync as p,lstat as x}from"fs";import{join as s,extname as S,basename as F,dirname as h,resolve as E}from"path";import{withPathPrefix as w}from"@redocly/theme/core/utils";import{PUBLIC_ASSETS_FOLDER as a}from"../constants/common.js";import{reporter as P}from"../tools/notifiers/reporter.js";import{logger as D}from"../tools/notifiers/logger.js";import{shaDirPathShort as b}from"../utils/crypto/sha-dir-path-short.js";import{shaFileHex as N}from"../utils/crypto/sha-file-hex.js";import{slug as $}from"../../utils/slugger.js";function I(r){if(!c(r))return[];const e=[];return o(r),e;function o(t){try{l(t).forEach(n=>{let i=s(t,n);f(i).isDirectory()?o(i):e.push(i)})}catch(n){D.error(`Error reading all file names in ${t}: `+n.message)}}}async function T(r,e,o){const t=E(r,e);if(!c(t))throw new v(`File not found at ${t}`,e);c(s(o,a))||u(s(o,a),{recursive:!0});const n=S(t),i=[$(F(t,n)),N(t),b(h(e))].join(".").concat(n),m=s(o,a,i),y=s(a,i);try{if(p(t,m),!c(m))throw new Error(`Cannot copy file ${e}`);return w(y)}catch(d){await P.panicOnBuild(d.message)}}function g(r,e){let o=[];if(c(r)){if(c(e)||u(e,{recursive:!0}),!f(r).isDirectory())throw new Error(`Source path must be a directory, but ${r} provided`);o=l(r),o.forEach(t=>{const n=s(r,t);if(f(n).isDirectory())g(n,s(e,t));else{const i=s(e,t);p(n,i)}})}}function U(r){return u(h(r),{recursive:!0}),r}async function q(r){return new Promise(e=>{x(r,(o,t)=>{e(o==null&&t.isFile())})})}class v extends Error{filePath;constructor(e,o){super(e),this.name="FileNotFoundError",this.filePath=o}}export{v as FileNotFoundError,g as copyFolderRecursiveSync,T as copyStaticFile,U as ensureDir,q as fileExistsAsync,I as readFileNames};
