Skip to content

Add cypress to npm_dependency if not present #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const path = require('path');
const { inspect } = require('util');

const archiver = require("../helpers/archiver"),
zipUploader = require("../helpers/zipUpload"),
Expand All @@ -24,6 +25,7 @@ const { getStackTraceUrl } = require('../helpers/sync/syncSpecsLogs');

module.exports = function run(args, rawArgs) {

markBlockStart('preBuild');
// set debug mode (--cli-debug)
utils.setDebugMode(args);

Expand Down Expand Up @@ -185,6 +187,8 @@ module.exports = function run(args, rawArgs) {
logger.debug("Started build creation");
markBlockStart('createBuild');
return build.createBuild(bsConfig, zip).then(function (data) {
markBlockEnd('preBuild');
markBlockStart('buildProcessing');
logger.debug("Completed build creation");
markBlockEnd('createBuild');
markBlockEnd('total');
Expand Down Expand Up @@ -216,6 +220,8 @@ module.exports = function run(args, rawArgs) {
if (args.sync) {
logger.debug("Started polling build status from BrowserStack");
syncRunner.pollBuildStatus(bsConfig, data, rawArgs, buildReportData).then(async (exitCode) => {
markBlockEnd('buildProcessing');
markBlockStart('postBuild');
logger.debug("Completed polling of build status");

// stop the Local instance
Expand All @@ -234,6 +240,7 @@ module.exports = function run(args, rawArgs) {
// Generate custom report!
reportGenerator(bsConfig, data.build_id, args, rawArgs, buildReportData, function(){
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null, buildReportData, rawArgs);
markBlockEnd('postBuild');
utils.handleSyncExit(exitCode, data.dashboard_url);
});
} else {
Expand Down
7 changes: 7 additions & 0 deletions bin/helpers/checkUploaded.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const checkPackageMd5 = (runSettings) => {
}

if (typeof runSettings.npm_dependencies === 'object') {
if (!("cypress" in runSettings.npm_dependencies)) {
if ("cypress_version" in runSettings && !runSettings.cypress_version.toString().match(Constants.LATEST_VERSION_SYNTAX_REGEX)) {
runSettings.npm_dependencies.cypress = runSettings.cypress_version;
} else {
runSettings.npm_dependencies.cypress = "latest";
}
}
Object.assign(packageJSON, {
devDependencies: utils.sortJsonKeys(runSettings.npm_dependencies),
});
Expand Down
84 changes: 84 additions & 0 deletions bin/helpers/readCypressConfigUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"use strict";
const path = require("path");
const fs = require("fs");
const exec = require("child_process").exec;
const execSync = require("child_process").execSync;

const isUndefined = value => (value === undefined || value === null);

exports.getCypressConfigDir = (bsConfig) => {
let directoryPath;
try {
directoryPath = !isUndefined(bsConfig.run_settings.cypress_proj_dir) ? bsConfig.run_settings.cypress_proj_dir : process.cwd();
console.log(`--> dir_path: ${directoryPath}`)
if (directoryPath.endsWith("/")) {
directoryPath = directoryPath.slice(0,-1);
}
} catch (error) {
console.log(`---> Error: ${error}`)
}
return path.join(directoryPath, bsConfig.run_settings.cypressConfigFilePath)
}

exports.readCypressConfig = (bsConfig) => {
try {
let cypress_conf_dir = this.getCypressConfigDir(bsConfig)
console.log(`---> Cypress conf dir: ${cypress_conf_dir}`)
const conf_lang = this.detectLanguage(path.dirname(cypress_conf_dir))
console.log(`---> Detected lang: ${conf_lang}`)
if (conf_lang == 'ts') {
cypress_conf_dir = this.convertTsConfig(cypress_conf_dir)
// cypress_conf_dir = path.join(path.dirname(cypress_conf_dir), 'cypress.config.js')
}
console.log(`---> Got cypress converted conf dir: ${cypress_conf_dir}`)
// Perform actions
const cypress_config = require(cypress_conf_dir)
console.log(`---> bsconf: ${JSON.stringify(cypress_config)}`)
} catch (error) {
console.log(`---> Got error reading cypress config file: ${JSON.stringify(error)}`)
}
}

exports.detectLanguage = (projectRoot) => {
for (let extension of ['ts', 'mts']) {
if (fs.existsSync(path.join(projectRoot, `cypress.config.${extension}`))) {
return 'ts'
}
}
for (let extension of ['js', 'cjs', 'mjs']) {
if (fs.existsSync(path.join(projectRoot, `cypress.config.${extension}`))) {
return 'js'
}
}
return 'js' // Default to js
}

exports.convertTsConfig = (cypress_conf_dir) => {
const working_dir = path.dirname(cypress_conf_dir);
const ts_working_dir = path.join(working_dir, 'tsWorkingDir')
const tsconfig_path = path.join(working_dir, 'tsconfig.json') // TODO: find tsconfig if cypress.config.ts and tsconfig not in same location
execSync('rm -rf tsWorkingDir', {cwd: working_dir})
execSync('mkdir tsWorkingDir', {cwd: working_dir})
console.log(`---> 123: ${working_dir}`)
let tsc_command = `npx tsc --outDir ${ts_working_dir} --listEmittedFiles true`;
if (!fs.existsSync(tsconfig_path)) {
tsc_command = `${tsc_command} cypress.config.ts`
}
try {
let stdout = execSync(tsc_command, { cwd: working_dir }).toString()
console.log(stdout)
const lines = stdout.split('\n');
let foundLine = null;
for(let i =0; i < lines.length; i++) {
if(lines[i].indexOf("cypress.config.") > -1){
foundLine = lines[i]
break;
}
}
console.log(foundLine)
return foundLine.split('TSFILE: ').pop()
} catch (err) {
console.log("output",err)
console.log("sdterr",err.stderr.toString())
}
}
2 changes: 2 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const usageReporting = require("./usageReporting"),
transports = require('./logger').transports;

const request = require('request');
const { readCypressConfig } = require("./readCypressConfigUtil");

exports.validateBstackJson = (bsConfigPath) => {
return new Promise(function (resolve, reject) {
Expand Down Expand Up @@ -309,6 +310,7 @@ exports.setCypressConfigFilename = (bsConfig, args) => {

logger.debug(`Setting cypress config file path = ${bsConfig.run_settings.cypressConfigFilePath}`);
logger.debug(`Setting cypress project dir = ${bsConfig.run_settings.cypressProjectDir}`);
console.log(readCypressConfig(bsConfig))
}

exports.setCypressTestSuiteType = (bsConfig) => {
Expand Down