-
Notifications
You must be signed in to change notification settings - Fork 9
/
install.js
203 lines (175 loc) · 5.57 KB
/
install.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Shamelessly copied from The Obvious Corporation
/*
* This simply fetches the right version of chromedriver for the current platform.
*/
'use strict';
var cp = require('child_process');
var fs = require('fs');
var http = require('http');
var kew = require('kew');
var path = require('path');
var url = require('url');
var rimraf = require('rimraf').sync;
var AdmZip = require('adm-zip');
var helper = require('./lib/chromedriver');
var ncp = require('ncp');
var npmconf = require('npmconf');
var util = require('util');
var libPath = path.join(__dirname, 'lib', 'bin', 'chromedriver');
var downloadUrl = 'http://chromedriver.storage.googleapis.com/2.9/chromedriver_';
if (process.platform === 'linux' && process.arch === 'x64') {
downloadUrl += 'linux64.zip';
} else if (process.platform === 'linux') {
downloadUrl += 'linux32.zip';
} else if (process.platform === 'darwin') {
downloadUrl += 'mac32.zip';
} else if (process.platform === 'win32') {
downloadUrl += 'win32.zip';
} else {
console.log('Unexpected platform or architecture:', process.platform, process.arch);
process.exit(1);
}
var fileName = downloadUrl.split('/').pop();
function getRequestOptions(proxyUrl) {
if (proxyUrl) {
var options = url.parse(proxyUrl);
options.path = downloadUrl;
options.headers = { Host: url.parse(downloadUrl).host };
// turn basic authorization into proxy-authorization
if (options.auth) {
options.headers['Proxy-Authorization'] = 'Basic ' + new Buffer(options.auth).toString('base64');
delete options.auth;
}
return options;
} else {
return url.parse(downloadUrl);
}
}
function requestBinary(requestOptions, filePath) {
var deferred = kew.defer();
var count = 0;
var notifiedCount = 0;
var outFile = fs.openSync(filePath, 'w');
var client = http.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);
deferred.resolve(true);
});
} else {
client.abort();
deferred.reject('Error with http request: ' + util.inspect(response.headers));
}
});
return deferred.promise;
}
function extractDownload(filePath, tmpPath) {
var deferred = kew.defer();
var options = {cwd: tmpPath};
rimraf(libPath);
if (filePath.substr(-4) === '.zip') {
console.log('Extracting zip contents');
try {
var zip = new AdmZip(filePath);
zip.extractAllTo(path.dirname(filePath), true);
deferred.resolve(true);
} catch (e) {
console.log(e);
deferred.reject('Error extracting archive ' + e.stack);
}
} else {
console.log('Extracting tar contents (via spawned process)');
cp.execFile('tar', ['jxf', filePath], options, function (err) {
if (err) {
deferred.reject('Error extracting archive ' + err.stack);
} else {
deferred.resolve(true);
}
});
}
return deferred.promise;
}
function copyIntoPlace(tmpPath, targetPath) {
var deferred = kew.defer();
// Look for the extracted directory, so we can rename it.
var files = fs.readdirSync(tmpPath);
for (var i = 0; i < files.length; i++) {
var file = path.join(tmpPath, files[i]);
if (fs.statSync(file).isFile() && file.search('zip') === -1) {
console.log('Renaming extracted folder', file, '->', targetPath);
if (process.platform === 'win32') {
targetPath = targetPath + '.exe';
}
ncp(file, targetPath, deferred.makeNodeResolver());
break;
}
}
return deferred.promise;
}
function fixFilePermissions() {
// Check that the binary is user-executable and fix it if it isn't (problems with unzip library)
if (process.platform !== 'win32') {
var stat = fs.statSync(helper.path);
// 64 == 0100 (no octal literal in strict mode)
if (!(stat.mode & 64)) {
console.log('Fixing file permissions');
fs.chmodSync(helper.path, '755');
}
}
}
function mkdir(name) {
var dir = path.dirname(name);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
npmconf.load(function(err, conf) {
if (err) {
console.log('Error loading npm config');
console.error(err);
process.exit(1);
return;
}
var tmpPath = path.join(conf.get('tmp'), 'chromedriver');
var downloadedFile = path.join(tmpPath, fileName);
var promise= kew.resolve(true);
// Start the install.
if (!fs.existsSync(downloadedFile)) {
promise = promise.then(function () {
rimraf(tmpPath);
mkdir(downloadedFile);
console.log('Downlaoading', downloadUrl);
console.log('Saving to', downloadedFile);
return requestBinary(getRequestOptions(conf.get('proxy')), downloadedFile);
});
} else {
console.log('Download already available at', downloadedFile);
}
promise.then(function () {
return extractDownload(downloadedFile, tmpPath);
})
.then(function () {
return copyIntoPlace(tmpPath, libPath);
})
.then(function () {
return fixFilePermissions();
})
.then(function () {
console.log('Done. Chromedriver binary available at', helper.path);
})
.fail(function (err) {
console.error('Chromedriver installation failed', err.stack);
process.exit(1);
});
});