-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.js
153 lines (144 loc) · 4.84 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
'use strict';
const StreamZip = require('node-stream-zip');
const os = require('os');
const fs = require('fs');
const path = require('path');
const pkg = require('./package');
const { DownloaderHelper } = require('node-downloader-helper');
const { promisify } = require('util');
const tar = require('tar');
const unlink = promisify(fs.unlink);
const mkdir = promisify(fs.mkdir);
const chmod = promisify(fs.chmod);
// The version of the driver that will be installed
const GECKODRIVER_VERSION = process.env.GECKODRIVER_VERSION
? `v${process.env.GECKODRIVER_VERSION}`
: `v${pkg.geckodriver_version}`;
const isWindows = os.platform() === 'win32';
function byteHelper(value) {
// https://gist.github.com/thomseddon/3511330
const units = ['b', 'kB', 'MB', 'GB', 'TB'],
number = Math.floor(Math.log(value) / Math.log(1024));
return (
(value / Math.pow(1024, Math.floor(number))).toFixed(1) +
' ' +
units[number]
);
}
function getDriverUrl() {
let urlBase;
if (process.env.GECKODRIVER_BASE_URL) {
urlBase = process.env.GECKODRIVER_BASE_URL;
} else if (os.platform() === 'linux' && os.arch() === 'arm') {
urlBase = `https://github.com/sitespeedio/geckodriver/releases/download/v0.29.0/`;
} else {
urlBase = `https://github.com/mozilla/geckodriver/releases/download/${GECKODRIVER_VERSION}/`;
}
switch (os.platform()) {
case 'darwin':
// Starting from Geckodriver v0.29.1, there is a separate build for arm64
// architecture. Let's install it if we are on arm64 as well.
// eslint-disable-next-line no-case-declarations
const arch = os.arch() === 'arm64' ? '-aarch64' : '';
return `${urlBase}geckodriver-${GECKODRIVER_VERSION}-macos${arch}.tar.gz`;
case 'linux': {
if (os.arch() === 'arm') {
// Don't want to spend hours to build a new one, so for now serve 0.29.0
// or unreleased 0.30.0
return `${urlBase}geckodriver-0.30.0-linux-arm.tar.gz`;
} else {
const arch = os.arch() === 'x64' ? '64' : '32';
return `${urlBase}geckodriver-${GECKODRIVER_VERSION}-linux${arch}.tar.gz`;
}
}
case 'win32': {
const arch = os.arch() === 'x64' ? 'win64' : 'win32';
return `${urlBase}geckodriver-${GECKODRIVER_VERSION}-${arch}.zip`;
}
default:
return undefined;
}
}
async function download() {
if (
process.env.npm_config_geckodriver_skip_download ||
process.env.GECKODRIVER_SKIP_DOWNLOAD
) {
console.log('Skip downloading Geckodriver');
} else {
const downloadUrl = getDriverUrl();
if (downloadUrl) {
try {
await mkdir('vendor');
} catch (e) {
try {
await unlink('vendor/geckodriver');
} catch (e) {
// nothing to do here
}
}
const dl = new DownloaderHelper(downloadUrl, 'vendor', {
fileName: 'geckodriver' + (isWindows ? '.zip' : '.tar.gz')
});
dl.on('error', err =>
console.error('Could not download Geckodriver: ' + downloadUrl, err)
)
.on('progress', stats => {
const progress = stats.progress.toFixed(1);
const speed = byteHelper(stats.speed);
const downloaded = byteHelper(stats.downloaded);
const total = byteHelper(stats.total);
console.log(`${speed}/s - ${progress}% [${downloaded}/${total}]`);
})
.on('end', () => {
if (isWindows) {
const zip = new StreamZip({
file: 'vendor/geckodriver.zip',
storeEntries: true
});
zip.on('ready', () => {
zip.extract(null, './vendor', async err => {
console.log(
err
? 'Could not extract and install Geckodriver'
: `Geckodriver ${GECKODRIVER_VERSION} installed in ${path.join(
__dirname,
'vendor'
)}`
);
zip.close();
await unlink('vendor/geckodriver.zip');
await chmod('vendor/geckodriver.exe', '755');
});
});
} else {
tar
.x({
file: 'vendor/geckodriver.tar.gz',
cwd: 'vendor'
})
.then(async () => {
await unlink('vendor/geckodriver.tar.gz');
await chmod('vendor/geckodriver', '755');
console.log(
`Geckodriver ${GECKODRIVER_VERSION} installed in ${path.join(
__dirname,
'vendor'
)}`
);
});
}
});
dl.start();
} else {
console.log(
'Skipping installing Geckodriver on ' +
os.platform() +
' for ' +
os.arch() +
" since there's no official build"
);
}
}
}
download();