Browse Source

cli: Add `idl-ts` option to build command (#940)

Kirill Fomichev 3 years ago
parent
commit
ac3fbe8d38
3 changed files with 21 additions and 13 deletions
  1. 1 0
      CHANGELOG.md
  2. 17 4
      cli/src/lib.rs
  3. 3 9
      cli/src/template.rs

+ 1 - 0
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
 

+ 17 - 4
cli/src/lib.rs

@@ -67,6 +67,9 @@ pub enum Command {
         /// Output directory for the IDL.
         #[clap(short, long)]
         idl: Option<String>,
+        /// Output directory for the TypeScript IDL.
+        #[clap(short = 't', long)]
+        idl_ts: Option<String>,
         /// 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<String>,
+    idl_ts: Option<String>,
     verifiable: bool,
     program_name: Option<String>,
     solana_version: Option<String>,
@@ -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,

+ 3 - 9
cli/src/template.rs

@@ -30,15 +30,9 @@ token = "{}"
 
 pub fn idl_ts(idl: &Idl) -> Result<String> {
     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 {} = {};