import{existsSync as c,readdirSync as m,lstatSync as u,mkdirSync as f,copyFileSync as p,lstat as F}from"fs";import{join as s,extname as S,basename as x,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{shaDirPathShort as P,shaFileForUrl as D}from"./crypto.js";import{reporter as b}from"./reporter/reporter.js";import{slug as N}from"./slugger.js";import $ from"./reporter/logger.js";function _(r){if(!c(r))return[];const e=[];return o(r),e;function o(t){try{m(t).forEach(n=>{let i=s(t,n);u(i).isDirectory()?o(i):e.push(i)})}catch(n){$.error(`Error reading all file names in ${t}: `+n.message)}}}function I(r,e,o){const t=E(r,e);if(!c(t))throw new v(`File not found at ${t}`,e);c(s(o,a))||f(s(o,a),{recursive:!0});const n=S(t),i=[N(x(t,n)),D(t),P(h(e))].join(".").concat(n),l=s(o,a,i),y=s(a,i);try{if(p(t,l),!c(l))throw new Error(`Cannot copy file ${e}`);return w(y)}catch(d){b.panicOnBuild(d.message)}}function g(r,e){let o=[];if(c(r)){if(c(e)||f(e,{recursive:!0}),!u(r).isDirectory())throw new Error(`Source path must be a directory, but ${r} provided`);o=m(r),o.forEach(t=>{const n=s(r,t);if(u(n).isDirectory())g(n,s(e,t));else{const i=s(e,t);p(n,i)}})}}function T(r){return f(h(r),{recursive:!0}),r}async function q(r){return new Promise(e=>{F(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,I as copyStaticFile,T as ensureDir,q as fileExistsAsync,_ as readFileNames};
