use crate::{CliOverridesPatch, ConfigOverride, FlagState};
use std::{path::PathBuf, process::ExitStatus};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum AppServerCodegenTarget {
TypeScript { prettier: Option<PathBuf> },
JsonSchema,
}
impl AppServerCodegenTarget {
pub(crate) fn subcommand(&self) -> &'static str {
match self {
AppServerCodegenTarget::TypeScript { .. } => "generate-ts",
AppServerCodegenTarget::JsonSchema => "generate-json-schema",
}
}
pub(crate) fn prettier(&self) -> Option<&PathBuf> {
match self {
AppServerCodegenTarget::TypeScript { prettier } => prettier.as_ref(),
AppServerCodegenTarget::JsonSchema => None,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AppServerCodegenRequest {
pub target: AppServerCodegenTarget,
pub out_dir: PathBuf,
pub experimental: bool,
pub overrides: CliOverridesPatch,
}
impl AppServerCodegenRequest {
pub fn typescript(out_dir: impl Into<PathBuf>) -> Self {
Self {
target: AppServerCodegenTarget::TypeScript { prettier: None },
out_dir: out_dir.into(),
experimental: false,
overrides: CliOverridesPatch::default(),
}
}
pub fn json_schema(out_dir: impl Into<PathBuf>) -> Self {
Self {
target: AppServerCodegenTarget::JsonSchema,
out_dir: out_dir.into(),
experimental: false,
overrides: CliOverridesPatch::default(),
}
}
pub fn experimental(mut self, enable: bool) -> Self {
self.experimental = enable;
self
}
pub fn prettier(mut self, prettier: impl Into<PathBuf>) -> Self {
if let AppServerCodegenTarget::TypeScript { prettier: slot } = &mut self.target {
*slot = Some(prettier.into());
}
self
}
pub fn with_overrides(mut self, overrides: CliOverridesPatch) -> Self {
self.overrides = overrides;
self
}
pub fn config_override(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.overrides
.config_overrides
.push(ConfigOverride::new(key, value));
self
}
pub fn config_override_raw(mut self, raw: impl Into<String>) -> Self {
self.overrides
.config_overrides
.push(ConfigOverride::from_raw(raw));
self
}
pub fn profile(mut self, profile: impl Into<String>) -> Self {
let profile = profile.into();
self.overrides.profile = (!profile.trim().is_empty()).then_some(profile);
self
}
pub fn oss(mut self, enable: bool) -> Self {
self.overrides.oss = if enable {
FlagState::Enable
} else {
FlagState::Disable
};
self
}
pub fn enable_feature(mut self, name: impl Into<String>) -> Self {
self.overrides.feature_toggles.enable.push(name.into());
self
}
pub fn disable_feature(mut self, name: impl Into<String>) -> Self {
self.overrides.feature_toggles.disable.push(name.into());
self
}
pub fn search(mut self, enable: bool) -> Self {
self.overrides.search = if enable {
FlagState::Enable
} else {
FlagState::Disable
};
self
}
}
#[derive(Clone, Debug)]
pub struct AppServerCodegenOutput {
pub status: ExitStatus,
pub stdout: String,
pub stderr: String,
pub out_dir: PathBuf,
}