rapid_cli/rapid_config/
config.rs

1use 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)]
41/// Eventually rapid will have something called plugins -- for now "features" are simply optional internal functionality
42/// that can be toggled by adding the desired feature to this "features" object inside of the rapid.toml config file
43pub struct Features {
44	// TODO: Add features here as needed
45}
46
47#[derive(Deserialize, Clone)]
48/// The RapidConfig file schemea
49/// # Example:
50pub 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
68// A helper function to check if the current running process is inside of a rapid application
69// aka does it have a rapid.toml
70pub fn is_rapid() -> bool {
71	let dir = current_directory();
72
73	let config_file_contents = read_to_string(dir.join("rapid.toml"));
74
75	// Check to make sure that the config file did not throw an error
76	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	// Look for the Rapid config file inside of the current working directory
85	let config_file_contents = read_to_string(dir.join("rapid.toml"));
86
87	// Check to make sure that the config file did not throw an error
88	if let Err(_) = config_file_contents {
89		// TODO: We should improve error log styling later (just uses standard exit 200 best practices for now)
90		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	// Parse/deserialize the rapid config file from the .toml file format
95	let rapid_config: RapidConfig = toml::from_str(&config_file_contents.unwrap()).unwrap();
96
97	// Before we output the config, we need to make sure that the app_type is valid (`it can only be either `server`, `remix`, or `nextjs`)
98	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}