@@ -5,22 +5,18 @@ import path from "node:path";
55
66// Third party imports
77import back_schemas from "@geode/opengeodeweb-back/opengeodeweb_back_schemas.json" with { type : "json" } ;
8- import { getPort } from "get-port-please" ;
9- import pTimeout from "p-timeout" ;
108
119// Local imports
12- import { commandExistsSync , waitForReady } from "./scripts.js" ;
10+ import { commandExistsSync , getAvailablePort , waitForReady } from "./scripts.js" ;
1311import { microservicesMetadatasPath , projectMicroservices } from "./cleanup.js" ;
1412import { executablePath } from "./path.js" ;
1513
16- const DEFAULT_TIMEOUT_SECONDS = 120 ;
1714const MILLISECONDS_PER_SECOND = 1000 ;
15+ const DEFAULT_TIMEOUT_SECONDS = 30 ;
1816
19- function getAvailablePort ( ) {
20- return getPort ( {
21- host : "localhost" ,
22- random : true ,
23- } ) ;
17+ function resolveCommand ( execPath , execName ) {
18+ const command = commandExistsSync ( execName ) ? execName : executablePath ( execPath , execName ) ;
19+ return command ;
2420}
2521
2622async function runScript (
@@ -30,33 +26,31 @@ async function runScript(
3026 expectedResponse ,
3127 timeoutSeconds = DEFAULT_TIMEOUT_SECONDS ,
3228) {
33- let command = "" ;
34- if ( commandExistsSync ( execName ) ) {
35- command = execName ;
36- } else {
37- command = path . join ( executablePath ( execPath , execName ) ) ;
38- }
29+ const command = resolveCommand ( execPath , execName ) ;
3930 console . log ( "runScript" , command , args ) ;
4031
41- const child = child_process . spawn ( process . platform === "win32" ? command : `"${ command } "` , args , {
42- encoding : "utf8" ,
43- shell : true ,
32+ const child = child_process . spawn ( command , args , {
33+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
4434 } ) ;
45- child . stdout . on ( "data" , ( data ) => console . log ( `[${ execName } ] ${ data . toString ( ) } ` ) ) ;
46- child . stderr . on ( "data" , ( data ) => console . log ( `[${ execName } ] ${ data . toString ( ) } ` ) ) ;
4735
48- child . on ( "close" , ( code ) => console . log ( `[${ execName } ] exited with code ${ code } ` ) ) ;
49- child . on ( "kill" , ( ) => {
50- console . log ( `[${ execName } ] process killed` ) ;
51- } ) ;
5236 child . name = command . replace ( / ^ .* [ \\ / ] / u, "" ) ;
5337
38+ child . on ( "spawn" , ( ) => {
39+ console . log ( `[${ child . name } ] spawned, pid=${ child . pid } ` ) ;
40+ } ) ;
41+
42+ const controller = new AbortController ( ) ;
43+ const timer = setTimeout ( ( ) => controller . abort ( ) , timeoutSeconds * MILLISECONDS_PER_SECOND ) ;
44+ if ( typeof timer . unref === "function" ) {
45+ timer . unref ( ) ;
46+ }
47+
5448 try {
55- return await pTimeout ( waitForReady ( child , expectedResponse ) , {
56- milliseconds : timeoutSeconds * MILLISECONDS_PER_SECOND ,
57- message : `Timed out after ${ timeoutSeconds } seconds` ,
58- } ) ;
49+ const result = await waitForReady ( child , expectedResponse , controller . signal ) ;
50+ clearTimeout ( timer ) ;
51+ return result ;
5952 } catch ( error ) {
53+ clearTimeout ( timer ) ;
6054 child . kill ( ) ;
6155 throw error ;
6256 }
@@ -73,11 +67,16 @@ async function runBack(execName, execPath, args = {}) {
7367 }
7468 const port = await getAvailablePort ( ) ;
7569 const backArgs = [
76- `--port ${ port } ` ,
77- `--data_folder_path ${ projectFolderPath } ` ,
78- `--upload_folder_path ${ uploadFolderPath } ` ,
79- `--allowed_origin http://localhost:*` ,
80- `--timeout ${ 0 } ` ,
70+ "--port" ,
71+ String ( port ) ,
72+ "--data_folder_path" ,
73+ projectFolderPath ,
74+ "--upload_folder_path" ,
75+ uploadFolderPath ,
76+ "--allowed_origin" ,
77+ "http://localhost:*" ,
78+ "--timeout" ,
79+ "0" ,
8180 ] ;
8281 if ( process . env . NODE_ENV === "development" || ! process . env . NODE_ENV ) {
8382 backArgs . push ( "--debug" ) ;
@@ -94,9 +93,12 @@ async function runViewer(execName, execPath, args = {}) {
9493 }
9594 const port = await getAvailablePort ( ) ;
9695 const viewerArgs = [
97- `--port ${ port } ` ,
98- `--data_folder_path ${ projectFolderPath } ` ,
99- `--timeout ${ 0 } ` ,
96+ "--port" ,
97+ String ( port ) ,
98+ "--data_folder_path" ,
99+ projectFolderPath ,
100+ "--timeout" ,
101+ "0" ,
100102 ] ;
101103 console . log ( "runViewer" , execPath , execName , viewerArgs ) ;
102104 await runScript ( execPath , execName , viewerArgs , "Starting factory" ) ;
@@ -121,4 +123,4 @@ function addMicroserviceMetadatas(projectFolderPath, serviceObj) {
121123 ) ;
122124}
123125
124- export { addMicroserviceMetadatas , getAvailablePort , runBack , runViewer } ;
126+ export { addMicroserviceMetadatas , runBack , runViewer } ;
0 commit comments