cli: Use ts-node to execute typescript migrations (#477)
This commit is contained in:
parent
53295e1e0b
commit
3b304588c8
|
@ -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
|
||||
|
|
|
@ -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.");
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue