#!/usr/bin/env node
import"./cli/utils/node-version-check.js";import i from"mri";import*as o from"node:path";import{tmpdir as A}from"node:os";import*as x from"node:fs";import"./server/node-fetch-polyfill.js";import{cliCommandNames as f}from"./constants/index.js";import{initPlugins as P}from"./server/plugins/lifecycle.js";import{develop as S}from"./cli/develop.js";import{build as T}from"./cli/build.js";import{eject as I}from"./cli/eject/index.js";import{PORTAL_VERSION as w}from"./server/version.js";import{beforeCommand as v}from"./server/utils/lifecycle-hooks.js";import{Store as g}from"./server/store.js";import{prepare as L}from"./cli/prepare/index.js";import{sha as k}from"./server/utils/crypto.js";import{fromCurrentDir as C}from"./server/utils/paths.js";import{loadEnvironmentVariables as R}from"./shared/env.js";import{telemetry as s}from"./cli/telemetry/index.js";import{translationsCliOpts as y}from"./cli/translations/options.js";import{generateTranslations as N}from"./cli/translations/index.js";import e from"./server/utils/reporter/logger.js";import{EntitlementsProvider as U}from"./server/entitlements/entitlements-provider.js";import{isValidPlan as V}from"./server/entitlements/is-valid-plan.js";import{stopAllCompilers as F}from"./server/esbuild/esbuild.js";const a=process.argv[2];let t;const m={alias:{d:"project-dir",t:"htmlTemplate",p:"port"},default:{"project-dir":process.env.REDOCLY_CONTENT_DIR||process.cwd(),verbose:!1,htmlTemplate:"",outdir:"public"}},G={alias:m.alias,default:{...m.default,plan:"enterprise"}},M={alias:{d:"prepareDir"},default:{verbose:!1,prepareDir:"public"}},B={boolean:["force"],alias:{f:"force",d:"project-dir"},default:{"project-dir":process.cwd()}};s.updateContext({command:a,version:w,anonymousId:"ANONYMOUS",platform:process.platform,orgSlug:process.env.ORG_SLUG,projectSlug:process.env.PROJECT_SLUG}),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 F(),s.send("cli_error_caught",{message:r.message}),e.error("Exiting due to uncaught exception"),e.panic(r))});try{a||e.panicOnContentError("Command not specified.");const r=U.instance();switch(["prepare","build","serve"].includes(a)&&await r.init(),["eject","translate"].includes(a)&&await r.init({developModePlan:"enterprise"}),a){case"develop":case"preview":const n=i(process.argv.slice(3),G),O=o.resolve(n["project-dir"]),h=o.join(A(),"redocly-public-"+k(O)),d=n.plan.toLowerCase();V(d)||e.panicOnContentError(`Invalid --plan argument value '${d}'.`),await r.init({developModePlan:d}),t=new g({contentDir:o.resolve(n["project-dir"]),outdir:h,serverOutDir:C(import.meta.url,"./server/esbuild/cache/server")}),await v(f.DEVELOP,n,t),await S(n,t);break;case"build":const l=i(process.argv.slice(3),m);t=new g({contentDir:o.resolve(l["project-dir"]),outdir:o.resolve(l.outdir),serverOutDir:C(import.meta.url,"./server/esbuild/cache/server")}),await v(f.BUILD,l,t),await T(l,t);break;case"prepare":const c=i(process.argv.slice(3),m);t=new g({contentDir:o.resolve(c["project-dir"]),outdir:o.resolve(c.outdir,"client"),serverOutDir:o.resolve(o.join(c.outdir,"server"))}),await v(f.BUILD,c,t),await L(c,t);break;case"serve":R();const b=i(process.argv.slice(3),M),D=o.resolve(b.prepareDir);s.send("cli_command_executed",{arguments:b});const j=o.join(D,"server","index.mjs");x.existsSync(j)||e.panic("Server not found. Please run `realm prepare` first"),import(j).catch(p=>{e.panic("Failed to load server",p)});break;case"eject":r.canAccessFeature("themeEjecting")||e.panicOnContentError('The "eject" command is not available for this project');const u=i(process.argv.slice(3),B),{lifecycleContext:{getConfig:E,fs:_}}=await P({outdir:"",contentDir:u["project-dir"],setGlobalConfig:()=>null});_.dispose(),s.send("cli_command_executed",{arguments:u}),await I({...u,config:await E()}),e.clearAllTimeouts();break;case"translate":if(r.canAccessFeature("l10n")){const p=i(process.argv.slice(3),y);s.send("cli_command_executed",{arguments:p}),await N(p)}else e.panicOnContentError('The "translate" command is not available for this project');break;case"--version":console.log(w);break;default:e.panicOnContentError(`Unknown command "${a}"`)}}catch(r){s.send("cli_error_caught",{message:r.message}),e.error("Exiting due to uncaught exception"),e.panic(r)}
