-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.js
164 lines (153 loc) · 4.85 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
'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 unlink = promisify(fs.unlink);
const mkdir = promisify(fs.mkdir);
const chmod = promisify(fs.chmod);
// The version of the driver that will be installed
const CHROMEDRIVER_VERSION = process.env.CHROMEDRIVER_VERSION
? process.env.CHROMEDRIVER_VERSION
: `${pkg.chromedriver_version}`;
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 getChromedriverUrl() {
let urlBase;
if (process.env.CHROMEDRIVER_BASE_URL) {
urlBase = process.env.CHROMEDRIVER_BASE_URL;
} else {
urlBase = `https://storage.googleapis.com/chrome-for-testing-public/${CHROMEDRIVER_VERSION}/`;
}
switch (os.platform()) {
case 'darwin':
if (os.arch() === 'x64')
return urlBase + 'mac-x64/chromedriver-mac-x64.zip';
else return urlBase + 'mac-arm64/chromedriver-mac-arm64.zip';
case 'linux':
if (os.arch() === 'x64')
return urlBase + 'linux64/chromedriver-linux64.zip';
else return undefined;
case 'win32':
return urlBase + 'win32/chromedriver-win32.zip';
case 'win64':
return urlBase + 'win64/chromedriver-win64.zip';
default:
return undefined;
}
}
function getPath() {
switch (os.platform()) {
case 'darwin':
if (os.arch() === 'x64') return 'chromedriver-mac-x64';
else return 'chromedriver-mac-arm64';
case 'linux':
if (os.arch() === 'x64') return 'chromedriver-linux64';
else return undefined;
case 'win32':
return 'chromedriver-win32';
case 'win64':
return 'chromedriver-win64';
default:
return undefined;
}
}
async function download() {
if (
process.env.npm_config_chromedriver_skip_download ||
process.env.CHROMEDRIVER_SKIP_DOWNLOAD
) {
console.log('Skip downloading Chromedriver');
} else {
const downloadUrl = getChromedriverUrl();
if (downloadUrl) {
try {
await mkdir('vendor');
} catch (e) {
try {
await unlink('vendor/chromedriver');
} catch (e) {
// DO nada
}
}
const dl = new DownloaderHelper(downloadUrl, 'vendor', {
fileName: 'chromedriver.zip'
});
dl.on('error', err =>
console.error('Could not download Chromedriver: ' + downloadUrl, err)
)
.on('progress', stats => {
const progress = stats.progress.toFixed(1);
const downloaded = byteHelper(stats.downloaded);
const total = byteHelper(stats.total);
console.log(`${progress}% [${downloaded}/${total}]`);
})
.on('end', () => {
const zip = new StreamZip({
file: 'vendor/chromedriver.zip',
storeEntries: true
});
zip.on('error', function(err) {
// We got an error from unpacking
console.error(
`Chromedriver ${CHROMEDRIVER_VERSION} could not be installed: ${err} `
);
// How should we exit?
});
let fileEnding = '';
if (os.platform() === 'win32' || os.platform() === 'win64') {
fileEnding = '.exe';
}
zip.on('ready', () => {
zip.extract(
getPath() + '/chromedriver' + fileEnding,
'./vendor/chromedriver' + fileEnding,
async err => {
console.log(
err
? 'Could not extract and install Chromedriver'
: `Chromedriver ${CHROMEDRIVER_VERSION} installed in ${path.join(
__dirname,
'vendor'
)}`
);
zip.close();
await unlink('vendor/chromedriver.zip');
let driverPath = 'vendor/chromedriver';
if (os.platform() === 'win32' || os.platform() === 'win64') {
driverPath = driverPath + '.exe';
}
await chmod(driverPath, '755');
}
);
});
});
dl.start();
} else {
console.log(
'Skipping installing Chromedriver on ' +
os.platform() +
' for ' +
os.arch() +
" since there's no official build"
);
if (os.platform() === 'linux' && os.arch() === 'arm') {
console.log(
'You can try downloading Chromedriver using: sudo apt-get install chromium-chromedriver -y'
);
}
}
}
}
download();