bynk_driver/tools/mod.rs
1//! Back-half paydown 1D.5b (#4003, Decision F): the spawning of the external
2//! tools the driver runs, in one place: `tsc`, `tsx`, `node`, `git`, and the
3//! npm, pnpm, bun and yarn installs.
4//!
5//! - [`pins`]: the versions fetched on demand (TypeScript, `tsx`).
6//! - [`ladder`]: the `tsc` and `tsx` runner ladders.
7//! - Here: the `node` and `git` commands, and [`run_to_completion`], which runs
8//! an install. What to run for each package manager (`PackageManager`'s
9//! runner and install methods) stays in [`crate::npm_install`].
10//!
11//! Each tool is found exactly as its callers always found it, so moving a
12//! spawn here changes no error text and no edge case: `node` through
13//! [`crate::probe::program_path`] for a test run ([`node_command`]) and through
14//! detection for `bynk test --inspect` ([`resolved_node_command`]), `git` by
15//! its bare name. Detection itself stays in [`crate::probe`], which wrangler
16//! and Docker also use. [`wrangler::command`] builds every wrangler spawn
17//! (1D.3a); what bynk asks wrangler is `bynk`'s `cloudflare` module.
18
19pub mod ladder;
20pub mod pins;
21pub mod wrangler;
22
23pub use ladder::{Ladder, Rung};
24
25use std::path::Path;
26use std::process::Command;
27
28/// `node`, found as [`crate::probe::program_path`] finds it: its resolved
29/// path, else the bare name.
30pub fn node_command() -> Command {
31 Command::new(crate::probe::program_path("node"))
32}
33
34/// `node` at the path detection resolves, or `None` when it is not on PATH.
35pub fn resolved_node_command() -> Option<Command> {
36 ladder::resolve_tool("node").map(Command::new)
37}
38
39/// `git`, by its bare name. A caller adds its own policy: `bynk fetch` hardens
40/// every invocation (`fetch::git`).
41pub fn git_command() -> Command {
42 Command::new("git")
43}
44
45/// Run `program args` in `dir` to completion. `Ok((success, output))`, where the
46/// output is the tool's stderr, or its stdout when stderr is blank; `Err` when
47/// the tool could not be started.
48pub fn run_to_completion(
49 program: &Path,
50 args: &[&str],
51 dir: &Path,
52) -> Result<(bool, String), String> {
53 let out = Command::new(program)
54 .args(args)
55 .current_dir(dir)
56 .output()
57 .map_err(|e| format!("could not run `{}`: {e}", program.display()))?;
58 let mut text = String::from_utf8_lossy(&out.stderr).into_owned();
59 if text.trim().is_empty() {
60 text = String::from_utf8_lossy(&out.stdout).into_owned();
61 }
62 Ok((out.status.success(), text))
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn git_is_found_by_its_bare_name() {
71 assert_eq!(git_command().get_program(), std::ffi::OsStr::new("git"));
72 assert_eq!(git_command().get_args().count(), 0);
73 }
74
75 #[test]
76 fn node_is_found_as_program_path_finds_it() {
77 assert_eq!(
78 node_command().get_program(),
79 crate::probe::program_path("node").as_os_str()
80 );
81 }
82
83 #[test]
84 fn a_tool_that_cannot_start_is_an_error_naming_it() {
85 let missing = Path::new("/no/such/bynk-tool");
86 let err = run_to_completion(missing, &[], Path::new(".")).unwrap_err();
87 assert!(err.starts_with("could not run `"), "{err}");
88 assert!(err.contains("bynk-tool"), "{err}");
89 }
90}