Skip to content

Commit f66bbb9

Browse files
authored
feat(cli): ✨ added --release mode to cli (#35)
* feat(cli): ✨ added `--release` mode to cli Perseus apps can now be built for production! * refactor: 🚨 refactored to fix clippy warnings
1 parent 83e365c commit f66bbb9

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

packages/perseus-actix-web/src/initial_load.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub async fn initial_load<C: ConfigManager, T: TranslationsManager>(
137137
let mut http_res = HttpResponse::Ok();
138138
http_res.content_type("text/html");
139139
// Generate and add HTTP headers
140-
for (key, val) in template.get_headers(page_data.state.clone()) {
140+
for (key, val) in template.get_headers(page_data.state) {
141141
http_res.set_header(key.unwrap(), val);
142142
}
143143

packages/perseus-cli/src/bin/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn core(dir: PathBuf) -> Result<i32, Error> {
113113
// We don't delete `render_conf.json` because it's literally impossible for that to be the source of a problem right now
114114
delete_artifacts(dir.clone(), "static")?;
115115
delete_artifacts(dir.clone(), "pkg")?;
116-
delete_artifacts(dir.clone(), "exported")?;
116+
delete_artifacts(dir, "exported")?;
117117
} else {
118118
// This command deletes the `.perseus/` directory completely, which musn't happen if the user has ejected
119119
if has_ejected(dir.clone()) && !clean_opts.force {

packages/perseus-cli/src/build.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn build_internal(
4747
dir: PathBuf,
4848
spinners: &MultiProgress,
4949
num_steps: u8,
50+
is_release: bool,
5051
) -> Result<
5152
(
5253
ThreadHandle<impl FnOnce() -> Result<i32, ExecutionError>, Result<i32, ExecutionError>>,
@@ -80,8 +81,9 @@ pub fn build_internal(
8081
let sg_thread = spawn_thread(move || {
8182
handle_exit_code!(run_stage(
8283
vec![&format!(
83-
"{} run",
84-
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
84+
"{} run {}",
85+
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
86+
if is_release { "--release" } else { "" }
8587
)],
8688
&sg_target,
8789
&sg_spinner,
@@ -93,8 +95,9 @@ pub fn build_internal(
9395
let wb_thread = spawn_thread(move || {
9496
handle_exit_code!(run_stage(
9597
vec![&format!(
96-
"{} build --target web",
97-
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string())
98+
"{} build --target web {}",
99+
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string()),
100+
if is_release { "--release" } else { "" }
98101
)],
99102
&wb_target,
100103
&wb_spinner,
@@ -108,10 +111,10 @@ pub fn build_internal(
108111
}
109112

110113
/// Builds the subcrates to get a directory that we can serve. Returns an exit code.
111-
pub fn build(dir: PathBuf, _opts: BuildOpts) -> Result<i32, ExecutionError> {
114+
pub fn build(dir: PathBuf, opts: BuildOpts) -> Result<i32, ExecutionError> {
112115
let spinners = MultiProgress::new();
113116

114-
let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 2)?;
117+
let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 2, opts.release)?;
115118
let sg_res = sg_thread
116119
.join()
117120
.map_err(|_| ExecutionError::ThreadWaitFailed)??;

packages/perseus-cli/src/export.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub fn export_internal(
131131
dir: PathBuf,
132132
spinners: &MultiProgress,
133133
num_steps: u8,
134+
is_release: bool,
134135
) -> Result<
135136
(
136137
ThreadHandle<impl FnOnce() -> Result<i32, ExportError>, Result<i32, ExportError>>,
@@ -164,8 +165,9 @@ pub fn export_internal(
164165
let ep_thread = spawn_thread(move || {
165166
handle_exit_code!(run_stage(
166167
vec![&format!(
167-
"{} run --bin perseus-exporter",
168-
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
168+
"{} run --bin perseus-exporter {}",
169+
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
170+
if is_release { "--release" } else { "" }
169171
)],
170172
&ep_target,
171173
&ep_spinner,
@@ -177,8 +179,9 @@ pub fn export_internal(
177179
let wb_thread = spawn_thread(move || {
178180
handle_exit_code!(run_stage(
179181
vec![&format!(
180-
"{} build --target web",
181-
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string())
182+
"{} build --target web {}",
183+
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string()),
184+
if is_release { "--release" } else { "" }
182185
)],
183186
&wb_target,
184187
&wb_spinner,
@@ -192,10 +195,10 @@ pub fn export_internal(
192195
}
193196

194197
/// Builds the subcrates to get a directory that we can serve. Returns an exit code.
195-
pub fn export(dir: PathBuf, _opts: ExportOpts) -> Result<i32, ExportError> {
198+
pub fn export(dir: PathBuf, opts: ExportOpts) -> Result<i32, ExportError> {
196199
let spinners = MultiProgress::new();
197200

198-
let (ep_thread, wb_thread) = export_internal(dir.clone(), &spinners, 2)?;
201+
let (ep_thread, wb_thread) = export_internal(dir.clone(), &spinners, 2, opts.release)?;
199202
let ep_res = ep_thread
200203
.join()
201204
.map_err(|_| ExecutionError::ThreadWaitFailed)??;

packages/perseus-cli/src/parse.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ pub enum Subcommand {
3232
pub struct BuildOpts {
3333
/// Build for production
3434
#[clap(long)]
35-
release: bool,
35+
pub release: bool,
3636
}
3737
/// Exports your app to purely static files
3838
#[derive(Clap)]
3939
pub struct ExportOpts {
4040
/// Export for production
4141
#[clap(long)]
42-
release: bool,
42+
pub release: bool,
4343
}
4444
/// Serves your app (set the `$HOST` and `$PORT` environment variables to change the location it's served at)
4545
#[derive(Clap)]
@@ -52,7 +52,7 @@ pub struct ServeOpts {
5252
pub no_build: bool,
5353
/// Build and serve for production
5454
#[clap(long)]
55-
release: bool,
55+
pub release: bool,
5656
}
5757
/// Removes `.perseus/` entirely for updates or to fix corruptions
5858
#[derive(Clap)]

packages/perseus-cli/src/serve.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn build_server(
3535
spinners: &MultiProgress,
3636
did_build: bool,
3737
exec: Arc<Mutex<String>>,
38+
is_release: bool,
3839
) -> Result<
3940
ThreadHandle<impl FnOnce() -> Result<i32, ExecutionError>, Result<i32, ExecutionError>>,
4041
ExecutionError,
@@ -64,8 +65,9 @@ fn build_server(
6465
let (stdout, _stderr) = handle_exit_code!(run_stage(
6566
vec![&format!(
6667
// This sets Cargo to tell us everything, including the executable path to the server
67-
"{} build --message-format json",
68-
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
68+
"{} build --message-format json {}",
69+
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
70+
if is_release { "--release" } else { "" }
6971
)],
7072
&sb_target,
7173
&sb_spinner,
@@ -182,10 +184,16 @@ pub fn serve(dir: PathBuf, opts: ServeOpts) -> Result<i32, ExecutionError> {
182184
// We need to have a way of knowing what the executable path to the server is
183185
let exec = Arc::new(Mutex::new(String::new()));
184186
// We can begin building the server in a thread without having to deal with the rest of the build stage yet
185-
let sb_thread = build_server(dir.clone(), &spinners, did_build, Arc::clone(&exec))?;
187+
let sb_thread = build_server(
188+
dir.clone(),
189+
&spinners,
190+
did_build,
191+
Arc::clone(&exec),
192+
opts.release,
193+
)?;
186194
// Only build if the user hasn't set `--no-build`, handling non-zero exit codes
187195
if did_build {
188-
let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 4)?;
196+
let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 4, opts.release)?;
189197
let sg_res = sg_thread
190198
.join()
191199
.map_err(|_| ExecutionError::ThreadWaitFailed)??;

packages/perseus/src/translations_manager.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,11 @@ impl TranslationsManager for FsTranslationsManager {
125125
}
126126
})?,
127127
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
128-
return Err(TranslationsManagerError::NotFound {
129-
locale: locale.clone(),
130-
})
128+
return Err(TranslationsManagerError::NotFound { locale })
131129
}
132130
Err(err) => {
133131
return Err(TranslationsManagerError::ReadFailed {
134-
locale: locale.clone(),
132+
locale,
135133
source: err.into(),
136134
})
137135
}

0 commit comments

Comments
 (0)