perseus_cli/
snoop.rs

1use crate::cmd::run_cmd_directly;
2use crate::install::Tools;
3use crate::parse::{Opts, SnoopServeOpts};
4use crate::{errors::*, get_user_crate_name};
5use std::path::PathBuf;
6
7// NOTE: Cargo colors are left to the terminal directly here.
8
9/// Runs static generation processes directly so the user can see detailed logs.
10/// This is commonly used for allowing users to see `dbg!` and the like in their
11/// builder functions.
12pub fn snoop_build(dir: PathBuf, tools: &Tools, global_opts: &Opts) -> Result<i32, ExecutionError> {
13    run_cmd_directly(
14        format!(
15            "{} run {}",
16            tools.cargo_engine, global_opts.cargo_engine_args
17        ),
18        &dir,
19        vec![
20            ("PERSEUS_ENGINE_OPERATION", "build"),
21            ("CARGO_TARGET_DIR", "dist/target_engine"),
22            ("RUSTFLAGS", "--cfg=engine"),
23        ],
24    )
25}
26
27/// Runs the commands to build the user's app to Wasm directly so they can see
28/// detailed logs. This can't be used for release builds, so we don't have to
29/// worry about `wasm-opt`.
30pub fn snoop_wasm_build(
31    dir: PathBuf,
32    tools: &Tools,
33    global_opts: &Opts,
34) -> Result<i32, ExecutionError> {
35    let crate_name = get_user_crate_name(&dir)?;
36
37    println!("[NOTE]: You should expect unused code warnings here! Don't worry about them, they're just a product of the target-gating.");
38    let exit_code = run_cmd_directly(
39        format!(
40            "{} build --target wasm32-unknown-unknown {}",
41            tools.cargo_browser, global_opts.cargo_browser_args
42        ),
43        &dir,
44        vec![
45            ("CARGO_TARGET_DIR", "dist/target_wasm"),
46            ("RUSTFLAGS", "--cfg=client"),
47        ],
48    )?;
49    if exit_code != 0 {
50        return Ok(exit_code);
51    }
52    run_cmd_directly(
53        format!(
54            "{cmd} ./dist/target_wasm/wasm32-unknown-unknown/debug/{crate_name}.wasm --out-dir dist/pkg --out-name perseus_engine --target web {args}",
55            cmd=tools.wasm_bindgen,
56            args=global_opts.wasm_bindgen_args,
57            crate_name=crate_name
58        ),
59        &dir,
60        vec![("CARGO_TARGET_DIR", "dist/target_wasm")],
61    )
62}
63
64/// Runs the commands to run the server directly so the user can see detailed
65/// logs.
66pub fn snoop_server(
67    dir: PathBuf,
68    opts: &SnoopServeOpts,
69    tools: &Tools,
70    global_opts: &Opts,
71) -> Result<i32, ExecutionError> {
72    run_cmd_directly(
73        format!(
74            "{} run {}",
75            tools.cargo_engine, global_opts.cargo_engine_args
76        ),
77        &dir,
78        vec![
79            ("PERSEUS_ENGINE_OPERATION", "serve"),
80            ("CARGO_TARGET_DIR", "dist/target_engine"),
81            ("PERSEUS_HOST", &opts.host),
82            ("PERSEUS_PORT", &opts.port.to_string()),
83            ("RUSTFLAGS", "--cfg=engine"),
84        ], /* Unlike the `serve` command, we're both
85            * building and running here, so we provide
86            * the operation */
87    )
88}