From ac3fbe8d386daedd8ed90dc2be452b2e9db54619 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Wed, 10 Nov 2021 09:40:28 +0300 Subject: [PATCH] cli: Add `idl-ts` option to build command (#940) --- CHANGELOG.md | 1 + cli/src/lib.rs | 21 +++++++++++++++++---- cli/src/template.rs | 12 +++--------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c84ec6619..340f4ceab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ incremented for features. * cli: fix dns in NODE_OPTIONS ([#928](https://github.com/project-serum/anchor/pull/928)). * cli: output TypeScript IDL in `idl parse` subcommand ([#941](https://github.com/project-serum/anchor/pull/941)). * cli: Add fields `os` and `cpu` to npm package `@project-serum/anchor-cli` ([#976](https://github.com/project-serum/anchor/pull/976)). +* cli: Allow specify output directory for TypeScript IDL ([#940](https://github.com/project-serum/anchor/pull/940)). ## [0.18.0] - 2021-10-24 diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 95833d295..5ab0bbab0 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -67,6 +67,9 @@ pub enum Command { /// Output directory for the IDL. #[clap(short, long)] idl: Option, + /// Output directory for the TypeScript IDL. + #[clap(short = 't', long)] + idl_ts: Option, /// True if the build artifact needs to be deterministic and verifiable. #[clap(short, long)] verifiable: bool, @@ -314,6 +317,7 @@ pub fn entry(opts: Opts) -> Result<()> { Command::New { name } => new(&opts.cfg_override, name), Command::Build { idl, + idl_ts, verifiable, program_name, solana_version, @@ -321,6 +325,7 @@ pub fn entry(opts: Opts) -> Result<()> { } => build( &opts.cfg_override, idl, + idl_ts, verifiable, program_name, solana_version, @@ -513,6 +518,7 @@ fn new_program(name: &str) -> Result<()> { pub fn build( cfg_override: &ConfigOverride, idl: Option, + idl_ts: Option, verifiable: bool, program_name: Option, solana_version: Option, @@ -530,14 +536,17 @@ pub fn build( let cargo = Manifest::discover()?; - fs::create_dir_all(cfg_parent.join("target/idl"))?; - fs::create_dir_all(cfg_parent.join("target/types"))?; - let idl_out = match idl { Some(idl) => Some(PathBuf::from(idl)), None => Some(cfg_parent.join("target/idl")), }; - let idl_ts_out = Some(cfg_parent.join("target/types")); + fs::create_dir_all(idl_out.as_ref().unwrap())?; + + let idl_ts_out = match idl_ts { + Some(idl_ts) => Some(PathBuf::from(idl_ts)), + None => Some(cfg_parent.join("target/types")), + }; + fs::create_dir_all(idl_ts_out.as_ref().unwrap())?; let solana_version = match solana_version.is_some() { true => solana_version, @@ -940,6 +949,7 @@ fn verify( build( cfg_override, None, + None, true, None, match solana_version.is_some() { @@ -1457,6 +1467,7 @@ fn test( build( cfg_override, None, + None, false, None, None, @@ -2364,6 +2375,7 @@ fn publish( build( cfg_override, None, + None, true, Some(program_name), cfg.solana_version.clone(), @@ -2458,6 +2470,7 @@ fn localnet( build( cfg_override, None, + None, false, None, None, diff --git a/cli/src/template.rs b/cli/src/template.rs index 9db4205f1..895292298 100644 --- a/cli/src/template.rs +++ b/cli/src/template.rs @@ -30,15 +30,9 @@ token = "{}" pub fn idl_ts(idl: &Idl) -> Result { let mut idl = idl.clone(); - idl.accounts = idl - .accounts - .into_iter() - .map(|acc| { - let mut acc = acc; - acc.name = acc.name.to_mixed_case(); - acc - }) - .collect(); + for acc in idl.accounts.iter_mut() { + acc.name = acc.name.to_mixed_case(); + } let idl_json = serde_json::to_string_pretty(&idl)?; Ok(format!( r#"export type {} = {};