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