-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
generate.rs
53 lines (44 loc) · 1.43 KB
/
generate.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::process::{Command, Stdio};
use anyhow::Result;
use crate::{child::spawn_child, cli::GenerateCommand};
fn verify_requirements() -> Result<()> {
let output = Command::new("npx")
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
match output {
Ok(result) if result.success() => Ok(()),
_ => {
Err(anyhow::anyhow!("Unable to run generate - missing requirements (npx)"))
}
}
fn call_turbo_gen(command: &str, tag: &String, raw_args: &str) -> Result<i32> {
let mut npx = Command::new("npx");
npx.arg("--yes")
.arg(format!("@turbo/gen@{}", tag))
.arg("raw")
.arg(command)
.args(["--json", raw_args])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
let child = spawn_child(npx)?;
let exit_code = child.wait()?.code().unwrap_or(2);
Ok(exit_code)
}
pub fn run(command: &GenerateCommand, tag: &String) -> Result<()> {
// ensure npx is available
verify_requirements()?;
match command {
GenerateCommand::Add(args) => {
// convert args to json
let raw_args = serde_json::to_string(args)?;
call_turbo_gen("add", tag, &raw_args)?;
}
GenerateCommand::Gen(args) => {
let raw_args = serde_json::to_string(args)?;
call_turbo_gen("generate", tag, &raw_args)?;
}
};
Ok(())
}