88 */
99
1010const { spawn, spawnSync } = require ( 'child_process' ) ;
11+ const path = require ( 'path' ) ;
12+ const fs = require ( 'fs' ) ;
13+
14+ function getPythonExecutable ( ) {
15+ const isWin = process . platform === 'win32' ;
16+ const pyRelativePath = isWin ? [ 'Scripts' , 'python.exe' ] : [ 'bin' , 'python' ] ;
17+ const fallback = isWin ? 'python' : 'python3' ;
18+
19+ if ( process . env . VIRTUAL_ENV ) {
20+ return path . join ( process . env . VIRTUAL_ENV , ...pyRelativePath ) ;
21+ }
22+ const rootDir = path . join ( __dirname , '..' , '..' ) ;
23+ const localVenv = path . join ( rootDir , '.venv' , ...pyRelativePath ) ;
24+ if ( fs . existsSync ( localVenv ) ) {
25+ return localVenv ;
26+ }
27+ const localVenvAlt = path . join ( rootDir , 'venv' , ...pyRelativePath ) ;
28+ if ( fs . existsSync ( localVenvAlt ) ) {
29+ return localVenvAlt ;
30+ }
31+ return fallback ;
32+ }
1133
1234async function runDemo ( page , name , opts = { } ) {
1335 const saveto = opts . env || `${ name } _${ Math . floor ( Math . random ( ) * 1e6 ) } ` ;
@@ -26,19 +48,27 @@ async function runDemo(page, name, opts = {}) {
2648 spawnArgs . push ( '-seed' , String ( opts . seed ) ) ;
2749 }
2850 if ( opts . args && opts . args . length > 0 ) {
29- spawnArgs . push ( '-arg ' , ...opts . args . map ( String ) ) ;
51+ spawnArgs . push ( '-args ' , ...opts . args . map ( String ) ) ;
3052 }
3153
54+ const pythonBin = getPythonExecutable ( ) ;
55+
3256 if ( opts . asyncrun ) {
33- const child = spawn ( 'python' , spawnArgs , {
57+ const child = spawn ( pythonBin , spawnArgs , {
3458 stdio : 'ignore' ,
3559 detached : true ,
3660 } ) ;
3761 child . unref ( ) ;
3862 } else {
39- spawnSync ( 'python' , spawnArgs , {
40- stdio : 'ignore' ,
63+ const result = spawnSync ( pythonBin , spawnArgs , {
64+ stdio : 'pipe' ,
65+ encoding : 'utf-8' ,
4166 } ) ;
67+ if ( result . status !== 0 ) {
68+ throw new Error (
69+ `Failed to run demo '${ name } '. Exit code: ${ result . status } .\nStderr: ${ result . stderr } \nStdout: ${ result . stdout } `
70+ ) ;
71+ }
4272 }
4373
4474 if ( opts . open === undefined || opts . open ) {
@@ -60,12 +90,12 @@ async function closeEnvs(page) {
6090async function expandAllEnvGroups ( page ) {
6191 const closedGroups = page . locator ( '.rc-tree-select-tree-switcher_close' ) ;
6292 let count = await closedGroups . count ( ) ;
63- while ( count > 0 ) {
64- for ( let i = 0 ; i < count ; i ++ ) {
65- await closedGroups . nth ( i ) . click ( { force : true } ) ;
66- await page . waitForTimeout ( 150 ) ;
67- }
93+ let attempts = 0 ;
94+ while ( count > 0 && attempts < 50 ) {
95+ await closedGroups . first ( ) . click ( { force : true } ) ;
96+ await page . waitForTimeout ( 150 ) ;
6897 count = await closedGroups . count ( ) ;
98+ attempts ++ ;
6999 }
70100}
71101
0 commit comments