Skip to main content

bynk_driver/tools/
wrangler.rs

1//! Spawning wrangler (back-half paydown 1D.3a, #4043 Decision J): the one
2//! place a wrangler `Command` is built.
3//!
4//! An installed wrangler (on `PATH`, or the project's own
5//! `node_modules/.bin`) runs directly. A provisionable one goes through the
6//! package manager's runner (`npx --yes -p wrangler@4 wrangler`, `pnpm dlx`,
7//! `bunx`), pinned to [`super::pins::WRANGLER_SPEC`]. A missing one gives
8//! `None`.
9
10use std::process::Command;
11
12use crate::probe::Provenance;
13
14/// `wrangler <subcommand>` for `provenance`, or `None` when wrangler is
15/// missing.
16pub fn command(provenance: &Provenance, subcommand: &str) -> Option<Command> {
17    match provenance {
18        Provenance::Path(p) | Provenance::ProjectLocal(p) => {
19            let mut cmd = Command::new(p);
20            cmd.arg(subcommand);
21            Some(cmd)
22        }
23        Provenance::Runner(manager) => {
24            // Yarn classic has no runner, so a probe never reports it; npx is
25            // the historical default should one arrive.
26            let program = manager.runner_program().unwrap_or("npx");
27            // #1758: the resolved runner, so Windows runs its `.cmd` shim.
28            let mut cmd = Command::new(crate::probe::program_path(program));
29            // #524: pinned provisioning, per the repo's runner convention: an
30            // unpinned `wrangler` here meant the dev server could drift from
31            // the wrangler the tests and deploys run.
32            cmd.args(manager.runner_args(super::pins::WRANGLER_SPEC, "wrangler"))
33                .arg(subcommand);
34            Some(cmd)
35        }
36        Provenance::Missing => None,
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::npm_install::PackageManager;
44    use std::path::PathBuf;
45
46    fn argv(cmd: &Command) -> Vec<String> {
47        cmd.get_args()
48            .map(|a| a.to_string_lossy().into_owned())
49            .collect()
50    }
51
52    #[test]
53    fn an_installed_wrangler_runs_directly() {
54        let cmd = command(&Provenance::Path(PathBuf::from("/bin/wrangler")), "kv").unwrap();
55        assert_eq!(cmd.get_program(), "/bin/wrangler");
56        assert_eq!(argv(&cmd), ["kv"]);
57    }
58
59    #[test]
60    fn a_runner_provisions_the_pinned_spec() {
61        let cmd = command(&Provenance::Runner(PackageManager::Npm), "dev").unwrap();
62        assert_eq!(argv(&cmd), ["--yes", "-p", "wrangler@4", "wrangler", "dev"]);
63    }
64
65    #[test]
66    fn a_missing_wrangler_has_no_command() {
67        assert!(command(&Provenance::Missing, "dev").is_none());
68    }
69}