Skip to content

Commit

Permalink
update args
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed May 11, 2023
1 parent 0574cb2 commit 1e6db95
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
22 changes: 9 additions & 13 deletions crates/turborepo-lib/src/cli.rs
Expand Up @@ -264,11 +264,13 @@ pub enum Command {
#[clap(long, value_enum, default_value_t = LinkTarget::RemoteCache)]
target: LinkTarget,
},
// Generate a new app / package
/// Generate a new app / package
G {
#[clap(long, default_value_t = String::from("latest"), hide = true)]
tag: String,
#[clap(subcommand)]
#[serde(flatten)]
command: Option<GenerateCommand>,
command: GenerateCommand,
},
/// Login to your Vercel account
Login {
Expand Down Expand Up @@ -307,7 +309,6 @@ pub enum Command {
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct GenerateArgs {
/// The name of the generator to run
pub generator_name: Option<String>,
Expand All @@ -317,13 +318,12 @@ pub struct GenerateArgs {
/// The root of your repository (default: directory with root turbo.json)
#[clap(short = 'r', long)]
pub root: Option<String>,
/// Arguments passed directly to generator
#[clap(short = 'a', long)]
/// Answers passed directly to generator
#[clap(short = 'a', long, value_delimiter = ' ', num_args = 1..)]
pub args: Vec<String>,
}

#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct GenerateAddArgs {
/// Name for the new workspace
#[clap(short = 'n', long)]
Expand Down Expand Up @@ -369,7 +369,7 @@ pub enum GenerateCommand {
#[clap(name = "add", aliases = &["a"])]
Add(GenerateAddArgs),
/// Run custom generators
#[clap(name = "gen", aliases = &["g"])]
#[clap(name = "generate", aliases = &["g", "gen"])]
Gen(GenerateArgs),
}

Expand Down Expand Up @@ -640,12 +640,8 @@ pub async fn run(

Ok(Payload::Rust(Ok(0)))
}
Command::G { command } => {
match command {
Some(command) => generate::run(command),
None => generate::run(&GenerateCommand::Add(GenerateAddArgs::default())),
}?;

Command::G { command, tag } => {
generate::run(command, tag)?;
Ok(Payload::Rust(Ok(0)))
}
Command::Daemon { command, idle_time } => {
Expand Down
11 changes: 6 additions & 5 deletions crates/turborepo-lib/src/commands/generate.rs
Expand Up @@ -25,10 +25,10 @@ fn verify_requirements() -> Result<()> {
}
}

fn call_turbo_gen(command: &str, raw_args: &str) -> Result<i32> {
fn call_turbo_gen(command: &str, tag: &String, raw_args: &str) -> Result<i32> {
let mut npx = Command::new("npx");
npx.arg("--yes")
.arg("@turbo/gen@latest")
.arg(format!("@turbo/gen@{}", tag))
.arg("raw")
.arg(command)
.args(["--json", raw_args])
Expand All @@ -40,19 +40,20 @@ fn call_turbo_gen(command: &str, raw_args: &str) -> Result<i32> {
Ok(exit_code)
}

pub fn run(command: &GenerateCommand) -> Result<()> {
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", &raw_args)?;
call_turbo_gen("add", tag, &raw_args)?;
}
GenerateCommand::Gen(args) => {
let raw_args = serde_json::to_string(args)?;
call_turbo_gen("generate", &raw_args)?;
println!("{:?}", raw_args);
call_turbo_gen("generate", tag, &raw_args)?;
}
};

Expand Down

0 comments on commit 1e6db95

Please sign in to comment.