rapid_cli/rapid_config/
config.rs1use crate::cli::{binary_dir, current_directory};
2use colorful::{Color, Colorful};
3use serde::Deserialize;
4use std::fs::read_to_string;
5use strum_macros::EnumString;
6use toml;
7
8#[derive(Debug, PartialEq, EnumString)]
9#[strum(ascii_case_insensitive)]
10#[derive(Deserialize, Clone)]
11pub enum AppType {
12 Server,
13 Remix,
14 Nextjs
15}
16
17#[derive(Deserialize, Clone)]
18pub struct ServerConfig {
19 pub port: Option<u16>,
20 pub is_logging: Option<bool>,
21 pub show_error_pages: Option<bool>,
22 pub serve_static_files: Option<bool>,
23 pub bindings_export_path: Option<String>,
24 pub routes_directory: Option<String>,
25 pub typescript_generation: Option<bool>,
26 pub typescript_generation_directory: Option<String>,
27}
28
29#[derive(Deserialize, Clone)]
30pub struct ReactFrameworkConfig {
31 pub server_port: Option<u16>,
32 pub is_logging: Option<bool>,
33 pub show_error_pages: Option<bool>,
34 pub serve_static_files: Option<bool>,
35 pub bindings_export_path: Option<String>,
36 pub typescript_generation: Option<bool>,
37 pub typescript_generation_directory: Option<String>,
38}
39
40#[derive(Deserialize, Clone)]
41pub struct Features {
44 }
46
47#[derive(Deserialize, Clone)]
48pub struct RapidConfig {
51 pub app_type: String,
52 pub features: Option<Features>,
53 pub server: Option<ServerConfig>,
54 pub remix: Option<ReactFrameworkConfig>,
55 pub nextjs: Option<ReactFrameworkConfig>,
56}
57
58pub fn find_rapid_config() -> RapidConfig {
59 let dir = current_directory();
60 find_config(&dir)
61}
62
63pub fn find_rapid_config_from_binary() -> RapidConfig {
64 let dir = binary_dir();
65 find_config(&dir)
66}
67
68pub fn is_rapid() -> bool {
71 let dir = current_directory();
72
73 let config_file_contents = read_to_string(dir.join("rapid.toml"));
74
75 if let Err(_) = config_file_contents {
77 return false;
78 }
79
80 return true;
81}
82
83pub fn find_config(dir: &std::path::PathBuf) -> RapidConfig {
84 let config_file_contents = read_to_string(dir.join("rapid.toml"));
86
87 if let Err(_) = config_file_contents {
89 eprintln!("Could not find a valid config file in the current working directory. Please make sure you are in a project scaffolded with the Rapid CLI.");
91 std::process::exit(200);
92 }
93
94 let rapid_config: RapidConfig = toml::from_str(&config_file_contents.unwrap()).unwrap();
96
97 let app_type = rapid_config.app_type.clone();
99
100 if app_type != "server" && app_type != "remix" && app_type != "nextjs" {
101 eprintln!(
102 "{}",
103 "Invalid `app_type` found in rapid.toml. The app_type can only be either `server`, `remix`, or `nextjs`."
104 .color(Color::Red)
105 .bold()
106 );
107 std::process::exit(200);
108 }
109
110 rapid_config
111}