Skip to content

Commit 89cc1eb

Browse files
TS support and fixes
1 parent 1099cbe commit 89cc1eb

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

bin/helpers/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ config.packageFileName = "bstackPackages.tar.gz";
2424
config.packageDirName = "tmpBstackPackages";
2525
config.retries = 5;
2626
config.networkErrorExitCode = 2;
27+
config.compiledConfigJsDirName = 'tmpBstackCompiledJs'
2728
config.configJsonFileName = 'tmpCypressConfig.json'
2829

2930
module.exports = config;

bin/helpers/readCypressConfigUtil.js

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,83 @@ const config = require('./config');
77
const constants = require("./constants");
88
const logger = require('./logger').winstonLogger;
99

10-
const detectLanguage = (cypress_config_filename) => {
10+
exports.detectLanguage = (cypress_config_filename) => {
1111
const extension = cypress_config_filename.split('.').pop()
1212
return constants.CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS.includes(extension) ? extension : 'js'
1313
}
1414

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+
1566
exports.readCypressConfigFile = (bsConfig) => {
67+
const cypress_config_filepath = path.resolve(bsConfig.run_settings.cypressConfigFilePath)
1668
try {
17-
const cypress_config_filepath = path.resolve(bsConfig.run_settings.cypressConfigFilePath)
1869
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
1970
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)
2272

2373
logger.debug(`cypress config path: ${cypress_config_filepath}`);
2474

2575
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)
3280
}
3381
} catch (error) {
3482
logger.error(`Error while reading cypress config: ${error.message}`)
83+
3584
// 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 })
3688
}
3789
}

bin/helpers/utils.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,6 @@ exports.getNumberOfSpecFiles = (bsConfig, args, cypressConfig) => {
999999
globCypressConfigSpecPatterns = Array.isArray(cypressConfig.e2e.specPattern) ?
10001000
cypressConfig.e2e.specPattern : [cypressConfig.e2e.specPattern];
10011001
} else {
1002-
console.log('herer', [`${testFolderPath}/**/*.+(${Constants.specFileTypes.join("|")})`])
10031002
globCypressConfigSpecPatterns = [`${testFolderPath}/**/*.+(${Constants.specFileTypes.join("|")})`]
10041003
}
10051004
} else {
@@ -1008,7 +1007,6 @@ exports.getNumberOfSpecFiles = (bsConfig, args, cypressConfig) => {
10081007
}
10091008
} else {
10101009
defaultSpecFolder = Constants.DEFAULT_CYPRESS_SPEC_PATH
1011-
// console.log('cypressConfig.integrationFolder', cypressConfig.integrationFolder)
10121010
let testFolderPath = cypressConfig.integrationFolder && cypressConfig.integrationFolder !== '.' ?
10131011
cypressConfig.integrationFolder : defaultSpecFolder;
10141012
if(!this.isUndefined(cypressConfig.testFiles)) {
@@ -1017,37 +1015,36 @@ exports.getNumberOfSpecFiles = (bsConfig, args, cypressConfig) => {
10171015
globCypressConfigSpecPatterns.push(`${testFolderPath}/${specPattern}`)
10181016
});
10191017
} else {
1020-
globCypressConfigSpecPatterns = [`${testFolderPath}/${specPattern}`]
1018+
globCypressConfigSpecPatterns = [`${testFolderPath}/${cypressConfig.testFiles}`]
10211019
}
10221020
} else {
10231021
globCypressConfigSpecPatterns = [`${testFolderPath}/**/*.+(${Constants.specFileTypes.join("|")})`]
10241022
}
10251023
}
10261024

1027-
let ignoreFiles = args.exclude || bsConfig.run_settings.exclude;
1028-
// TODO: remove console logs
1029-
let fileMatchedWithConfigSpecPattern = [];
1025+
let ignoreFiles = args.exclude || bsConfig.run_settings.exclude
1026+
let fileMatchedWithConfigSpecPattern = []
10301027
globCypressConfigSpecPatterns.forEach(specPattern => {
10311028
fileMatchedWithConfigSpecPattern.push(
10321029
...glob.sync(specPattern, {
10331030
cwd: bsConfig.run_settings.cypressProjectDir, matchBase: true, ignore: ignoreFiles
10341031
})
10351032
);
10361033
});
1037-
1038-
console.log('configSpecPattern', fileMatchedWithConfigSpecPattern)
1039-
let files
1034+
fileMatchedWithConfigSpecPattern = fileMatchedWithConfigSpecPattern.map((file) => path.resolve(bsConfig.run_settings.cypressProjectDir, file))
10401035

1036+
let files
10411037
if (globSearchPattern) {
10421038
let fileMatchedWithBstackSpecPattern = glob.sync(globSearchPattern, {
10431039
cwd: bsConfig.run_settings.cypressProjectDir, matchBase: true, ignore: ignoreFiles
10441040
});
1045-
console.log('specArg', fileMatchedWithBstackSpecPattern);
1041+
fileMatchedWithBstackSpecPattern = fileMatchedWithBstackSpecPattern.map((file) => path.resolve(bsConfig.run_settings.cypressProjectDir, file))
1042+
10461043
files = fileMatchedWithBstackSpecPattern.filter(file => fileMatchedWithConfigSpecPattern.includes(file))
10471044
} else {
10481045
files = fileMatchedWithConfigSpecPattern;
10491046
}
1050-
console.log('files', files)
1047+
10511048
logger.debug(`${files ? files.length : 0} spec files found`);
10521049
return files;
10531050
};

0 commit comments

Comments
 (0)