UNPKG

3.26 kBJavaScriptView Raw
1var os = require('os');
2var fs = require('fs');
3var path = require('path');
4var url = require('url');
5
6var got = require('got');
7var tar = require('tar');
8var AdmZip = require('adm-zip');
9var proxyAgent = require('https-proxy-agent');
10
11var Promise = require('bluebird');
12
13var platform = os.platform();
14var arch = os.arch();
15
16var skipDownload = process.env.GECKODRIVER_SKIP_DOWNLOAD || process.env.npm_config_geckodriver_skip_download;
17if (skipDownload === 'true') {
18 console.log('Found GECKODRIVER_SKIP_DOWNLOAD variable, skipping installation.');
19 process.exit(0);
20}
21
22var baseCDNURL = process.env.GECKODRIVER_CDNURL || process.env.npm_config_geckodriver_cdnurl || 'https://github.com/mozilla/geckodriver/releases/download';
23var CACHED_ARCHIVE = process.env.GECKODRIVER_FILEPATH ? path.resolve(process.env.GECKODRIVER_FILEPATH) : undefined;
24
25var version = process.env.GECKODRIVER_VERSION || process.env.npm_config_geckodriver_version || '0.29.0';
26
27// Remove trailing slash if included
28baseCDNURL = baseCDNURL.replace(/\/+$/, '');
29
30var baseDownloadUrl = baseCDNURL + '/v' + version + '/geckodriver-v' + version;
31var DOWNLOAD_MAC = baseDownloadUrl +'-macos.tar.gz';
32var DOWNLOAD_LINUX64 = baseDownloadUrl +'-linux64.tar.gz';
33var DOWNLOAD_LINUX32 = baseDownloadUrl +'-linux32.tar.gz';
34var DOWNLOAD_WIN32 = baseDownloadUrl +'-win32.zip';
35var DOWNLOAD_WIN64 = baseDownloadUrl +'-win64.zip';
36
37// TODO: move this to package.json or something
38var downloadUrl = DOWNLOAD_MAC;
39var outFile = 'geckodriver.tar.gz';
40var executable = 'geckodriver';
41
42var downloadOptions = {}
43var proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || process.env.https_proxy || process.env.http_proxy || null;
44if (proxy !== null) {
45 downloadOptions.agent = new proxyAgent(proxy);
46}
47
48if (platform === 'linux') {
49 downloadUrl = arch === 'x64' ? DOWNLOAD_LINUX64 : DOWNLOAD_LINUX32;
50}
51
52if (platform === 'win32') {
53 // No 32-bits of geckodriver for now
54 downloadUrl = arch === 'x64' ? DOWNLOAD_WIN64 : DOWNLOAD_WIN32;
55 outFile = 'geckodriver.zip';
56 executable = 'geckodriver.exe';
57}
58
59if (CACHED_ARCHIVE) {
60 extract(CACHED_ARCHIVE);
61} else {
62 process.stdout.write('Downloading geckodriver... ');
63 got.stream(url.parse(downloadUrl), downloadOptions)
64 .pipe(fs.createWriteStream(outFile))
65 .on('close', function () {
66 extract(path.join(__dirname, outFile));
67 });
68}
69
70
71function extract(archivePath) {
72 process.stdout.write('Extracting... ');
73 var targetDirectoryPath = __dirname;
74
75 return new Promise(function (resolve, reject) {
76 if (outFile.indexOf('.tar.gz') >= 0) {
77 tar.extract({
78 file: archivePath,
79 cwd: targetDirectoryPath
80 }).then(function (err) {
81 if (err) {
82 reject(err);
83 } else {
84 resolve();
85 }
86 });
87 } else if (outFile.indexOf('.zip') >= 0) {
88 new AdmZip(archivePath).extractAllToAsync(targetDirectoryPath, true, function (err) {
89 if (err) {
90 reject(err);
91 } else {
92 resolve();
93 }
94 });
95 } else {
96 reject('This archive extension is not supported: ' + archivePath);
97 }
98 })
99 .then(function () {
100 console.log('Complete.');
101 })
102 .catch(function (err) {
103 console.log('Something is wrong ', err.stack);
104 });
105}