forked from dtolstyi/node-chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
87 lines (72 loc) · 2.74 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
'use strict';
const fs = require('fs');
const extractZip = require('extract-zip');
const got = require('got');
const tmp = require('tmp');
const debug = require('debug')('node-chromium');
const config = require('./config');
const utils = require('./utils');
const chromiumRevision = process.env.CHROMIUM_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);
}
});
});
}
async function downloadChromiumRevision(revision) {
const tmpPath = await createTempFile();
debug('Downloading Chromium archive from Google CDN');
const url = utils.getDownloadUrl(revision);
return _downloadFile(url, tmpPath);
}
function _downloadFile(url, destPath) {
return new Promise((resolve, reject) => {
got.stream(url, utils.getRequestOptions(url))
.on('error', error => {
console.error('An error occurred while trying to download file', error.message);
reject(error);
})
.pipe(fs.createWriteStream(destPath))
.on('error', error => {
console.error('An error occurred while trying to save file to disk', error);
reject(error);
})
.on('finish', () => {
resolve(destPath);
});
});
}
function unzipArchive(archivePath, outputFolder) {
debug('Started extracting archive', archivePath);
return new Promise((resolve, reject) => {
extractZip(archivePath, {dir: outputFolder}, error => {
if (error) {
console.error('An error occurred while trying to extract archive', error);
reject(error);
} else {
debug('Archive was successfully extracted');
resolve(true);
}
});
});
}
async function install() {
try {
console.info('Step 1. Retrieving Chromium latest revision number');
const revision = chromiumRevision || await utils.getLatestRevisionNumber();
console.info(`Step 2. Downloading Chromium (this might take a while). Revision number: ${revision}`);
const tmpPath = await downloadChromiumRevision(revision);
console.info('Step 3. Setting up Chromium binaries');
await unzipArchive(tmpPath, config.BIN_OUT_PATH);
console.info('Process is successfully finished');
} catch (err) {
console.error('An error occurred while trying to setup Chromium. Resolve all issues and restart the process', err);
}
}
module.exports = install();