@@ -18,48 +18,6 @@ export type CreateLlamaResult = {
18
18
appProcess : ChildProcess ;
19
19
} ;
20
20
21
- // eslint-disable-next-line max-params
22
- export async function checkAppHasStarted (
23
- frontend : boolean ,
24
- framework : TemplateFramework ,
25
- port : number ,
26
- externalPort : number ,
27
- timeout : number ,
28
- ) {
29
- if ( frontend ) {
30
- await Promise . all ( [
31
- waitPort ( {
32
- host : "localhost" ,
33
- port : port ,
34
- timeout,
35
- } ) ,
36
- waitPort ( {
37
- host : "localhost" ,
38
- port : externalPort ,
39
- timeout,
40
- } ) ,
41
- ] ) . catch ( ( err ) => {
42
- console . error ( err ) ;
43
- throw err ;
44
- } ) ;
45
- } else {
46
- let wPort : number ;
47
- if ( framework === "nextjs" ) {
48
- wPort = port ;
49
- } else {
50
- wPort = externalPort ;
51
- }
52
- await waitPort ( {
53
- host : "localhost" ,
54
- port : wPort ,
55
- timeout,
56
- } ) . catch ( ( err ) => {
57
- console . error ( err ) ;
58
- throw err ;
59
- } ) ;
60
- }
61
- }
62
-
63
21
// eslint-disable-next-line max-params
64
22
export async function runCreateLlama (
65
23
cwd : string ,
@@ -142,25 +100,10 @@ export async function runCreateLlama(
142
100
templateFramework ,
143
101
port ,
144
102
externalPort ,
145
- 1000 * 60 * 5 ,
146
103
) ;
147
104
} else {
148
- // wait create-llama to exit
149
- // we don't test install dependencies for now, so just set timeout for 10 seconds
150
- await new Promise ( ( resolve , reject ) => {
151
- const timeout = setTimeout ( ( ) => {
152
- reject ( new Error ( "create-llama timeout error" ) ) ;
153
- } , 1000 * 10 ) ;
154
- appProcess . on ( "exit" , ( code ) => {
155
- if ( code !== 0 && code !== null ) {
156
- clearTimeout ( timeout ) ;
157
- reject ( new Error ( "create-llama command was failed!" ) ) ;
158
- } else {
159
- clearTimeout ( timeout ) ;
160
- resolve ( undefined ) ;
161
- }
162
- } ) ;
163
- } ) ;
105
+ // wait 10 seconds for create-llama to exit
106
+ await waitForProcess ( appProcess , 1000 * 10 ) ;
164
107
}
165
108
166
109
return {
@@ -174,3 +117,53 @@ export async function createTestDir() {
174
117
await mkdir ( cwd , { recursive : true } ) ;
175
118
return cwd ;
176
119
}
120
+
121
+ // eslint-disable-next-line max-params
122
+ async function checkAppHasStarted (
123
+ frontend : boolean ,
124
+ framework : TemplateFramework ,
125
+ port : number ,
126
+ externalPort : number ,
127
+ ) {
128
+ const portsToWait = frontend
129
+ ? [ port , externalPort ]
130
+ : [ framework === "nextjs" ? port : externalPort ] ;
131
+ await waitPorts ( portsToWait ) ;
132
+ }
133
+
134
+ async function waitPorts ( ports : number [ ] ) : Promise < void > {
135
+ const waitForPort = async ( port : number ) : Promise < void > => {
136
+ await waitPort ( {
137
+ host : "localhost" ,
138
+ port : port ,
139
+ // wait max. 5 mins for start up of app
140
+ timeout : 1000 * 60 * 5 ,
141
+ } ) ;
142
+ } ;
143
+ try {
144
+ await Promise . all ( ports . map ( waitForPort ) ) ;
145
+ } catch ( err ) {
146
+ console . error ( err ) ;
147
+ throw err ;
148
+ }
149
+ }
150
+
151
+ async function waitForProcess (
152
+ process : ChildProcess ,
153
+ timeoutMs : number ,
154
+ ) : Promise < void > {
155
+ return new Promise ( ( resolve , reject ) => {
156
+ const timeout = setTimeout ( ( ) => {
157
+ reject ( new Error ( "Process timeout error" ) ) ;
158
+ } , timeoutMs ) ;
159
+
160
+ process . on ( "exit" , ( code ) => {
161
+ clearTimeout ( timeout ) ;
162
+ if ( code !== 0 && code !== null ) {
163
+ reject ( new Error ( "Process exited with non-zero code" ) ) ;
164
+ } else {
165
+ resolve ( ) ;
166
+ }
167
+ } ) ;
168
+ } ) ;
169
+ }
0 commit comments