@@ -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." ) ;
0 commit comments