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