UNPKG

4.21 kBJavaScriptView Raw
1/**
2 * Copyright 2017 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17buildNode6IfNecessary();
18
19if (process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD) {
20 console.log('**INFO** Skipping Chromium download. "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" environment variable was found.');
21 return;
22}
23if (process.env.NPM_CONFIG_PUPPETEER_SKIP_CHROMIUM_DOWNLOAD || process.env.npm_config_puppeteer_skip_chromium_download) {
24 console.log('**INFO** Skipping Chromium download. "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" was set in npm config.');
25 return;
26}
27
28const downloadHost = process.env.PUPPETEER_DOWNLOAD_HOST || process.env.npm_config_puppeteer_download_host;
29
30const puppeteer = require('./index');
31const browserFetcher = puppeteer.createBrowserFetcher({ host: downloadHost });
32
33const revision = process.env.PUPPETEER_CHROMIUM_REVISION || process.env.npm_config_puppeteer_chromium_revision
34 || require('./package.json').puppeteer.chromium_revision;
35
36const revisionInfo = browserFetcher.revisionInfo(revision);
37
38// Do nothing if the revision is already downloaded.
39if (revisionInfo.local)
40 return;
41
42// Override current environment proxy settings with npm configuration, if any.
43const NPM_HTTPS_PROXY = process.env.npm_config_https_proxy || process.env.npm_config_proxy;
44const NPM_HTTP_PROXY = process.env.npm_config_http_proxy || process.env.npm_config_proxy;
45const NPM_NO_PROXY = process.env.npm_config_no_proxy;
46
47if (NPM_HTTPS_PROXY)
48 process.env.HTTPS_PROXY = NPM_HTTPS_PROXY;
49if (NPM_HTTP_PROXY)
50 process.env.HTTP_PROXY = NPM_HTTP_PROXY;
51if (NPM_NO_PROXY)
52 process.env.NO_PROXY = NPM_NO_PROXY;
53
54browserFetcher.download(revisionInfo.revision, onProgress)
55 .then(() => browserFetcher.localRevisions())
56 .then(onSuccess)
57 .catch(onError);
58
59/**
60 * @param {!Array<string>}
61 * @return {!Promise}
62 */
63function onSuccess(localRevisions) {
64 console.log('Chromium downloaded to ' + revisionInfo.folderPath);
65 localRevisions = localRevisions.filter(revision => revision !== revisionInfo.revision);
66 // Remove previous chromium revisions.
67 const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision));
68 return Promise.all(cleanupOldVersions);
69}
70
71/**
72 * @param {!Error} error
73 */
74function onError(error) {
75 console.error(`ERROR: Failed to download Chromium r${revision}! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.`);
76 console.error(error);
77 process.exit(1);
78}
79
80let progressBar = null;
81let lastDownloadedBytes = 0;
82function onProgress(downloadedBytes, totalBytes) {
83 if (!progressBar) {
84 const ProgressBar = require('progress');
85 progressBar = new ProgressBar(`Downloading Chromium r${revision} - ${toMegabytes(totalBytes)} [:bar] :percent :etas `, {
86 complete: '=',
87 incomplete: ' ',
88 width: 20,
89 total: totalBytes,
90 });
91 }
92 const delta = downloadedBytes - lastDownloadedBytes;
93 lastDownloadedBytes = downloadedBytes;
94 progressBar.tick(delta);
95}
96
97function toMegabytes(bytes) {
98 const mb = bytes / 1024 / 1024;
99 return `${Math.round(mb * 10) / 10} Mb`;
100}
101
102function buildNode6IfNecessary() {
103 const fs = require('fs');
104 const path = require('path');
105
106 // if this package is installed from NPM, then it already has up-to-date node6
107 // folder.
108 if (!fs.existsSync(path.join('utils', 'node6-transform')))
109 return;
110 let asyncawait = true;
111 try {
112 new Function('async function test(){await 1}');
113 } catch (error) {
114 asyncawait = false;
115 }
116 // if async/await is supported, then node6 is not needed.
117 if (asyncawait)
118 return;
119 // Re-build node6/ folder.
120 console.log('Building Puppeteer for Node 6');
121 require(path.join(__dirname, 'utils', 'node6-transform'));
122}