From 3b304588c841cd90d4488ceb4ff42e99ed31426e Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Sun, 4 Jul 2021 19:57:10 -0700 Subject: [PATCH] cli: Use ts-node to execute typescript migrations (#477) --- .travis.yml | 1 + cli/src/main.rs | 52 ++++++++++++++++++++------------------------- cli/src/template.rs | 30 +++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/.travis.yml b/.travis.yml index dcd7da7e..076e6c3d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,7 @@ _examples: &examples - npm install -g mocha - npm install -g ts-mocha - npm install -g typescript + - npm install -g ts-node - npm install -g buffer - cd ts && yarn && yarn build && npm link && cd ../ - npm install -g @project-serum/serum diff --git a/cli/src/main.rs b/cli/src/main.rs index b3fc8c8d..5ffafd8e 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1468,42 +1468,36 @@ fn migrate(cfg_override: &ConfigOverride) -> Result<()> { let url = cfg.provider.cluster.url().to_string(); let cur_dir = std::env::current_dir()?; - let module_path = cur_dir.join("migrations/deploy.js"); - let ts_config_exist = Path::new("tsconfig.json").exists(); - let ts_deploy_file_exists = Path::new("migrations/deploy.ts").exists(); - - if ts_config_exist && ts_deploy_file_exists { - let ts_module_path = cur_dir.join("migrations/deploy.ts"); - let exit = std::process::Command::new("tsc") - .arg(&ts_module_path) - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output()?; - if !exit.status.success() { - std::process::exit(exit.status.code().unwrap()); - } - }; - - let deploy_script_host_str = - template::deploy_script_host(&url, &module_path.display().to_string()); + let use_ts = + Path::new("tsconfig.json").exists() && Path::new("migrations/deploy.ts").exists(); if !Path::new(".anchor").exists() { fs::create_dir(".anchor")?; } std::env::set_current_dir(".anchor")?; - std::fs::write("deploy.js", deploy_script_host_str)?; - let exit = std::process::Command::new("node") - .arg("deploy.js") - .stdout(Stdio::inherit()) - .stderr(Stdio::inherit()) - .output()?; - - if ts_config_exist && ts_deploy_file_exists { - std::fs::remove_file(&module_path) - .map_err(|_| anyhow!("Unable to remove file {}", module_path.display()))?; - } + let exit = if use_ts { + let module_path = cur_dir.join("migrations/deploy.ts"); + let deploy_script_host_str = + template::deploy_ts_script_host(&url, &module_path.display().to_string()); + std::fs::write("deploy.ts", deploy_script_host_str)?; + std::process::Command::new("ts-node") + .arg("deploy.ts") + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output()? + } else { + let module_path = cur_dir.join("migrations/deploy.js"); + let deploy_script_host_str = + template::deploy_js_script_host(&url, &module_path.display().to_string()); + std::fs::write("deploy.js", deploy_script_host_str)?; + std::process::Command::new("node") + .arg("deploy.js") + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .output()? + }; if !exit.status.success() { println!("Deploy failed."); diff --git a/cli/src/template.rs b/cli/src/template.rs index dcc4b090..ad391c04 100644 --- a/cli/src/template.rs +++ b/cli/src/template.rs @@ -38,7 +38,7 @@ anchor-lang = "{2}" ) } -pub fn deploy_script_host(cluster_url: &str, script_path: &str) -> String { +pub fn deploy_js_script_host(cluster_url: &str, script_path: &str) -> String { format!( r#" const anchor = require('@project-serum/anchor'); @@ -66,6 +66,34 @@ main(); ) } +pub fn deploy_ts_script_host(cluster_url: &str, script_path: &str) -> String { + format!( + r#" +import * as anchor from '@project-serum/anchor'; + +// Deploy script defined by the user. +const userScript = require("{0}"); + +async function main() {{ + const url = "{1}"; + const preflightCommitment = 'recent'; + const connection = new anchor.web3.Connection(url, preflightCommitment); + const wallet = anchor.Wallet.local(); + + const provider = new anchor.Provider(connection, wallet, {{ + preflightCommitment, + commitment: 'recent', + }}); + + // Run the user's deploy script. + userScript(provider); +}} +main(); +"#, + script_path, cluster_url, + ) +} + pub fn deploy_script() -> &'static str { r#" // Migrations are an early feature. Currently, they're nothing more than this