@@ -7,31 +7,83 @@ const config = require('./config');
7
7
const constants = require ( "./constants" ) ;
8
8
const logger = require ( './logger' ) . winstonLogger ;
9
9
10
- const detectLanguage = ( cypress_config_filename ) => {
10
+ exports . detectLanguage = ( cypress_config_filename ) => {
11
11
const extension = cypress_config_filename . split ( '.' ) . pop ( )
12
12
return constants . CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS . includes ( extension ) ? extension : 'js'
13
13
}
14
14
15
+ exports . convertTsConfig = ( bsConfig , cypress_config_filepath , bstack_node_modules_path ) => {
16
+ const cypress_config_filename = bsConfig . run_settings . cypress_config_filename
17
+ const working_dir = path . dirname ( cypress_config_filepath ) ;
18
+ const complied_js_dir = path . join ( working_dir , config . compiledConfigJsDirName )
19
+ execSync ( `rm -rf ${ config . compiledConfigJsDirName } ` , { cwd : working_dir } )
20
+ execSync ( `mkdir ${ config . compiledConfigJsDirName } ` , { cwd : working_dir } )
21
+
22
+ let tsc_command = `NODE_PATH=${ bstack_node_modules_path } ${ bstack_node_modules_path } /typescript/bin/tsc --outDir ${ complied_js_dir } --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false ${ cypress_config_filepath } `
23
+ let tsc_output
24
+
25
+ try {
26
+ logger . debug ( `Running: ${ tsc_command } ` )
27
+ tsc_output = execSync ( tsc_command , { cwd : working_dir } )
28
+ } catch ( err ) {
29
+ // error while compiling ts files
30
+ logger . debug ( err . message ) ;
31
+ logger . debug ( err . output . toString ( ) ) ;
32
+ tsc_output = err . output // if there is an error, tsc adds output of complilation to err.output key
33
+ } finally {
34
+ logger . debug ( `Saved compiled js output at: ${ complied_js_dir } ` ) ;
35
+ logger . debug ( `Finding compiled cypress config file in: ${ complied_js_dir } ` ) ;
36
+
37
+ const lines = tsc_output . toString ( ) . split ( '\n' ) ;
38
+ let foundLine = null ;
39
+ for ( let i = 0 ; i < lines . length ; i ++ ) {
40
+ if ( lines [ i ] . indexOf ( `${ path . parse ( cypress_config_filename ) . name } .js` ) > - 1 ) {
41
+ foundLine = lines [ i ]
42
+ break ;
43
+ }
44
+ }
45
+ if ( foundLine === null ) {
46
+ logger . error ( `No compiled cypress config found. There might some error running ${ tsc_command } command` )
47
+ return null
48
+ } else {
49
+ const compiled_cypress_config_filepath = foundLine . split ( 'TSFILE: ' ) . pop ( )
50
+ logger . debug ( `Found compiled cypress config file: ${ compiled_cypress_config_filepath } ` ) ;
51
+ return compiled_cypress_config_filepath
52
+ }
53
+ }
54
+ }
55
+
56
+ exports . loadJsFile = ( cypress_config_filepath , bstack_node_modules_path ) => {
57
+ const require_module_helper_path = `${ __dirname } /requireModule.js`
58
+ execSync ( `NODE_PATH=${ bstack_node_modules_path } node ${ require_module_helper_path } ${ cypress_config_filepath } ` )
59
+ const cypress_config = JSON . parse ( fs . readFileSync ( config . configJsonFileName ) . toString ( ) )
60
+ if ( fs . existsSync ( config . configJsonFileName ) ) {
61
+ fs . unlinkSync ( config . configJsonFileName )
62
+ }
63
+ return cypress_config
64
+ }
65
+
15
66
exports . readCypressConfigFile = ( bsConfig ) => {
67
+ const cypress_config_filepath = path . resolve ( bsConfig . run_settings . cypressConfigFilePath )
16
68
try {
17
- const cypress_config_filepath = path . resolve ( bsConfig . run_settings . cypressConfigFilePath )
18
69
const cypress_config_filename = bsConfig . run_settings . cypress_config_filename
19
70
const bstack_node_modules_path = `${ path . resolve ( config . packageDirName ) } /node_modules`
20
- const conf_lang = detectLanguage ( cypress_config_filename )
21
- const require_module_helper_path = `${ __dirname } /requireModule.js`
71
+ const conf_lang = this . detectLanguage ( cypress_config_filename )
22
72
23
73
logger . debug ( `cypress config path: ${ cypress_config_filepath } ` ) ;
24
74
25
75
if ( conf_lang == 'js' || conf_lang == 'cjs' ) {
26
- execSync ( `NODE_PATH=${ bstack_node_modules_path } node ${ require_module_helper_path } ${ cypress_config_filepath } ` )
27
- const cypress_config = JSON . parse ( fs . readFileSync ( config . configJsonFileName ) . toString ( ) )
28
- if ( fs . existsSync ( config . configJsonFileName ) ) {
29
- fs . unlinkSync ( config . configJsonFileName )
30
- }
31
- return cypress_config
76
+ return this . loadJsFile ( cypress_config_filepath , bstack_node_modules_path )
77
+ } else if ( conf_lang === 'ts' ) {
78
+ const compiled_cypress_config_filepath = this . convertTsConfig ( bsConfig , cypress_config_filepath , bstack_node_modules_path )
79
+ return this . loadJsFile ( compiled_cypress_config_filepath , bstack_node_modules_path )
32
80
}
33
81
} catch ( error ) {
34
82
logger . error ( `Error while reading cypress config: ${ error . message } ` )
83
+
35
84
// TODO: Add instrumention if error occurred
85
+ } finally {
86
+ const working_dir = path . dirname ( cypress_config_filepath ) ;
87
+ execSync ( `rm -rf ${ config . compiledConfigJsDirName } ` , { cwd : working_dir } )
36
88
}
37
89
}
0 commit comments