forked from webdriverio-community/node-geckodriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (70 loc) · 2.42 KB
/
index.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
var os = require('os');
var fs = require('fs');
var path = require('path');
var url = require('url');
var got = require('got');
var tar = require('tar');
var AdmZip = require('adm-zip');
var Promise = require('bluebird');
var platform = os.platform();
var arch = os.arch();
var baseCDNURL = process.env.GECKODRIVER_CDNURL || process.env.npm_config_geckodriver_cdnurl || 'https://github.com/mozilla/geckodriver/releases/download';
// Remove trailing slash if included
baseCDNURL = baseCDNURL.replace(/\/+$/, '');
var DOWNLOAD_MAC = baseCDNURL + '/v0.21.0/geckodriver-v0.21.0-macos.tar.gz';
var DOWNLOAD_LINUX64 = baseCDNURL + '/v0.21.0/geckodriver-v0.21.0-linux64.tar.gz';
var DOWNLOAD_LINUX32 = baseCDNURL + '/v0.21.0/geckodriver-v0.21.0-linux32.tar.gz';
var DOWNLOAD_WIN32 = baseCDNURL + '/v0.21.0/geckodriver-v0.21.0-win32.zip';
var DOWNLOAD_WIN64 = baseCDNURL + '/v0.21.0/geckodriver-v0.21.0-win64.zip';
// TODO: move this to package.json or something
var downloadUrl = DOWNLOAD_MAC;
var outFile = 'geckodriver.tar.gz';
var executable = 'geckodriver';
if (platform === 'linux') {
downloadUrl = arch === 'x64' ? DOWNLOAD_LINUX64 : DOWNLOAD_LINUX32;
}
if (platform === 'win32') {
// No 32-bits of geckodriver for now
downloadUrl = arch === 'x64' ? DOWNLOAD_WIN64 : DOWNLOAD_WIN32;
outFile = 'geckodriver.zip';
executable = 'geckodriver.exe';
}
process.stdout.write('Downloading geckodriver... ');
got.stream(url.parse(downloadUrl))
.pipe(fs.createWriteStream(outFile))
.on('close', function() {
process.stdout.write('Extracting... ');
extract(path.join(__dirname, outFile), __dirname)
.then(function(){
console.log('Complete.');
})
.catch(function(err){
console.log('Something is wrong ', err.stack);
});
});
function extract(archivePath, targetDirectoryPath) {
return new Promise(function(resolve, reject) {
if (outFile.indexOf('.tar.gz') >= 0) {
tar.extract({
file: archivePath,
cwd: targetDirectoryPath
}).then(function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
} else if (outFile.indexOf('.zip') >= 0) {
new AdmZip(archivePath).extractAllToAsync(targetDirectoryPath, true, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
reject('This archive extension is not supported: ' + archivePath);
}
});
}