#!/usr/bin/env node
import"./cli/utils/node-version-check.js";import a from"mri";import*as o from"node:path";import{tmpdir as D}from"node:os";import*as P from"node:fs";import"./server/node-fetch-polyfill.js";import{cliCommandNames as v}from"./constants/common.js";import{initPlugins as A}from"./server/plugins/lifecycle.js";import{loadEnvVariables as x}from"./server/utils/envs/load-env-variables.js";import{PORTAL_VERSION as T}from"./server/version.js";import{logger as e}from"./server/tools/notifiers/logger.js";import{develop as y}from"./cli/develop.js";import{eject as R}from"./cli/eject/index.js";import{beforeCommand as g}from"./server/utils/lifecycle-hooks.js";import{Store as C}from"./server/store.js";import{prepare as S}from"./cli/prepare/index.js";import{sha as k}from"./server/utils/crypto.js";import{fromCurrentDir as M}from"./server/utils/paths.js";import{translationsCliOpts as V}from"./cli/translations/options.js";import{generateTranslations as I}from"./cli/translations/index.js";import{EntitlementsProvider as L}from"./server/entitlements/entitlements-provider.js";import{isValidPlan as N}from"./server/entitlements/is-valid-plan.js";import{stopAllCompilers as _}from"./server/esbuild/esbuild.js";import{copyLibsqlPrebuiltBinary as F}from"./cli/prepare/libsql/copy-prebuilt-binary.js";import{telemetry as n}from"./cli/telemetry/index.js";const c=process.argv[2];let t;const d={alias:{d:"project-dir",p:"port"},default:{"project-dir":process.env.REDOCLY_CONTENT_DIR||process.cwd(),verbose:!1,outdir:"public"}},U={alias:d.alias,default:{...d.default,plan:"enterprise"}},$={alias:{d:"prepareDir"},default:{verbose:!1,prepareDir:"public"}},q={boolean:["force"],alias:{f:"force",d:"project-dir"},default:{"project-dir":process.cwd()}};process.on("uncaughtException",async function(r){r?.code==="ERR_INVALID_STATE"?console.log("Ignore premature close error"):(e.error("Uncaught exception occurred. Stopping compilers."),await _(),n.sendCliErrorCaughtMessage({message:r.message}),e.error("Exiting due to uncaught exception"),e.panic(r))});try{c||e.panicOnContentError("Command not specified.");const r=L.instance();switch(["prepare","build","serve"].includes(c)&&await r.init(),["eject","translate"].includes(c)&&await r.init({developModePlan:"enterprise"}),c){case"develop":case"preview":const i=a(process.argv.slice(3),U),E=o.resolve(i["project-dir"]),b=o.join(D(),"redocly-public-"+k(E)),p=i.plan.toLowerCase();N(p)||e.panicOnContentError(`Invalid --plan argument value '${p}'.`),await r.init({developModePlan:p}),t=new C({contentDir:o.resolve(i["project-dir"]),outdir:b,serverOutDir:M(import.meta.url,"./server/esbuild/cache/server")}),await g(v.DEVELOP,i,t),await y(i,t);break;case"prepare":const s=a(process.argv.slice(3),d),f=o.resolve(o.join(s.outdir,"server"));t=new C({contentDir:o.resolve(s["project-dir"]),outdir:o.resolve(s.outdir,"client"),serverOutDir:f}),await g(v.PREPARE,s,t),F(f),await S(s,t);break;case"serve":x();const j=a(process.argv.slice(3),$),w=o.resolve(j.prepareDir);n.sendServeCliCommandExecutedMessage();const u=o.join(w,"server","index.mjs");P.existsSync(u)||e.panic("Server not found. Please run `realm prepare` first"),import(u).catch(l=>{e.panic("Failed to load server",l)});break;case"eject":r.canAccessFeature("themeEjecting")||e.panicOnContentError('The "eject" command is not available for this project');const m=a(process.argv.slice(3),q),{lifecycleContext:{getConfig:h,fs:O}}=await A({outdir:"",contentDir:o.resolve(m["project-dir"]),setGlobalConfig:()=>null});O.dispose(),n.sendCliCommandEjectExecutedMessage({arguments:m}),await R({...m,config:await h()}),e.clearAllTimeouts();break;case"translate":if(r.canAccessFeature("l10n")){const l=a(process.argv.slice(3),V);n.sendCliCommandTranslateExecutedMessage({arguments:l}),await I(l)}else e.panicOnContentError('The "translate" command is not available for this project');break;case"--version":console.log(T);break;default:e.panicOnContentError(`Unknown command "${c}"`)}}catch(r){n.sendCliErrorCaughtMessage({message:r.message}),e.error("Exiting due to uncaught exception"),e.panic(r)}
