UNPKG

3.95 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 Downloader = require('./lib/Downloader');
29const downloader = Downloader.createDefault();
30
31const platform = downloader.currentPlatform();
32const revision = Downloader.defaultRevision();
33const ProgressBar = require('progress');
34
35const revisionInfo = downloader.revisionInfo(platform, revision);
36// Do nothing if the revision is already downloaded.
37if (revisionInfo.downloaded)
38 return;
39
40// Override current environment proxy settings with npm configuration, if any.
41const NPM_HTTPS_PROXY = process.env.npm_config_https_proxy || process.env.npm_config_proxy;
42const NPM_HTTP_PROXY = process.env.npm_config_http_proxy || process.env.npm_config_proxy;
43const NPM_NO_PROXY = process.env.npm_config_no_proxy;
44
45if (NPM_HTTPS_PROXY)
46 process.env.HTTPS_PROXY = NPM_HTTPS_PROXY;
47if (NPM_HTTP_PROXY)
48 process.env.HTTP_PROXY = NPM_HTTP_PROXY;
49if (NPM_NO_PROXY)
50 process.env.NO_PROXY = NPM_NO_PROXY;
51
52const allRevisions = downloader.downloadedRevisions();
53const downloadHost = process.env.PUPPETEER_DOWNLOAD_HOST || process.env.npm_config_puppeteer_download_host;
54if (downloadHost)
55 downloader.setDownloadHost(downloadHost);
56downloader.downloadRevision(platform, revision, onProgress)
57 .then(onSuccess)
58 .catch(onError);
59
60/**
61 * @return {!Promise}
62 */
63function onSuccess() {
64 console.log('Chromium downloaded to ' + revisionInfo.folderPath);
65 // Remove previous chromium revisions.
66 const cleanupOldVersions = allRevisions.map(({platform, revision}) => downloader.removeRevision(platform, revision));
67 return Promise.all(cleanupOldVersions);
68}
69
70/**
71 * @param {!Error} error
72 */
73function onError(error) {
74 console.error(`ERROR: Failed to download Chromium r${revision}! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.`);
75 console.error(error);
76 process.exit(1);
77}
78
79let progressBar = null;
80function onProgress(bytesTotal, delta) {
81 if (!progressBar) {
82 progressBar = new ProgressBar(`Downloading Chromium r${revision} - ${toMegabytes(bytesTotal)} [:bar] :percent :etas `, {
83 complete: '=',
84 incomplete: ' ',
85 width: 20,
86 total: bytesTotal,
87 });
88 }
89 progressBar.tick(delta);
90}
91
92function toMegabytes(bytes) {
93 const mb = bytes / 1024 / 1024;
94 return `${Math.round(mb * 10) / 10} Mb`;
95}
96
97function buildNode6IfNecessary() {
98 const fs = require('fs');
99 const path = require('path');
100
101 // if this package is installed from NPM, then it already has up-to-date node6
102 // folder.
103 if (!fs.existsSync(path.join('utils', 'node6-transform')))
104 return;
105 let asyncawait = true;
106 try {
107 new Function('async function test(){await 1}');
108 } catch (error) {
109 asyncawait = false;
110 }
111 // if async/await is supported, then node6 is not needed.
112 if (asyncawait)
113 return;
114 // Re-build node6/ folder.
115 console.log('Building Puppeteer for Node 6');
116 require(path.join(__dirname, 'utils', 'node6-transform'));
117}