|
1 |
| -'use strict'; |
| 1 | +"use strict"; |
2 | 2 | const os = require("os");
|
3 | 3 | const path = require("path");
|
4 | 4 | const fs = require("fs");
|
5 | 5 |
|
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"); |
9 | 9 |
|
10 | 10 | exports.validateBstackJson = (bsConfigPath) => {
|
11 | 11 | return new Promise(function (resolve, reject) {
|
12 | 12 | try {
|
13 | 13 | logger.info(`Reading config from ${bsConfigPath}`);
|
14 | 14 | let bsConfig = require(bsConfigPath);
|
15 | 15 | 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 | + ); |
19 | 24 | }
|
20 | 25 | });
|
21 |
| -} |
| 26 | +}; |
22 | 27 |
|
23 | 28 | exports.getErrorCodeFromMsg = (errMsg) => {
|
24 | 29 | let errorCode = null;
|
@@ -51,139 +56,190 @@ exports.getErrorCodeFromMsg = (errMsg) => {
|
51 | 56 | errorCode = "invalid_directory_structure";
|
52 | 57 | break;
|
53 | 58 | }
|
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 | + ) { |
55 | 62 | errorCode = "bstack_json_path_invalid";
|
| 63 | + } else if (errMsg.includes("Invalid browserstack.json file.")) { |
| 64 | + errorCode = "bstack_json_invalid"; |
56 | 65 | }
|
57 | 66 | return errorCode;
|
58 |
| -} |
| 67 | +}; |
59 | 68 |
|
60 | 69 | exports.getErrorCodeFromErr = (err) => {
|
61 | 70 | 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"; |
66 | 75 | } else {
|
67 |
| - errorCode = 'bstack_json_invalid_unknown'; |
| 76 | + errorCode = "bstack_json_invalid_unknown"; |
68 | 77 | }
|
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 | +) => { |
73 | 88 | usageReporting.send({
|
74 | 89 | cli_args: args,
|
75 | 90 | message: message,
|
76 | 91 | message_type: message_type,
|
77 | 92 | error_code: error_code,
|
78 |
| - bstack_config: bsConfig |
| 93 | + bstack_config: bsConfig, |
79 | 94 | });
|
80 |
| -} |
| 95 | +}; |
81 | 96 |
|
82 | 97 | 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"]; |
85 | 104 | } else {
|
86 | 105 | process.env.DISABLE_USAGE_REPORTING = disableUsageReporting;
|
87 | 106 | }
|
88 |
| -} |
| 107 | +}; |
89 | 108 |
|
90 | 109 | exports.setParallels = (bsConfig, args) => {
|
91 | 110 | if (!this.isUndefined(args.parallels)) {
|
92 |
| - bsConfig['run_settings']['parallels'] = args.parallels; |
| 111 | + bsConfig["run_settings"]["parallels"] = args.parallels; |
93 | 112 | }
|
94 |
| -} |
| 113 | +}; |
95 | 114 |
|
96 | 115 | exports.setUsername = (bsConfig, args) => {
|
97 | 116 | if (!this.isUndefined(args.username)) {
|
98 |
| - bsConfig['auth']['username'] = args.username; |
| 117 | + bsConfig["auth"]["username"] = args.username; |
99 | 118 | } 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 | + ); |
102 | 123 | }
|
103 |
| -} |
| 124 | +}; |
104 | 125 |
|
105 | 126 | exports.setAccessKey = (bsConfig, args) => {
|
106 | 127 | if (!this.isUndefined(args.key)) {
|
107 |
| - bsConfig['auth']['access_key'] = args.key; |
| 128 | + bsConfig["auth"]["access_key"] = args.key; |
108 | 129 | } 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 | + ); |
111 | 134 | }
|
112 |
| -} |
| 135 | +}; |
113 | 136 |
|
114 | 137 | 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"]; |
117 | 140 | }
|
118 |
| -} |
| 141 | +}; |
119 | 142 |
|
120 |
| -exports.isUndefined = value => (value === undefined || value === null); |
| 143 | +exports.isUndefined = (value) => value === undefined || value === null; |
121 | 144 |
|
122 |
| -exports.isFloat = value => (Number(value) && Number(value) % 1 !== 0); |
| 145 | +exports.isFloat = (value) => Number(value) && Number(value) % 1 !== 0; |
123 | 146 |
|
124 | 147 | exports.isParallelValid = (value) => {
|
125 |
| - return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1) || value === Constants.constants.DEFAULT_PARALLEL_MESSAGE; |
126 |
| -} |
| 148 | + return ( |
| 149 | + this.isUndefined(value) || |
| 150 | + !( |
| 151 | + isNaN(value) || |
| 152 | + this.isFloat(value) || |
| 153 | + parseInt(value, 10) === 0 || |
| 154 | + parseInt(value, 10) < -1 |
| 155 | + ) || |
| 156 | + value === Constants.constants.DEFAULT_PARALLEL_MESSAGE |
| 157 | + ); |
| 158 | +}; |
127 | 159 |
|
128 | 160 | exports.getUserAgent = () => {
|
129 | 161 | return `BStack-Cypress-CLI/1.3.0 (${os.arch()}/${os.platform()}/${os.release()})`;
|
130 |
| -} |
| 162 | +}; |
131 | 163 |
|
132 | 164 | exports.isAbsolute = (configPath) => {
|
133 |
| - return path.isAbsolute(configPath) |
134 |
| -} |
| 165 | + return path.isAbsolute(configPath); |
| 166 | +}; |
135 | 167 |
|
136 | 168 | exports.getConfigPath = (configPath) => {
|
137 |
| - return this.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath); |
138 |
| -} |
| 169 | + return this.isAbsolute(configPath) |
| 170 | + ? configPath |
| 171 | + : path.join(process.cwd(), configPath); |
| 172 | +}; |
139 | 173 |
|
140 | 174 | exports.configCreated = (args) => {
|
141 |
| - let message = Constants.userMessages.CONFIG_FILE_CREATED |
| 175 | + let message = Constants.userMessages.CONFIG_FILE_CREATED; |
142 | 176 | logger.info(message);
|
143 |
| - this.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null); |
144 |
| -} |
| 177 | + this.sendUsageReport( |
| 178 | + null, |
| 179 | + args, |
| 180 | + message, |
| 181 | + Constants.messageTypes.SUCCESS, |
| 182 | + null |
| 183 | + ); |
| 184 | +}; |
145 | 185 |
|
146 | 186 | exports.exportResults = (buildId, buildUrl) => {
|
147 | 187 | let data = "BUILD_ID=" + buildId + "\nBUILD_URL=" + buildUrl;
|
148 | 188 | fs.writeFileSync("log/build_results.txt", data, function (err) {
|
149 | 189 | if (err) {
|
150 |
| - logger.warn(`Couldn't write BUILD_ID with value: ${buildId} to browserstack/build_results.txt`); |
151 |
| - logger.warn(`Couldn't write BUILD_URL with value: ${buildUrl} to browserstack/build_results.txt`); |
| 190 | + logger.warn( |
| 191 | + `Couldn't write BUILD_ID with value: ${buildId} to browserstack/build_results.txt` |
| 192 | + ); |
| 193 | + logger.warn( |
| 194 | + `Couldn't write BUILD_URL with value: ${buildUrl} to browserstack/build_results.txt` |
| 195 | + ); |
152 | 196 | }
|
153 | 197 | });
|
154 |
| -} |
| 198 | +}; |
155 | 199 |
|
156 | 200 | exports.deleteResults = () => {
|
157 |
| - fs.unlink("log/build_results.txt", function (err) { |
158 |
| - }); |
159 |
| -} |
| 201 | + fs.unlink("log/build_results.txt", function (err) {}); |
| 202 | +}; |
160 | 203 |
|
161 |
| -exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => { |
| 204 | +exports.isCypressProjDirValid = (cypressProjDir, integrationFoldDir) => { |
162 | 205 | // Getting absolute path
|
163 |
| - cypressDir = path.resolve(cypressDir); |
164 |
| - cypressProjDir = path.resolve(cypressProjDir); |
165 |
| - if (cypressProjDir === cypressDir) return true; |
166 |
| - let parentTokens = cypressDir.split('/').filter(i => i.length); |
167 |
| - let childTokens = cypressProjDir.split('/').filter(i => i.length); |
| 206 | + let cypressDir = path.resolve(cypressProjDir); |
| 207 | + let integrationFolderDir = path.resolve(integrationFoldDir); |
| 208 | + if (!this.isAbsolute(integrationFoldDir)) { |
| 209 | + integrationFolderDir = path.join(cypressProjDir, integrationFoldDir); |
| 210 | + } |
| 211 | + if (integrationFolderDir === cypressDir) return true; |
| 212 | + let parentTokens = cypressDir.split("/").filter((i) => i.length); |
| 213 | + let childTokens = integrationFolderDir.split("/").filter((i) => i.length); |
168 | 214 | return parentTokens.every((t, i) => childTokens[i] === t);
|
169 |
| -} |
| 215 | +}; |
170 | 216 |
|
171 | 217 | exports.getLocalFlag = (connectionSettings) => {
|
172 |
| - return !this.isUndefined(connectionSettings) && !this.isUndefined(connectionSettings.local) && connectionSettings.local |
173 |
| -} |
| 218 | + return ( |
| 219 | + !this.isUndefined(connectionSettings) && |
| 220 | + !this.isUndefined(connectionSettings.local) && |
| 221 | + connectionSettings.local |
| 222 | + ); |
| 223 | +}; |
174 | 224 |
|
175 | 225 | exports.setLocal = (bsConfig) => {
|
176 | 226 | if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL)) {
|
177 | 227 | let local = false;
|
178 |
| - if (String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true; |
179 |
| - bsConfig['connection_settings']['local'] = local; |
180 |
| - logger.info("Reading local setting from the environment variable BROWSERSTACK_LOCAL"); |
| 228 | + if (String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") |
| 229 | + local = true; |
| 230 | + bsConfig["connection_settings"]["local"] = local; |
| 231 | + logger.info( |
| 232 | + "Reading local setting from the environment variable BROWSERSTACK_LOCAL" |
| 233 | + ); |
181 | 234 | }
|
182 |
| -} |
| 235 | +}; |
183 | 236 |
|
184 | 237 | exports.setLocalIdentifier = (bsConfig) => {
|
185 | 238 | if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER)) {
|
186 |
| - bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER; |
187 |
| - logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER"); |
| 239 | + bsConfig["connection_settings"]["local_identifier"] = |
| 240 | + process.env.BROWSERSTACK_LOCAL_IDENTIFIER; |
| 241 | + logger.info( |
| 242 | + "Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER" |
| 243 | + ); |
188 | 244 | }
|
189 |
| -} |
| 245 | +}; |
0 commit comments