1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use super::RapidCommand;
use crate::{
cli::{current_directory, logo, rapid_logo, Config},
constants::BOLT_EMOJI,
};
use clap::{arg, value_parser, ArgAction, ArgMatches, Command};
use colorful::{Color, Colorful};
use rust_embed::RustEmbed;
use std::{
fs::{write, File},
path::PathBuf,
thread, time,
};
#[derive(RustEmbed)]
#[folder = "src/templates/rapidUI/reactVite/"]
struct Asset;
#[derive(RustEmbed)]
#[folder = "src/templates/rapidUI/remix/"]
struct RemixAssets;
pub struct Init {}
impl RapidCommand for Init {
fn cmd() -> clap::Command {
Command::new("init")
.about("A command for initializing Rapid libraries in your projects!")
.arg(
arg!(
-vite --vite "initializes rapid-ui in your React + Vitejs project!"
)
.required(false)
.action(ArgAction::SetTrue)
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(
-remix --remix "initializes rapid-ui in your Remix.run application!"
)
.required(false)
.action(ArgAction::SetTrue)
.value_parser(value_parser!(PathBuf)),
)
}
fn execute(_: &Config, args: &ArgMatches) -> Result<(), crate::cli::CliError<'static>> {
println!("{}", logo());
parse_init_args(args);
Ok(())
}
}
fn parse_init_args(args: &ArgMatches) {
const INIT_ARGS: [&str; 2] = ["vite", "remix"];
let current_working_directory = current_directory();
for arg in INIT_ARGS {
match args.get_one::<PathBuf>(arg) {
Some(val) => {
if val == &PathBuf::from("true") {
match arg {
"vite" => {
init_vite_template(current_working_directory, arg);
break;
}
"remix" => {
init_remix_template(current_working_directory, arg);
break;
}
_ => {
println!("{} {}", "No template found. Please try '--vite' or '--remix'".color(Color::Red), arg);
break;
}
}
}
}
None => {
println!("{} {}", "No template found. Please try '--vite' or '--remix'".color(Color::Red), arg);
}
}
}
}
pub fn init_vite_template(current_working_directory: PathBuf, arg: &str) {
println!("{} {:?}...", "Initializing rapid-ui with the template".color(Color::Green), arg);
let tailwind_config_contents = Asset::get("tailwind.config.js").unwrap();
let postcss_config_contents = Asset::get("postcss.config.js").unwrap();
let index_css_contents = Asset::get("index.css").unwrap();
File::create(current_working_directory.join("tailwind.config.js")).expect("Failed to create a tailwind config file. Please try again!");
File::create(current_working_directory.join("postcss.config.js")).expect("Failed to create a postcss config file. Please try again!");
File::create(current_working_directory.join("src/index.css")).expect("Failed to create the css entrypoint file. Please try again!");
write("tailwind.config.js", std::str::from_utf8(tailwind_config_contents.data.as_ref()).unwrap())
.expect("Could not write to tailwind config file!");
write("postcss.config.js", std::str::from_utf8(postcss_config_contents.data.as_ref()).unwrap()).expect("Could not write to postcss config file!");
write("src/index.css", std::str::from_utf8(index_css_contents.data.as_ref()).unwrap()).expect("Could not write to index.css file!");
let timeout = time::Duration::from_millis(500);
thread::sleep(timeout);
println!(
"{} {} {} {}",
format!("{}", rapid_logo()).bold(),
"Success".bg_blue().color(Color::White).bold(),
BOLT_EMOJI,
"Rapid-ui has been initialized in your Vite project!"
);
}
pub fn init_remix_template(current_working_directory: PathBuf, arg: &str) {
println!("{} {:?}...", "Initializing rapid-ui with the template".color(Color::Green), arg);
let tailwind_config_contents = RemixAssets::get("tailwind.config.js").unwrap();
let index_css_contents = RemixAssets::get("index.css").unwrap();
File::create(current_working_directory.join("tailwind.config.js")).expect("Failed to create a tailwind config file. Please try again!");
File::create(current_working_directory.join("app/index.css")).expect("Failed to create the css entrypoint file. Please try again!");
write("tailwind.config.js", std::str::from_utf8(tailwind_config_contents.data.as_ref()).unwrap())
.expect("Could not write to tailwind config file!");
write("app/index.css", std::str::from_utf8(index_css_contents.data.as_ref()).unwrap()).expect("Could not write to index.css file!");
let timeout = time::Duration::from_millis(500);
thread::sleep(timeout);
println!(
"{} {} {} {}",
format!("{}", rapid_logo()).bold(),
"Success".bg_blue().color(Color::White).bold(),
BOLT_EMOJI,
"Rapid-ui has been initialized in your Remix project!"
);
}