This repository was archived by the owner on Dec 19, 2023. It is now read-only.
forked from arian/selenium-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.js
More file actions
117 lines (98 loc) · 3.2 KB
/
install.js
File metadata and controls
117 lines (98 loc) · 3.2 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
"use strict";
var fs = require('fs');
var https = require('https');
var path = require('path');
var url = require('url');
var util = require('util');
var AdmZip = require('adm-zip');
// TODO: use https://selenium-release.storage.googleapis.com/index.html to find latest version number
var jarURL = 'https://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.jar';
// TODO: use contents of https://chromedriver.storage.googleapis.com/LATEST_RELEASE to determine version to download
var chromeDriverURL = 'https://chromedriver.storage.googleapis.com/2.14/chromedriver_';
if (process.platform === 'linux' && process.arch === 'x64') {
chromeDriverURL += 'linux64.zip';
} else if (process.platform === 'linux') {
chromeDriverURL += 'linux32.zip';
} else if (process.platform === 'darwin') {
chromeDriverURL += 'mac32.zip';
} else if (process.platform === 'win32') {
chromeDriverURL += 'win32.zip';
} else {
console.log('Unexpected platform or architecture:', process.platform, process.arch);
process.exit(1);
}
function doThings(things, callback) {
var results = [], i = 0;
function doThing() {
var thing = things[i++];
if (!thing) return callback(results);
thing[0].apply(null, thing.slice(1).concat(function(err) {
if (err) return callback(err);
results.push(Array.prototype.slice(arguments, 1));
doThing();
}));
}
doThing();
}
doThings([
[downloadFile, jarURL],
[downloadFile, chromeDriverURL],
[unzip, chromeDriverURL],
[chmod, 'chromedriver']
], function() {
console.log('done');
});
function unzip(fileURL, callback) {
var requestOptions = url.parse(fileURL);
var filePath = path.basename(requestOptions.path);
var extractedPath = process.cwd();
var zip = new AdmZip(filePath);
zip.extractAllTo(extractedPath, true);
callback();
}
function chmod(filePath, callback) {
if (process.platform != 'win32') {
console.log('chmod', filePath);
fs.chmod(filePath, '0777', callback);
} else {
callback();
}
}
function downloadFile(fileURL, callback) {
var requestOptions = url.parse(fileURL);
var filePath = path.basename(requestOptions.path);
if (fs.existsSync(filePath)) {
console.log(filePath + ' already exists');
callback(null, filePath);
return;
}
console.log('Downloading ' + fileURL + ' to ' + filePath);
var count = 0;
var notifiedCount = 0;
var writePath = filePath + '-download-' + Date.now();
var outFile = fs.openSync(writePath, 'w');
var client = https.get(requestOptions, function(response) {
var status = response.statusCode;
console.log('Receiving...');
if (status === 200) {
response.addListener('data', function(data) {
fs.writeSync(outFile, data, 0, data.length, null);
count += data.length;
if ((count - notifiedCount) > 800000) {
console.log('Received ' + Math.floor(count / 1024) + 'K...');
notifiedCount = count;
}
});
response.addListener('end', function() {
console.log('Received ' + Math.floor(count / 1024) + 'K total.');
fs.closeSync(outFile);
fs.renameSync(writePath, filePath);
callback(null, filePath);
});
} else {
client.abort();
console.error('Error requesting archive');
callback(new Error('Error with https request: ' + util.inspect(response.headers)));
}
});
}