Skip to content

Commit

Permalink
feat(cli): added --verbose for easier logging
Browse files Browse the repository at this point in the history
Note that this will *not* print server logs in testing.
  • Loading branch information
arctic-hen7 committed Feb 7, 2023
1 parent a8afc7d commit 10cd48b
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/perseus-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub async fn get_router<M: MutableStore + 'static, T: TranslationsManager + 'sta
turbine: &'static Turbine<M, T>,
opts: ServerOptions,
) -> Router {
dbg!("Server started!");

let router = Router::new()
// --- File handlers ---
.route(
Expand Down
7 changes: 5 additions & 2 deletions packages/perseus-cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn build_internal(
cargo_browser_args,
wasm_bindgen_args,
wasm_opt_args,
verbose,
..
} = global_opts.clone();
wasm_release_rustflags.push_str(" --cfg=client");
Expand Down Expand Up @@ -94,7 +95,8 @@ pub fn build_internal(
("CARGO_TARGET_DIR", "dist/target_engine"),
("RUSTFLAGS", "--cfg=engine"),
("CARGO_TERM_COLOR", "always")
]
],
verbose,
)?);

Ok(0)
Expand Down Expand Up @@ -146,7 +148,8 @@ pub fn build_internal(
("RUSTFLAGS", "--cfg=client"),
("CARGO_TERM_COLOR", "always"),
]
}
},
verbose,
)?);

Ok(0)
Expand Down
14 changes: 10 additions & 4 deletions packages/perseus-cli/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn cargo_check(
let Opts {
cargo_engine_args,
cargo_browser_args,
verbose,
..
} = global_opts.clone();

Expand Down Expand Up @@ -125,7 +126,8 @@ fn cargo_check(
("CARGO_TARGET_DIR", "dist/target_engine"),
("RUSTFLAGS", "--cfg=engine"),
("CARGO_TERM_COLOR", "always")
]
],
verbose,
)?);

Ok(0)
Expand All @@ -146,7 +148,8 @@ fn cargo_check(
("CARGO_TARGET_DIR", "dist/target_wasm"),
("RUSTFLAGS", "--cfg=client"),
("CARGO_TERM_COLOR", "always")
]
],
verbose,
)?);

Ok(0)
Expand All @@ -166,7 +169,9 @@ fn run_static_generation(
global_opts: &Opts,
) -> Result<i32, ExecutionError> {
let Opts {
cargo_engine_args, ..
cargo_engine_args,
verbose,
..
} = global_opts.clone();

let msg = format!(
Expand All @@ -189,7 +194,8 @@ fn run_static_generation(
("CARGO_TARGET_DIR", "dist/target_engine"),
("RUSTFLAGS", "--cfg=engine"),
("CARGO_TERM_COLOR", "always")
]
],
verbose,
)?);

Ok(0)
Expand Down
29 changes: 23 additions & 6 deletions packages/perseus-cli/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ pub static FAILURE: Emoji<'_, '_> = Emoji("❌", "failed!");
/// Runs the given command conveniently, returning the exit code. Notably, this
/// parses the given command by separating it on spaces. Returns the command's
/// output and the exit code.
///
/// If `full_logging` is set to `true`, this will share stdio with the parent
/// process, meaning the user will see all their app's logs, no matter what. If
/// not, logs will only be printed on a failure.
pub fn run_cmd(
cmd: String,
dir: &Path,
envs: Vec<(&str, &str)>,
pre_dump: impl Fn(),
full_logging: bool,
) -> Result<(String, String, i32), ExecutionError> {
// We run the command in a shell so that NPM/Yarn binaries can be recognized
// (see #5)
Expand Down Expand Up @@ -49,8 +54,13 @@ pub fn run_cmd(
// Print `stderr` and `stdout` only if there's something therein and the exit
// code is non-zero If we only print `stderr`, we can miss some things (see
// #74)
if !output.stderr.is_empty() && exit_code != 0 {
pre_dump();
//
// Or, if we're being verbose, log everything anyway
if full_logging || (!output.stderr.is_empty() && exit_code != 0) {
if !output.stderr.is_empty() && exit_code != 0 {
pre_dump()
};

std::io::stderr().write_all(&output.stdout).unwrap();
std::io::stderr().write_all(&output.stderr).unwrap();
}
Expand Down Expand Up @@ -89,15 +99,22 @@ pub fn run_stage(
spinner: &ProgressBar,
message: &str,
envs: Vec<(&str, &str)>,
full_logging: bool,
) -> Result<(String, String, i32), ExecutionError> {
let mut last_output = (String::new(), String::new());
// Run the commands
for cmd in cmds {
// We make sure all commands run in the target directory ('.perseus/' itself)
let (stdout, stderr, exit_code) = run_cmd(cmd.to_string(), target, envs.to_vec(), || {
// This stage has failed
fail_spinner(spinner, message);
})?;
let (stdout, stderr, exit_code) = run_cmd(
cmd.to_string(),
target,
envs.to_vec(),
|| {
// This stage has failed
fail_spinner(spinner, message);
},
full_logging,
)?;
last_output = (stdout, stderr);
// If we have a non-zero exit code, we should NOT continue (stderr has been
// written to the console already)
Expand Down
7 changes: 5 additions & 2 deletions packages/perseus-cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pub fn export_internal(
cargo_engine_args,
wasm_bindgen_args,
wasm_opt_args,
verbose,
mut wasm_release_rustflags,
..
} = global_opts.clone();
Expand Down Expand Up @@ -205,7 +206,8 @@ pub fn export_internal(
("CARGO_TARGET_DIR", "dist/target_engine"),
("RUSTFLAGS", "--cfg=engine"),
("CARGO_TERM_COLOR", "always")
]
],
verbose,
)?);

Ok(0)
Expand Down Expand Up @@ -257,7 +259,8 @@ pub fn export_internal(
("RUSTFLAGS", "--cfg=client"),
("CARGO_TERM_COLOR", "always"),
]
}
},
verbose,
)?);

Ok(0)
Expand Down
1 change: 1 addition & 0 deletions packages/perseus-cli/src/export_error_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn export_error_page(
("CARGO_TERM_COLOR", "always"),
],
|| {},
global_opts.verbose,
)?;

if prompt {
Expand Down
3 changes: 3 additions & 0 deletions packages/perseus-cli/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ impl Tools {
("RUSTFLAGS", "--cfg=engine"),
("CARGO_TERM_COLOR", "always"),
],
// Sure, the user *might* want logs on this process (but this will only be run the
// first time)
global_opts.verbose,
)
.map_err(|err| InstallError::LockfileGenerationFailed { source: err })?;
if exit_code != 0 {
Expand Down
6 changes: 6 additions & 0 deletions packages/perseus-cli/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ pub struct Opts {
/// should set this for CI)
#[clap(long, global = true)]
pub no_system_tools_cache: bool,
/// Shows the logs from building and serving your app no matter what (the
/// default is to only show them on a compilation/build failure); this
/// is intended mainly for end-to-end debugging, although the `snoop`
/// commands are more useful for targeted debugging
#[clap(long, global = true)]
pub verbose: bool,
}

#[derive(Parser, Clone)]
Expand Down
20 changes: 16 additions & 4 deletions packages/perseus-cli/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ fn build_server(
("CARGO_TARGET_DIR", "dist/target_engine"),
("RUSTFLAGS", "--cfg=engine"),
("CARGO_TERM_COLOR", "always")
]
],
// These are JSON logs, never print them (they're duplicated by the build logs
// anyway, we're compiling the same thing)
false,
)?);

let msgs: Vec<&str> = stdout.trim().split('\n').collect();
Expand Down Expand Up @@ -135,6 +138,7 @@ fn run_server(
exec: Arc<Mutex<String>>,
dir: PathBuf,
num_steps: u8,
verbose: bool,
) -> Result<i32, ExecutionError> {
// First off, handle any issues with the executable path
let exec_val = exec.lock().unwrap();
Expand All @@ -153,8 +157,16 @@ fn run_server(
// This needs to be provided in development, but not in production
.env("PERSEUS_ENGINE_OPERATION", "serve")
// We should be able to access outputs in case there's an error
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdout(if verbose {
Stdio::inherit()
} else {
Stdio::piped()
})
.stderr(if verbose {
Stdio::inherit()
} else {
Stdio::piped()
})
.spawn()
.map_err(|err| ExecutionError::CmdExecFailed {
cmd: server_exec_path,
Expand Down Expand Up @@ -272,7 +284,7 @@ pub fn serve(

// Now actually run that executable path if we should
if should_run {
let exit_code = run_server(Arc::clone(&exec), dir, num_steps)?;
let exit_code = run_server(Arc::clone(&exec), dir, num_steps, global_opts.verbose)?;
Ok((exit_code, None))
} else {
// The user doesn't want to run the server, so we'll give them the executable
Expand Down
4 changes: 3 additions & 1 deletion packages/perseus-cli/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn test(
let Opts {
cargo_engine_path,
cargo_engine_args,
verbose,
..
} = global_opts.clone();

Expand Down Expand Up @@ -119,7 +120,8 @@ pub fn test(
("CARGO_TERM_COLOR", "always"),
("PERSEUS_RUN_WASM_TESTS", "true"),
]
}
},
verbose,
)?);

Ok(0)
Expand Down
7 changes: 5 additions & 2 deletions packages/perseus-cli/src/tinker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub fn tinker_internal(
> {
let tools = tools.clone();
let Opts {
cargo_engine_args, ..
cargo_engine_args,
verbose,
..
} = global_opts.clone();

// Tinkering message
Expand All @@ -65,7 +67,8 @@ pub fn tinker_internal(
("CARGO_TARGET_DIR", "dist/target_engine"),
("RUSTFLAGS", "--cfg=engine"),
("CARGO_TERM_COLOR", "always")
]
],
verbose,
)?);

Ok(0)
Expand Down

0 comments on commit 10cd48b

Please sign in to comment.