import{otelTelemetry as _}from"../telemetry/otel/telemetry.js";_.init();import h from"node:path";import{unlink as J}from"node:fs/promises";import{writeFileSync as y}from"node:fs";import{createClientCompiler as L,createNodeBundleCompiler as j}from"../../server/esbuild/esbuild.js";import{initPlugins as k,runPlugins as U,writeClientEntries as F}from"../../server/plugins/lifecycle.js";import{promiseMapLimit as M,ensureDir as E,logger as e,reporter as D,readEnvVariable as W}from"../../server/utils/index.js";import{exit as B}from"../../server/utils/process.js";import{sendCliCommandExecution as N,telemetry as o}from"../telemetry/index.js";import{validateAllMarkdowns as z}from"../../server/plugins/markdown/compiler.js";import{copyEnvFiles as G}from"./copy-env-files.js";import{EsbuildError as a,esbuildLogger as c}from"../../server/esbuild/esbuild-logger.js";import{reportAllErrors as I}from"../../server/utils/report-all-errors.js";import{collectAnalytics as V}from"./analytics/collect-analytics.js";async function si(p,i){e.info("Starting project build.");const R=e.startTiming();F(i);const m=await L(i,i.outdir,"production"),l=await j(i,i.serverOutDir,"production");let t;try{const r=e.startTiming(),s=await m.rebuild().catch(n=>{throw new a(n)});Array.isArray(s.warnings)&&c(new a({warnings:s.warnings}));const w=e.infoTime(r,"Client JavaScript compiled");w&&o.send("timing_performed",w);const x=e.startTiming(),T=await l.rebuild().catch(n=>{throw new a(n)});Array.isArray(T.warnings)&&c(new a({warnings:T.warnings}));const v=e.infoTime(x,"Server JavaScript compiled");v&&o.send("timing_performed",v),await i.reloadMarkdocOptions();let C;({pluginInstances:C,lifecycleContext:t}=await k({outdir:i.outdir,serverOutDir:i.serverOutDir,contentDir:i.contentDir,devLogin:p.devLogin,setGlobalConfig:i.setGlobalConfig})),t.fs.dispose(),i.lifecycleContext=t,await U(C,i,t),V(i,t).catch(()=>{}),await z(i,t),await I(t),N(p,t,i);const P=e.startTiming();e.info("Re-compiling JavaScript with user code"),(await Promise.all([m.rebuild(),l.rebuild()]).catch(n=>{throw new a(n)})).forEach(({warnings:n})=>{Array.isArray(n)&&c(new a({warnings:n}))});const b=e.infoTime(P,"JavaScript re-compiled");b&&o.send("timing_performed",b),i.markUserCodeReady()}catch(r){r instanceof a&&c(r)}finally{await m.dispose(),await l.dispose()}D.printErrors(),e.info("Rendering pages...");const S=e.startTiming(),A=[...i.getAllRoutes(),...i.getAllApiRoutes()];await M(A,15,r=>i.writeRouteStaticData(r,t));const d=e.successTime(S,"Page static data prepared.");d&&o.send("timing_performed",d);const O=e.startTiming();E(i.serverOutDir),y(h.join(i.serverOutDir,"store.json"),JSON.stringify(i.toJson())),e.successTime(O,"Store serialized and written."),G(i);const u=i.searchEngine;if(u){e.info("Writing out search data...");const r=e.startTiming();await u.export(i.outdir);const s=e.successTime(r,"Search data written.");s&&o.send("timing_performed",s)}const f=W("REDOCLY_PROBLEMS_OUTPUT_FILE");if(f){const r=e.startTiming();y(E(f),JSON.stringify(D.getProblems()),"utf-8"),e.successTime(r,"Broken links written.")}await J(h.join(i.serverOutDir,"index.css"));const g=e.successTime(R,"All done");g&&o.send("timing_performed",g),B(0)}export{si as prepare};
