-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathreadCypressConfigUtil.js
More file actions
160 lines (142 loc) · 7.16 KB
/
readCypressConfigUtil.js
File metadata and controls
160 lines (142 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
const path = require("path");
const fs = require("fs");
const cp = require('child_process');
const config = require('./config');
const constants = require("./constants");
const utils = require("./utils");
const logger = require('./logger').winstonLogger;
exports.detectLanguage = (cypress_config_filename) => {
const extension = cypress_config_filename.split('.').pop()
return constants.CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS.includes(extension) ? extension : 'js'
}
function generateTscCommandAndTempTsConfig(bsConfig, bstack_node_modules_path, complied_js_dir, cypress_config_filepath) {
const working_dir = path.dirname(cypress_config_filepath);
const typescript_path = path.join(bstack_node_modules_path, 'typescript', 'bin', 'tsc');
const tsc_alias_path = path.join(bstack_node_modules_path, 'tsc-alias', 'dist', 'bin', 'index.js');
const tsConfigFilePath = path.resolve(bsConfig.run_settings.ts_config_file_path);
// Prepare base temp tsconfig
const tempTsConfig = {
extends: tsConfigFilePath, // Use a base tsconfig if available
compilerOptions: {
"outDir": `${path.basename(complied_js_dir)}`, // Add ./ prefix for consistency
"listEmittedFiles": true,
},
include: [cypress_config_filepath]
};
// Write the temporary tsconfig
const tempTsConfigPath = path.join(working_dir, 'tsconfig.singlefile.tmp.json');
fs.writeFileSync(tempTsConfigPath, JSON.stringify(tempTsConfig, null, 2));
logger.info(`Temporary tsconfig created at: ${tempTsConfigPath}`);
// Platform-specific command generation
const isWindows = /^win/.test(process.platform);
if (isWindows) {
// Windows: Use && to chain commands, no space after SET
const setNodePath = isWindows
? `set NODE_PATH=${bstack_node_modules_path}`
: `NODE_PATH="${bstack_node_modules_path}"`;
const tscCommand = `${setNodePath} && node "${typescript_path}" --project "${tempTsConfigPath}" && ${setNodePath} && node "${tsc_alias_path}" --project "${tempTsConfigPath}" --verbose`;
logger.info(`TypeScript compilation command: ${tscCommand}`);
return { tscCommand, tempTsConfigPath };
} else {
// Unix/Linux/macOS: Use ; to separate commands or && to chain
const nodePathPrefix = `NODE_PATH=${bsConfig.run_settings.bstack_node_modules_path}`;
const tscCommand = `${nodePathPrefix} node "${typescript_path}" --project "${tempTsConfigPath}" && ${nodePathPrefix} node "${tsc_alias_path}" --project "${tempTsConfigPath}" --verbose`;
logger.info(`TypeScript compilation command: ${tscCommand}`);
return { tscCommand, tempTsConfigPath };
}
}
exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_modules_path) => {
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
const working_dir = path.dirname(cypress_config_filepath);
const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName)
if (fs.existsSync(complied_js_dir)) {
fs.rmdirSync(complied_js_dir, { recursive: true })
}
fs.mkdirSync(complied_js_dir, { recursive: true })
const { tscCommand, tempTsConfigPath } = generateTscCommandAndTempTsConfig(bsConfig, bstack_node_modules_path, complied_js_dir, cypress_config_filepath);
let tsc_output
try {
logger.debug(`Running: ${tscCommand}`)
tsc_output = cp.execSync(tscCommand, { cwd: working_dir })
cp.execSync(tscCommand, { cwd: working_dir })
} catch (err) {
// error while compiling ts files
logger.debug(err.message);
logger.debug(err.output.toString());
tsc_output = err.output // if there is an error, tsc adds output of complilation to err.output key
} finally {
logger.debug(`Saved compiled js output at: ${complied_js_dir}`);
logger.debug(`Finding compiled cypress config file in: ${complied_js_dir}`);
// Clean up the temporary tsconfig file
if (fs.existsSync(tempTsConfigPath)) {
fs.unlinkSync(tempTsConfigPath);
logger.info(`Temporary tsconfig file removed: ${tempTsConfigPath}`);
}
logger.info(tsc_output.toString());
const lines = tsc_output.toString().split('\n');
let foundLine = null;
for (let i = 0; i < lines.length; i++) {
if (lines[i].indexOf(`${path.parse(cypress_config_filename).name}.js`) > -1) {
foundLine = lines[i]
break;
}
}
if (foundLine === null) {
logger.error(`No compiled cypress config found. There might some error running ${tscCommand} command`)
return null
} else {
const compiled_cypress_config_filepath = foundLine.split('TSFILE: ').pop()
logger.debug(`Found compiled cypress config file: ${compiled_cypress_config_filepath}`);
return compiled_cypress_config_filepath
}
}
}
exports.loadJsFile = (cypress_config_filepath, bstack_node_modules_path) => {
const require_module_helper_path = path.join(__dirname, 'requireModule.js')
let load_command = `NODE_PATH="${bstack_node_modules_path}" node "${require_module_helper_path}" "${cypress_config_filepath}"`
if (/^win/.test(process.platform)) {
load_command = `set NODE_PATH=${bstack_node_modules_path}&& node "${require_module_helper_path}" "${cypress_config_filepath}"`
}
logger.debug(`Running: ${load_command}`)
cp.execSync(load_command)
const cypress_config = JSON.parse(fs.readFileSync(config.configJsonFileName).toString())
if (fs.existsSync(config.configJsonFileName)) {
fs.unlinkSync(config.configJsonFileName)
}
return cypress_config
}
exports.readCypressConfigFile = (bsConfig) => {
const cypress_config_filepath = path.resolve(bsConfig.run_settings.cypressConfigFilePath)
try {
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
const bstack_node_modules_path = path.join(path.resolve(config.packageDirName), 'node_modules')
const conf_lang = this.detectLanguage(cypress_config_filename)
logger.debug(`cypress config path: ${cypress_config_filepath}`);
if (conf_lang == 'js' || conf_lang == 'cjs') {
return this.loadJsFile(cypress_config_filepath, bstack_node_modules_path)
} else if (conf_lang === 'ts') {
const compiled_cypress_config_filepath = this.convertTsConfig(bsConfig, cypress_config_filepath, bstack_node_modules_path)
return this.loadJsFile(compiled_cypress_config_filepath, bstack_node_modules_path)
}
} catch (error) {
const errorMessage = `Error while reading cypress config: ${error.message}`
const errorCode = 'cypress_config_file_read_failed'
logger.error(errorMessage)
utils.sendUsageReport(
bsConfig,
null,
errorMessage,
constants.messageTypes.WARNING,
errorCode,
null,
null
)
} finally {
const working_dir = path.dirname(cypress_config_filepath)
const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName)
if (fs.existsSync(complied_js_dir)) {
fs.rmdirSync(complied_js_dir, { recursive: true })
}
}
}