forked from dtolstyi/node-chromium
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinstall.js
110 lines (93 loc) · 3.48 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
'use strict';
const fs = require('fs');
const extractZip = require('extract-zip');
const got = require('got');
const tmp = require('tmp');
const config = require('./config');
const utils = require('./utils');
const operationSystemRevisions = require('./operationSystemRevisions');
function getOperationSystemRevision(platform, architecture) {
const osWithRevision = operationSystemRevisions.find(operationSystem => {
let sameOs = operationSystem.platform === platform;
if (operationSystem.architecture) {
sameOs = sameOs && (operationSystem.architecture === architecture);
}
return sameOs;
});
if (!osWithRevision) {
console.log('Unknown platform or architecture found:', platform, architecture);
throw new Error('Unsupported platform');
}
return osWithRevision.revision;
}
function createTempFile() {
return new Promise((resolve, reject) => {
tmp.file((error, path) => {
if (error) {
console.log('An error occured while trying to create temporary file', error);
reject(error);
} else {
resolve(path);
}
});
});
}
function getCdnDownloadStream(cdnUrl) {
return new Promise((resolve, reject) => {
const stream = got.stream(cdnUrl);
stream.on('error', error => {
if (error.statusCode === 404) {
resolve(null);
} else {
reject(error);
}
});
stream.on('response', () => resolve(stream));
});
}
function downloadChromium() {
return new Promise((resolve, reject) => {
const platform = process.platform;
const architecture = process.arch;
const revision = getOperationSystemRevision(platform, architecture);
const cdnUrls = utils.getOsCdnUrls({
platform,
architecture,
revision
});
const promises = cdnUrls.map(cdnUrl => getCdnDownloadStream(cdnUrl));
promises.push(createTempFile());
Promise.all(promises)
.then(promiseResults => {
const path = promiseResults.pop();
const downloadStream = promiseResults.find(promiseResult => promiseResult);
console.log('Downloading Chromium archive from Google CDN');
downloadStream
.pipe(fs.createWriteStream(path))
.on('error', error => {
console.log('An error occurred while trying to save Chromium archive to disk', error);
reject(error);
})
.on('finish', () => {
resolve(path);
});
});
});
}
function unzipArchive(archivePath, outputFolder) {
console.log('Started extracting archive', archivePath);
return new Promise((resolve, reject) => {
extractZip(archivePath, {dir: outputFolder}, error => {
if (error) {
console.log('An error occurred while trying to extract archive', error);
reject(error);
} else {
console.log('Archive was successfully extracted');
resolve(true);
}
});
});
}
module.exports = downloadChromium()
.then(path => unzipArchive(path, config.BIN_OUT_PATH))
.catch(err => console.error('An error occurred while trying to setup Chromium. Resolve all issues and restart the process', err));