import{logger as n}from"../server/tools/notifiers/logger.js";import{reporter as u}from"../server/tools/notifiers/reporter.js";import{green as g}from"../server/tools/notifiers/helpers/colors.js";import{resolveCache as v}from"../server/esbuild/plugins/themes-resolver.js";import{getBilledPagesCount as m}from"../server/utils/index.js";import{createClientCompiler as w,createServerCompiler as h}from"../server/esbuild/esbuild.js";import{initPlugins as C,runPlugins as p,writeClientEntries as y}from"../server/plugins/lifecycle.js";import{startDevServer as S}from"../server/web-server/dev-server.js";import{validateAllMarkdowns as f}from"../server/plugins/markdown/compiler.js";import{reportAllErrors as b}from"../server/utils/report-all-errors.js";import{copyMigrationsFolder as R}from"../server/providers/database/copy-migrations.js";async function G(i,e){n.patchConsole(),n.info("Starting project preview mode."),y(e),n.startTiming("initial-js"),await E(e),n.infoTime("initial-js","JavaScript compiled");const{pluginInstances:a,lifecycleContext:t}=await C({contentDir:e.contentDir,outdir:e.outdir,serverOutDir:void 0,devLogin:i["dev-login"]!==!1,setGlobalConfig:e.setGlobalConfig});e.lifecycleContext=t,R(e),await p(a,e,t,{failFast:!0}),t.cache.enableCacheCleanup();const d=async()=>{const r=performance.now();u.clearErrors(),await p(a,e,t),await f(e,t),P().then(async()=>{await b(t);const l=m(e.getAllRoutes()),o=u.summary(`  \u{1F440} Last change processed in ${g(Math.round(performance.now()-r)+"ms")}`,l);e.runListeners("errors-updated",void 0,o),e.runListeners("pages-updated",void 0,l),e.runListeners("routes-updated")})};await f(e,t),await b(t);const s=m(e.getAllRoutes());u.summary("  \u{1F440} Watching for changes...",s),e.runListeners("pages-updated",void 0,s),t.fs.watch(async()=>{v.clear(),await d()}),n.verbose("Starting development server."),await S(e,t,{port:Number(i.port||4e3)}),u.listenStdin()}async function E(i){let e=!0,a=!0;const t=r=>{r==="client"?e=!0:a=!0,i.runListeners("build-started"),e!=a&&(i.startEsbuildRun(),n.startTiming("rebuild"))},d=async r=>{r==="client"?e=!1:a=!1,!(e||a)&&(i.finishEsbuildRun(),i.buildRevision++,n.infoTime("rebuild","JavaScript re-compiled"),await i.reloadMarkdocOptions(),i.markUserCodeReady(),i.runListeners("build-updated"),i.lifecycleContext&&i.buildRevision>1&&i.lifecycleContext.fs.emitCodeUpdated())},s=[new Promise((r,l)=>{const o={label:"client",buildStart(){n.verbose("Start compiling client code."),n.startTiming(this.label)},buildEnd(){n.verboseTime(this.label,"Client compiled"),r(!0)},rebuildStart:()=>t("client"),rebuildEnd:()=>d("client")};w(i,i.outdir,"development",o).then(c=>c.watch()).catch(l)}),new Promise((r,l)=>{const o={label:"server",buildStart(){n.verbose("Start compiling server code."),n.startTiming(this.label)},buildEnd(){n.verboseTime(this.label,"Server compiled"),r(!0)},rebuildStart:()=>t("server"),rebuildEnd:()=>d("server")};h(i,i.serverOutDir,"development",o).then(c=>c.watch()).catch(l)})];await Promise.all(s),await i.reloadMarkdocOptions()}function P(){return new Promise(i=>{setTimeout(i,0)})}export{G as develop};
