|
| 1 | +"use strict"; |
| 2 | +const path = require("path"); |
| 3 | +const fs = require("fs"); |
| 4 | +const cp = require('child_process'); |
| 5 | + |
| 6 | +const config = require('./config'); |
| 7 | +const constants = require("./constants"); |
| 8 | +const utils = require("./utils"); |
| 9 | +const logger = require('./logger').winstonLogger; |
| 10 | + |
| 11 | +exports.detectLanguage = (cypress_config_filename) => { |
| 12 | + const extension = cypress_config_filename.split('.').pop() |
| 13 | + return constants.CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS.includes(extension) ? extension : 'js' |
| 14 | +} |
| 15 | + |
| 16 | +exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_modules_path) => { |
| 17 | + const cypress_config_filename = bsConfig.run_settings.cypress_config_filename |
| 18 | + const working_dir = path.dirname(cypress_config_filepath); |
| 19 | + const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName) |
| 20 | + cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir }) |
| 21 | + cp.execSync(`mkdir ${config.compiledConfigJsDirName}`, { cwd: working_dir }) |
| 22 | + |
| 23 | + 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}` |
| 24 | + let tsc_output |
| 25 | + try { |
| 26 | + logger.debug(`Running: ${tsc_command}`) |
| 27 | + tsc_output = cp.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 | + cp.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 | + |
| 66 | +exports.readCypressConfigFile = (bsConfig) => { |
| 67 | + const cypress_config_filepath = path.resolve(bsConfig.run_settings.cypressConfigFilePath) |
| 68 | + try { |
| 69 | + const cypress_config_filename = bsConfig.run_settings.cypress_config_filename |
| 70 | + const bstack_node_modules_path = `${path.resolve(config.packageDirName)}/node_modules` |
| 71 | + const conf_lang = this.detectLanguage(cypress_config_filename) |
| 72 | + |
| 73 | + logger.debug(`cypress config path: ${cypress_config_filepath}`); |
| 74 | + |
| 75 | + if (conf_lang == 'js' || conf_lang == 'cjs') { |
| 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) |
| 80 | + } |
| 81 | + } catch (error) { |
| 82 | + const errorMessage = `Error while reading cypress config: ${error.message}` |
| 83 | + const errorCode = 'cypress_config_file_read_failed' |
| 84 | + logger.error(errorMessage) |
| 85 | + utils.sendUsageReport( |
| 86 | + bsConfig, |
| 87 | + null, |
| 88 | + errorMessage, |
| 89 | + constants.messageTypes.WARNING, |
| 90 | + errorCode, |
| 91 | + null, |
| 92 | + null |
| 93 | + ) |
| 94 | + } finally { |
| 95 | + const working_dir = path.dirname(cypress_config_filepath); |
| 96 | + cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir }) |
| 97 | + } |
| 98 | +} |
0 commit comments