@@ -20,6 +20,7 @@ import { DEFAULT_SDK_VERSION, PREFERRED_PORT } from "../config.js";
2020import {
2121 AVAILABLE_SERVICES ,
2222 deployApplication ,
23+ host ,
2324 removeApplication ,
2425 type RollupsDeployment ,
2526 startEnvironment ,
@@ -38,32 +39,17 @@ const commaSeparatedList = (value: string) => value.split(",");
3839
3940const shell = async ( options : {
4041 build ?: CommandUnknownOpts ;
42+ deployment ?: RollupsDeployment ;
4143 epochLength : number ;
4244 log ?: CommandUnknownOpts ;
4345 projectName : string ;
4446 prt ?: boolean ;
47+ salt : number ;
4548} ) => {
4649 const { build, epochLength, log, projectName, prt } = options ;
4750
48- // keep track of last deployment
49- let lastDeployment : RollupsDeployment | undefined ;
50- let salt = 0 ;
51-
52- // deploy for the first time
53- const hash = getMachineHash ( ) ;
54- if ( hash ) {
55- lastDeployment = await deploy ( {
56- epochLength,
57- hash,
58- projectName,
59- prt,
60- salt : numberToHex ( salt ++ , { size : 32 } ) ,
61- } ) ;
62- } else {
63- console . warn (
64- chalk . yellow ( "machine snapshot not found, waiting for build" ) ,
65- ) ;
66- }
51+ let lastDeployment = options . deployment ;
52+ let salt = options . salt ;
6753
6854 while ( true ) {
6955 try {
@@ -313,11 +299,15 @@ export const createRunCommand = () => {
313299 // configure optional anvil fork
314300 const forkConfig = await configureFork ( options ) ;
315301
302+ // if TTY is not attached, run on foreground (not detached)
303+ const detach = process . stdin . isTTY ;
304+
316305 // run compose environment (detached)
317- const { address , config } = await startEnvironment ( {
306+ const { cmd , config } = await startEnvironment ( {
318307 blockTime,
319308 cpus,
320309 defaultBlock,
310+ detach,
321311 dryRun,
322312 forkConfig,
323313 memory,
@@ -328,6 +318,9 @@ export const createRunCommand = () => {
328318 verbose,
329319 } ) ;
330320
321+ // host address
322+ const address = `${ host } :${ port } ` ;
323+
331324 if ( dryRun && config ) {
332325 // just show the docker compose configuration and quit
333326 process . stdout . write ( config ) ;
@@ -346,6 +339,27 @@ export const createRunCommand = () => {
346339 services,
347340 } ) ;
348341
342+ // deploy the application
343+ let deployment : RollupsDeployment | undefined ;
344+ let salt = 0 ;
345+ const prt = ! authority ;
346+ const hash = getMachineHash ( ) ;
347+ if ( hash ) {
348+ deployment = await deploy ( {
349+ epochLength,
350+ hash,
351+ projectName,
352+ prt,
353+ salt : numberToHex ( salt ++ , { size : 32 } ) ,
354+ } ) ;
355+ } else {
356+ console . warn (
357+ chalk . yellow (
358+ "machine snapshot not found, waiting for build" ,
359+ ) ,
360+ ) ;
361+ }
362+
349363 const shutdown = async ( ) => {
350364 progress . start ( `${ chalk . cyan ( projectName ) } stopping...` ) ;
351365 try {
@@ -359,23 +373,41 @@ export const createRunCommand = () => {
359373 process . exit ( 0 ) ;
360374 } ;
361375
362- // inhibit SIGINT and SIGTERM, will be handled gracefully by the shell
363- process . on ( "SIGINT" , ( ) => { } ) ;
364- process . on ( "SIGTERM" , ( ) => { } ) ;
376+ if ( detach ) {
377+ // inhibit SIGINT and SIGTERM, will be handled gracefully by the shell
378+ process . on ( "SIGINT" , ( ) => { } ) ;
379+ process . on ( "SIGTERM" , ( ) => { } ) ;
365380
366- const log = program . parent ?. commands . find (
367- ( c ) => c . name ( ) === "logs" ,
368- ) ;
369- const build = program . parent ?. commands . find (
370- ( c ) => c . name ( ) === "build" ,
371- ) ;
372- await shell ( {
373- build,
374- epochLength,
375- log,
376- projectName,
377- prt : ! authority ,
378- } ) ;
379- await shutdown ( ) ;
381+ const log = program . parent ?. commands . find (
382+ ( c ) => c . name ( ) === "logs" ,
383+ ) ;
384+ const build = program . parent ?. commands . find (
385+ ( c ) => c . name ( ) === "build" ,
386+ ) ;
387+ await shell ( {
388+ build,
389+ deployment,
390+ epochLength,
391+ log,
392+ projectName,
393+ prt,
394+ salt,
395+ } ) ;
396+ await shutdown ( ) ;
397+ } else {
398+ process . on ( "SIGINT" , shutdown ) ;
399+ process . on ( "SIGTERM" , shutdown ) ;
400+ try {
401+ await cmd ;
402+ } catch ( error : unknown ) {
403+ if ( error instanceof ExecaError ) {
404+ // just continue gracefully
405+ if ( error . exitCode === 130 ) {
406+ return ;
407+ }
408+ throw error ;
409+ }
410+ }
411+ }
380412 } ) ;
381413} ;
0 commit comments