UNPKG

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