Skip to content

Commit 7f2ef23

Browse files
author
Standaa
authored
Feat/migrations typescript support (#132)
1 parent d0396ea commit 7f2ef23

File tree

13 files changed

+167
-12
lines changed

13 files changed

+167
-12
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ jobs:
5656
- pushd examples/misc && anchor test && popd
5757
- pushd examples/events && anchor test && popd
5858
- pushd examples/cashiers-check && anchor test && popd
59+
- pushd examples/typescript && yarn && anchor test && popd
5960
- <<: *examples
6061
name: Runs the examples 2
6162
script:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ incremented for features.
1717
* lang: Allow overriding the `#[state]` account's size ([#121](https://github.com/project-serum/anchor/pull/121)).
1818
* lang, client, ts: Add event emission and subscriptions ([#89](https://github.com/project-serum/anchor/pull/89)).
1919
* lang/account: Allow namespacing account discriminators ([#128](https://github.com/project-serum/anchor/pull/128)).
20+
* cli: TypeScript migrations ([#132](https://github.com/project-serum/anchor/pull/132)).
2021

2122
## Breaking Changes
2223

cli/src/main.rs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,22 +252,26 @@ fn init(name: String, typescript: bool) -> Result<()> {
252252

253253
// Build the test suite.
254254
fs::create_dir("tests")?;
255+
// Build the migrations directory.
256+
fs::create_dir("migrations")?;
257+
255258
if typescript {
256259
// Build typescript config
257260
let mut ts_config = File::create("tsconfig.json")?;
258261
ts_config.write_all(template::ts_config().as_bytes())?;
259262

263+
let mut deploy = File::create("migrations/deploy.ts")?;
264+
deploy.write_all(&template::ts_deploy_script().as_bytes())?;
265+
260266
let mut mocha = File::create(&format!("tests/{}.spec.ts", name))?;
261267
mocha.write_all(template::ts_mocha(&name).as_bytes())?;
262268
} else {
263269
let mut mocha = File::create(&format!("tests/{}.js", name))?;
264270
mocha.write_all(template::mocha(&name).as_bytes())?;
265-
}
266271

267-
// Build the migrations directory.
268-
fs::create_dir("migrations")?;
269-
let mut deploy = File::create("migrations/deploy.js")?;
270-
deploy.write_all(&template::deploy_script().as_bytes())?;
272+
let mut deploy = File::create("migrations/deploy.js")?;
273+
deploy.write_all(&template::deploy_script().as_bytes())?;
274+
}
271275

272276
println!("{} initialized", name);
273277

@@ -1206,7 +1210,8 @@ fn launch(url: Option<String>, keypair: Option<String>, verifiable: bool) -> Res
12061210
}
12071211

12081212
// Run migration script.
1209-
if Path::new("migrations/deploy.js").exists() {
1213+
if Path::new("migrations/deploy.js").exists() || Path::new("migrations/deploy.ts").exists()
1214+
{
12101215
migrate(Some(url))?;
12111216
}
12121217

@@ -1386,22 +1391,46 @@ fn migrate(url: Option<String>) -> Result<()> {
13861391

13871392
let url = url.unwrap_or_else(|| cfg.cluster.url().to_string());
13881393
let cur_dir = std::env::current_dir()?;
1389-
let module_path = format!("{}/migrations/deploy.js", cur_dir.display());
1390-
let deploy_script_host_str = template::deploy_script_host(&url, &module_path);
1394+
let module_path = cur_dir.join("migrations/deploy.js");
1395+
1396+
let ts_config_exist = Path::new("tsconfig.json").exists();
1397+
let ts_deploy_file_exists = Path::new("migrations/deploy.ts").exists();
1398+
1399+
if ts_config_exist && ts_deploy_file_exists {
1400+
let ts_module_path = cur_dir.join("migrations/deploy.ts");
1401+
let exit = std::process::Command::new("tsc")
1402+
.arg(&ts_module_path)
1403+
.stdout(Stdio::inherit())
1404+
.stderr(Stdio::inherit())
1405+
.output()?;
1406+
if !exit.status.success() {
1407+
std::process::exit(exit.status.code().unwrap());
1408+
}
1409+
};
1410+
1411+
let deploy_script_host_str =
1412+
template::deploy_script_host(&url, &module_path.display().to_string());
13911413

13921414
if !Path::new(".anchor").exists() {
13931415
fs::create_dir(".anchor")?;
13941416
}
13951417
std::env::set_current_dir(".anchor")?;
13961418

13971419
std::fs::write("deploy.js", deploy_script_host_str)?;
1398-
if let Err(_e) = std::process::Command::new("node")
1420+
let exit = std::process::Command::new("node")
13991421
.arg("deploy.js")
14001422
.stdout(Stdio::inherit())
14011423
.stderr(Stdio::inherit())
1402-
.output()
1403-
{
1404-
std::process::exit(1);
1424+
.output()?;
1425+
1426+
if ts_config_exist && ts_deploy_file_exists {
1427+
std::fs::remove_file(&module_path)
1428+
.map_err(|_| anyhow!("Unable to remove file {}", module_path.display()))?;
1429+
}
1430+
1431+
if !exit.status.success() {
1432+
println!("Deploy failed.");
1433+
std::process::exit(exit.status.code().unwrap());
14051434
}
14061435

14071436
println!("Deploy complete.");

cli/src/template.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ module.exports = async function (provider) {
7979
}
8080
"#
8181
}
82+
83+
pub fn ts_deploy_script() -> &'static str {
84+
r#"
85+
// Migrations are an early feature. Currently, they're nothing more than this
86+
// single deploy script that's invoked from the CLI, injecting a provider
87+
// configured from the workspace's Anchor.toml.
88+
89+
const anchor = require("@project-serum/anchor");
90+
91+
module.exports = async function (provider) {
92+
// Configure client to use the provider.
93+
anchor.setProvider(provider);
94+
95+
// Add your deploy script here.
96+
}
97+
"#
98+
}
99+
82100
pub fn xargo_toml() -> &'static str {
83101
r#"[target.bpfel-unknown-unknown.dependencies.std]
84102
features = []"#

examples/typescript/Anchor.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cluster = "localnet"
2+
wallet = "~/.config/solana/id.json"

examples/typescript/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = [
3+
"programs/*"
4+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Migrations are an early feature. Currently, they're nothing more than this
2+
// single deploy script that's invoked from the CLI, injecting a provider
3+
// configured from the workspace's Anchor.toml.
4+
5+
const anchor = require("@project-serum/anchor");
6+
7+
module.exports = async function (provider) {
8+
// Configure client to use the provider.
9+
anchor.setProvider(provider);
10+
11+
// Add your deploy script here.
12+
async function deployAsync(exampleString: string): Promise<void> {
13+
return new Promise((resolve) => {
14+
setTimeout(() => {
15+
console.log(exampleString);
16+
resolve();
17+
}, 2000);
18+
});
19+
}
20+
21+
await deployAsync("Typescript migration example complete.");
22+
}

examples/typescript/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "typescript",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "tests"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"@types/node": "^14.14.37"
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "typescript"
3+
version = "0.1.0"
4+
description = "Created with Anchor"
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib", "lib"]
9+
name = "typescript"
10+
11+
[features]
12+
no-entrypoint = []
13+
no-idl = []
14+
cpi = ["no-entrypoint"]
15+
default = []
16+
17+
[dependencies]
18+
anchor-lang = { path = "../../../../lang" }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.bpfel-unknown-unknown.dependencies.std]
2+
features = []

0 commit comments

Comments
 (0)