@@ -12,7 +12,11 @@ import { spawn } from "node:child_process";
12
12
export const dev = new Command ( )
13
13
. name ( "dev" )
14
14
. description ( "Run locally your ActorCore project." )
15
- . addArgument ( new Argument ( "<path>" , "Location of the app.ts file" ) )
15
+ . addArgument (
16
+ new Argument ( "[path]" , "Location of the app.ts file" ) . default (
17
+ "actors/app.ts" ,
18
+ ) ,
19
+ )
16
20
. addOption (
17
21
new Option ( "-r, --root [path]" , "Location of the project" ) . default ( "./" ) ,
18
22
)
@@ -29,77 +33,102 @@ export const dev = new Command()
29
33
. option ( "--no-open" , "Do not open the browser with ActorCore Studio" )
30
34
. action ( action ) ;
31
35
32
- export async function action ( appPath : string , opts : {
33
- root : string ;
34
- port ?: string ;
35
- open : boolean ;
36
- } ) {
36
+ export async function action (
37
+ appPath : string ,
38
+ opts : {
39
+ root : string ;
40
+ port ?: string ;
41
+ open : boolean ;
42
+ } ,
43
+ ) {
37
44
const cwd = path . join ( process . cwd ( ) , opts . root ) ;
38
45
39
- await workflow ( "Run locally your ActorCore project" , async function * ( ctx ) {
40
- if ( opts . open ) {
41
- open (
42
- process . env . _ACTOR_CORE_CLI_DEV
43
- ? "http://localhost:43708"
44
- : "http://studio.rivet.gg" ,
45
- ) ;
46
- }
46
+ await workflow (
47
+ `Run locally your ActorCore project (${ appPath } )` ,
48
+ async function * ( ctx ) {
49
+ if ( opts . open ) {
50
+ open (
51
+ process . env . _ACTOR_CORE_CLI_DEV
52
+ ? "http://localhost:43708"
53
+ : "http://studio.rivet.gg" ,
54
+ ) ;
55
+ }
47
56
48
- const watcher = chokidar . watch ( cwd , {
49
- awaitWriteFinish : true ,
50
- ignoreInitial : true ,
51
- ignored : ( path ) => path . includes ( "node_modules" ) ,
52
- } ) ;
57
+ const watcher = chokidar . watch ( cwd , {
58
+ awaitWriteFinish : true ,
59
+ ignoreInitial : true ,
60
+ ignored : ( path ) => path . includes ( "node_modules" ) ,
61
+ } ) ;
53
62
54
- function createServer ( ) {
55
- return spawn (
56
- process . execPath ,
57
- [
58
- path . join (
59
- path . dirname ( require . resolve ( "@actor-core/cli" ) ) ,
60
- "server-entry.js" ,
61
- ) ,
62
- ] ,
63
- { env : { ...process . env , PORT : opts . port , APP_PATH : appPath } , cwd } ,
64
- ) ;
65
- }
63
+ function createServer ( ) {
64
+ return spawn (
65
+ process . execPath ,
66
+ [
67
+ path . join (
68
+ path . dirname ( require . resolve ( "@actor-core/cli" ) ) ,
69
+ "server-entry.js" ,
70
+ ) ,
71
+ ] ,
72
+ {
73
+ env : { ...process . env , PORT : opts . port , APP_PATH : appPath } ,
74
+ cwd,
75
+ stdio : "overlapped" ,
76
+ } ,
77
+ ) ;
78
+ }
66
79
67
- let server : ReturnType < typeof spawn > | undefined = undefined ;
68
- let lock : ReturnType < typeof withResolvers > = withResolvers ( ) ;
80
+ let server : ReturnType < typeof spawn > | undefined = undefined ;
81
+ let lock : ReturnType < typeof withResolvers > = withResolvers ( ) ;
69
82
70
- function createLock ( ) {
71
- if ( lock ) {
72
- lock . resolve ( undefined ) ;
83
+ function createLock ( ) {
84
+ if ( lock ) {
85
+ lock . resolve ( undefined ) ;
86
+ }
87
+ lock = withResolvers ( ) ;
73
88
}
74
- lock = withResolvers ( ) ;
75
- }
76
89
77
- watcher . on ( "all" , async ( _ , path ) => {
78
- if ( path . includes ( "node_modules" ) || path . includes ( "/." ) ) return ;
90
+ watcher . on ( "all" , async ( _ , path ) => {
91
+ if ( path . includes ( "node_modules" ) || path . includes ( "/." ) ) return ;
79
92
80
- server ?. kill ( ) ;
81
- } ) ;
93
+ if ( server ?. exitCode === 1 ) {
94
+ // Server exited with error
95
+ console . log ( "Server exited with error" ) ;
96
+ lock . resolve ( undefined ) ;
97
+ return ;
98
+ } else {
99
+ server ?. kill ( "SIGQUIT" ) ;
100
+ }
101
+ } ) ;
82
102
83
- while ( true ) {
84
- yield * validateConfigTask ( ctx , cwd , appPath ) ;
85
- server = createServer ( ) ;
86
103
createLock ( ) ;
87
104
88
- server ?. addListener ( "exit" , ( ) => {
89
- lock . resolve ( undefined ) ;
90
- } ) ;
105
+ while ( true ) {
106
+ yield * validateConfigTask ( ctx , cwd , appPath ) ;
107
+ yield * ctx . task (
108
+ "Server started. Watching for changes" ,
109
+ async function * ( ctx ) {
110
+ server = createServer ( ) ;
111
+ if ( server ?. stdout ) {
112
+ yield ctx . attach ( server . stdout , server . stderr ) ;
113
+ }
114
+ createLock ( ) ;
91
115
92
- server ?. addListener ( "close" , ( ) => {
93
- lock . resolve ( undefined ) ;
94
- } ) ;
116
+ server ?. addListener ( "exit" , ( code ) => {
117
+ if ( code === 1 ) {
118
+ ctx . changeLabel (
119
+ "Server exited with error. It will be restarted after next file change..." ,
120
+ ) ;
121
+ // Server exited with error
122
+ return ;
123
+ }
124
+ lock . resolve ( undefined ) ;
125
+ } ) ;
95
126
96
- yield * ctx . task (
97
- "Watching for changes..." ,
98
- async ( ) => {
99
- await lock . promise ;
100
- } ,
101
- { success : < Text dimColor > (Changes detected, restarting!)</ Text > } ,
102
- ) ;
103
- }
104
- } ) . render ( ) ;
127
+ await lock . promise ;
128
+ } ,
129
+ { success : < Text dimColor > (Changes detected, restarting!)</ Text > } ,
130
+ ) ;
131
+ }
132
+ } ,
133
+ ) . render ( ) ;
105
134
}
0 commit comments