Skip to content

Commit 71838b6

Browse files
committed
Merge branch 'master' into cypress_config_file_support
2 parents d8d560d + 2b7b833 commit 71838b6

File tree

2 files changed

+117
-70
lines changed

2 files changed

+117
-70
lines changed

bin/helpers/utils.js

Lines changed: 116 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
'use strict';
1+
"use strict";
22
const os = require("os");
33
const path = require("path");
44
const fs = require("fs");
55

6-
const usageReporting = require('./usageReporting'),
7-
logger = require('./logger').winstonLogger,
8-
Constants = require('./constants');
6+
const usageReporting = require("./usageReporting"),
7+
logger = require("./logger").winstonLogger,
8+
Constants = require("./constants");
99

1010
exports.validateBstackJson = (bsConfigPath) => {
1111
return new Promise(function (resolve, reject) {
1212
try {
1313
logger.info(`Reading config from ${bsConfigPath}`);
1414
let bsConfig = require(bsConfigPath);
1515
resolve(bsConfig);
16-
}
17-
catch (e) {
18-
reject("Couldn't find the browserstack.json file at \"" + bsConfigPath + "\". Please use --config-file <path to browserstack.json>.");
16+
} catch (e) {
17+
reject(
18+
e.code === "MODULE_NOT_FOUND"
19+
? "Couldn't find the browserstack.json file at \"" +
20+
bsConfigPath +
21+
'". Please use --config-file <path to browserstack.json>.'
22+
: `Invalid browserstack.json file. Error : ${e.message}`
23+
);
1924
}
2025
});
21-
}
26+
};
2227

2328
exports.getErrorCodeFromMsg = (errMsg) => {
2429
let errorCode = null;
@@ -51,71 +56,89 @@ exports.getErrorCodeFromMsg = (errMsg) => {
5156
errorCode = "invalid_directory_structure";
5257
break;
5358
}
54-
if (errMsg.includes("Please use --config-file <path to browserstack.json>.")) {
59+
if (
60+
errMsg.includes("Please use --config-file <path to browserstack.json>.")
61+
) {
5562
errorCode = "bstack_json_path_invalid";
63+
} else if (errMsg.includes("Invalid browserstack.json file.")) {
64+
errorCode = "bstack_json_invalid";
5665
}
5766
return errorCode;
58-
}
67+
};
5968

6069
exports.getErrorCodeFromErr = (err) => {
6170
let errorCode = null;
62-
if (err.code === 'SyntaxError') {
63-
errorCode = 'bstack_json_parse_error';
64-
} else if (err.code === 'EACCES') {
65-
errorCode = 'bstack_json_no_permission';
71+
if (err.code === "SyntaxError") {
72+
errorCode = "bstack_json_parse_error";
73+
} else if (err.code === "EACCES") {
74+
errorCode = "bstack_json_no_permission";
6675
} else {
67-
errorCode = 'bstack_json_invalid_unknown';
76+
errorCode = "bstack_json_invalid_unknown";
6877
}
69-
return errorCode
70-
}
71-
72-
exports.sendUsageReport = (bsConfig, args, message, message_type, error_code) => {
78+
return errorCode;
79+
};
80+
81+
exports.sendUsageReport = (
82+
bsConfig,
83+
args,
84+
message,
85+
message_type,
86+
error_code
87+
) => {
7388
usageReporting.send({
7489
cli_args: args,
7590
message: message,
7691
message_type: message_type,
7792
error_code: error_code,
78-
bstack_config: bsConfig
93+
bstack_config: bsConfig,
7994
});
80-
}
95+
};
8196

8297
exports.setUsageReportingFlag = (bsConfig, disableUsageReporting) => {
83-
if (disableUsageReporting === undefined && bsConfig && bsConfig['disable_usage_reporting'] != undefined) {
84-
process.env.DISABLE_USAGE_REPORTING = bsConfig['disable_usage_reporting'];
98+
if (
99+
disableUsageReporting === undefined &&
100+
bsConfig &&
101+
bsConfig["disable_usage_reporting"] != undefined
102+
) {
103+
process.env.DISABLE_USAGE_REPORTING = bsConfig["disable_usage_reporting"];
85104
} else {
86105
process.env.DISABLE_USAGE_REPORTING = disableUsageReporting;
87106
}
88-
}
107+
};
89108

90109
exports.setParallels = (bsConfig, args) => {
91110
if (!this.isUndefined(args.parallels)) {
92-
bsConfig['run_settings']['parallels'] = args.parallels;
111+
bsConfig["run_settings"]["parallels"] = args.parallels;
93112
}
94-
}
113+
};
95114

96115
exports.setUsername = (bsConfig, args) => {
97116
if (!this.isUndefined(args.username)) {
98-
bsConfig['auth']['username'] = args.username;
117+
bsConfig["auth"]["username"] = args.username;
99118
} else if (!this.isUndefined(process.env.BROWSERSTACK_USERNAME)) {
100-
bsConfig['auth']['username'] = process.env.BROWSERSTACK_USERNAME;
101-
logger.info("Reading username from the environment variable BROWSERSTACK_USERNAME");
119+
bsConfig["auth"]["username"] = process.env.BROWSERSTACK_USERNAME;
120+
logger.info(
121+
"Reading username from the environment variable BROWSERSTACK_USERNAME"
122+
);
102123
}
103-
}
124+
};
104125

105126
exports.setAccessKey = (bsConfig, args) => {
106127
if (!this.isUndefined(args.key)) {
107-
bsConfig['auth']['access_key'] = args.key;
128+
bsConfig["auth"]["access_key"] = args.key;
108129
} else if (!this.isUndefined(process.env.BROWSERSTACK_ACCESS_KEY)) {
109-
bsConfig['auth']['access_key'] = process.env.BROWSERSTACK_ACCESS_KEY;
110-
logger.info("Reading access key from the environment variable BROWSERSTACK_ACCESS_KEY");
130+
bsConfig["auth"]["access_key"] = process.env.BROWSERSTACK_ACCESS_KEY;
131+
logger.info(
132+
"Reading access key from the environment variable BROWSERSTACK_ACCESS_KEY"
133+
);
111134
}
112-
}
135+
};
113136

114137
exports.setBuildName = (bsConfig, args) => {
115-
if (!this.isUndefined(args['build-name'])) {
116-
bsConfig['run_settings']['build_name'] = args['build-name'];
138+
if (!this.isUndefined(args["build-name"])) {
139+
bsConfig["run_settings"]["build_name"] = args["build-name"];
117140
}
118-
}
141+
};
119142

120143
exports.searchForOption = (option) => {
121144
return (process.argv.indexOf(option) > -1);
@@ -180,71 +203,95 @@ exports.fixCommaSeparatedString = (string) => {
180203

181204
exports.isUndefined = value => (value === undefined || value === null);
182205

183-
exports.isFloat = value => (Number(value) && Number(value) % 1 !== 0);
206+
exports.isFloat = (value) => Number(value) && Number(value) % 1 !== 0;
184207

185208
exports.isParallelValid = (value) => {
186209
return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1) || value === Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE;
187210
}
188211

189212
exports.getUserAgent = () => {
190-
return `BStack-Cypress-CLI/1.3.0 (${os.arch()}/${os.platform()}/${os.release()})`;
191-
}
213+
return `BStack-Cypress-CLI/1.4.0 (${os.arch()}/${os.platform()}/${os.release()})`;
214+
};
192215

193216
exports.isAbsolute = (configPath) => {
194-
return path.isAbsolute(configPath)
195-
}
217+
return path.isAbsolute(configPath);
218+
};
196219

197220
exports.getConfigPath = (configPath) => {
198-
return this.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);
199-
}
221+
return this.isAbsolute(configPath)
222+
? configPath
223+
: path.join(process.cwd(), configPath);
224+
};
200225

201226
exports.configCreated = (args) => {
202-
let message = Constants.userMessages.CONFIG_FILE_CREATED
227+
let message = Constants.userMessages.CONFIG_FILE_CREATED;
203228
logger.info(message);
204-
this.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
205-
}
229+
this.sendUsageReport(
230+
null,
231+
args,
232+
message,
233+
Constants.messageTypes.SUCCESS,
234+
null
235+
);
236+
};
206237

207238
exports.exportResults = (buildId, buildUrl) => {
208239
let data = "BUILD_ID=" + buildId + "\nBUILD_URL=" + buildUrl;
209240
fs.writeFileSync("log/build_results.txt", data, function (err) {
210241
if (err) {
211-
logger.warn(`Couldn't write BUILD_ID with value: ${buildId} to browserstack/build_results.txt`);
212-
logger.warn(`Couldn't write BUILD_URL with value: ${buildUrl} to browserstack/build_results.txt`);
242+
logger.warn(
243+
`Couldn't write BUILD_ID with value: ${buildId} to browserstack/build_results.txt`
244+
);
245+
logger.warn(
246+
`Couldn't write BUILD_URL with value: ${buildUrl} to browserstack/build_results.txt`
247+
);
213248
}
214249
});
215-
}
250+
};
216251

217252
exports.deleteResults = () => {
218-
fs.unlink("log/build_results.txt", function (err) {
219-
});
220-
}
253+
fs.unlink("log/build_results.txt", function (err) {});
254+
};
221255

222-
exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => {
256+
exports.isCypressProjDirValid = (cypressProjDir, integrationFoldDir) => {
223257
// Getting absolute path
224-
cypressDir = path.resolve(cypressDir);
225-
cypressProjDir = path.resolve(cypressProjDir);
226-
if (cypressProjDir === cypressDir) return true;
227-
let parentTokens = cypressDir.split('/').filter(i => i.length);
228-
let childTokens = cypressProjDir.split('/').filter(i => i.length);
258+
let cypressDir = path.resolve(cypressProjDir);
259+
let integrationFolderDir = path.resolve(integrationFoldDir);
260+
if (!this.isAbsolute(integrationFoldDir)) {
261+
integrationFolderDir = path.resolve(path.join(cypressProjDir, integrationFoldDir));
262+
}
263+
if (integrationFolderDir === cypressDir) return true;
264+
let parentTokens = cypressDir.split("/").filter((i) => i.length);
265+
let childTokens = integrationFolderDir.split("/").filter((i) => i.length);
229266
return parentTokens.every((t, i) => childTokens[i] === t);
230-
}
267+
};
231268

232269
exports.getLocalFlag = (connectionSettings) => {
233-
return !this.isUndefined(connectionSettings) && !this.isUndefined(connectionSettings.local) && connectionSettings.local
234-
}
270+
return (
271+
!this.isUndefined(connectionSettings) &&
272+
!this.isUndefined(connectionSettings.local) &&
273+
connectionSettings.local
274+
);
275+
};
235276

236277
exports.setLocal = (bsConfig) => {
237278
if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL)) {
238279
let local = false;
239-
if (String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true;
240-
bsConfig['connection_settings']['local'] = local;
241-
logger.info("Reading local setting from the environment variable BROWSERSTACK_LOCAL");
280+
if (String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true")
281+
local = true;
282+
bsConfig["connection_settings"]["local"] = local;
283+
logger.info(
284+
"Reading local setting from the environment variable BROWSERSTACK_LOCAL"
285+
);
242286
}
243-
}
287+
};
244288

245289
exports.setLocalIdentifier = (bsConfig) => {
246290
if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER)) {
247-
bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
248-
logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER");
291+
bsConfig["connection_settings"]["local_identifier"] =
292+
process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
293+
logger.info(
294+
"Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER"
295+
);
249296
}
250-
}
297+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "browserstack-cypress-cli",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "BrowserStack Cypress CLI for Cypress integration with BrowserStack's remote devices.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)