This repository was archived by the owner on May 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
121 lines (106 loc) · 3.19 KB
/
Copy pathutils.js
File metadata and controls
121 lines (106 loc) · 3.19 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
var Log = require('./logger'),
logger = new Log(global.logLevel),
config = require('./config'),
http = require('http'),
url = require('url'),
querystring = require('querystring');
String.prototype.escapeSpecialChars = function() {
return this.replace(/\n/g, "\\n")
.replace(/\\s/g, "\s")
.replace(/\\\'/, "\'");
};
var titleCase = function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
var uuid = function uuidGenerator() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
var browserString = function browserString(config) {
var os_details = config.os + ' ' + config.os_version;
if (config.browser) {
return os_details + ', ' + (config.browser == 'ie' ? 'Internet Explorer' : titleCase(config.browser)) + ' ' + config.browser_version;
} else {
return os_details + (config.device ? (', ' + config.device) : "");
}
};
var objectSize = function objectSize(obj) {
var size = 0,
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
var alertBrowserStack = function alertBrowserStack(subject, content, params, fn) {
var endpoint = config.alert_endpoint || "http://www.browserstack.com/automate/alert";
var urlObject = url.parse(endpoint);
var context = config.alert_context || "Runner alert";
logger.info("[%s] [%s] %s", new Date(), context, subject);
if (typeof fn !== 'function') {
if (typeof params === 'function') {
} else {
fn = function() {
process.kill(process.pid, 'SIGINT');
};
}
}
if (!params || typeof(params) !== 'object') {
params = {};
}
params.subject = subject;
params.content = content;
params.context = context;
var body = querystring.stringify(params);
var options = {
hostname: urlObject.hostname,
port: urlObject.port,
path: urlObject.path,
method: 'POST',
auth: config.username + ":" + config.key,
headers: {
'Content-Length': body.length
}
};
var callback = function(res) {
var response = "";
res.setEncoding("utf8");
res.on("data", function(chunk) {
response += chunk;
});
res.on("end", function() {
if (res.statusCode !== 200) {
var message;
if (res.headers["content-type"].indexOf("json") !== -1) {
var resp = JSON.parse(response);
message = resp.message;
message += ' - ' + resp.errors.map(function(err) {
return '`' + err.field + '`' + ' ' + err.code;
}).join(', ');
} else {
message = response;
}
if (!message && res.statusCode === 403) {
message = "Forbidden";
}
fn(new Error(message));
} else {
fn(null, JSON.parse(response));
}
});
};
var request = http.request(options, callback);
request.write(body);
request.end();
return request;
};
exports.titleCase = titleCase;
exports.uuid = uuid;
exports.browserString = browserString;
exports.objectSize = objectSize;
exports.alertBrowserStack = alertBrowserStack;