UNPKG

4.76 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 generateProtocolTypesIfNecessary(false /* updated */);
41 return;
42}
43
44// Override current environment proxy settings with npm configuration, if any.
45const NPM_HTTPS_PROXY = process.env.npm_config_https_proxy || process.env.npm_config_proxy;
46const NPM_HTTP_PROXY = process.env.npm_config_http_proxy || process.env.npm_config_proxy;
47const NPM_NO_PROXY = process.env.npm_config_no_proxy;
48
49if (NPM_HTTPS_PROXY)
50 process.env.HTTPS_PROXY = NPM_HTTPS_PROXY;
51if (NPM_HTTP_PROXY)
52 process.env.HTTP_PROXY = NPM_HTTP_PROXY;
53if (NPM_NO_PROXY)
54 process.env.NO_PROXY = NPM_NO_PROXY;
55
56browserFetcher.download(revisionInfo.revision, onProgress)
57 .then(() => browserFetcher.localRevisions())
58 .then(onSuccess)
59 .catch(onError);
60
61/**
62 * @param {!Array<string>}
63 * @return {!Promise}
64 */
65function onSuccess(localRevisions) {
66 console.log('Chromium downloaded to ' + revisionInfo.folderPath);
67 localRevisions = localRevisions.filter(revision => revision !== revisionInfo.revision);
68 // Remove previous chromium revisions.
69 const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision));
70 return Promise.all([...cleanupOldVersions, generateProtocolTypesIfNecessary(true /* updated */)]);
71}
72
73/**
74 * @param {!Error} error
75 */
76function onError(error) {
77 console.error(`ERROR: Failed to download Chromium r${revision}! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.`);
78 console.error(error);
79 process.exit(1);
80}
81
82let progressBar = null;
83let lastDownloadedBytes = 0;
84function onProgress(downloadedBytes, totalBytes) {
85 if (!progressBar) {
86 const ProgressBar = require('progress');
87 progressBar = new ProgressBar(`Downloading Chromium r${revision} - ${toMegabytes(totalBytes)} [:bar] :percent :etas `, {
88 complete: '=',
89 incomplete: ' ',
90 width: 20,
91 total: totalBytes,
92 });
93 }
94 const delta = downloadedBytes - lastDownloadedBytes;
95 lastDownloadedBytes = downloadedBytes;
96 progressBar.tick(delta);
97}
98
99function toMegabytes(bytes) {
100 const mb = bytes / 1024 / 1024;
101 return `${Math.round(mb * 10) / 10} Mb`;
102}
103
104function buildNode6IfNecessary() {
105 const fs = require('fs');
106 const path = require('path');
107
108 // if this package is installed from NPM, then it already has up-to-date node6
109 // folder.
110 if (!fs.existsSync(path.join('utils', 'node6-transform')))
111 return;
112 // if async/await is supported, then node6 is not needed.
113 if (supportsAsyncAwait())
114 return;
115 // Re-build node6/ folder.
116 console.log('Building Puppeteer for Node 6');
117 require(path.join(__dirname, 'utils', 'node6-transform'));
118}
119
120function supportsAsyncAwait() {
121 try {
122 new Function('async function test(){await 1}');
123 } catch (error) {
124 return false;
125 }
126 return true;
127}
128
129function generateProtocolTypesIfNecessary(updated) {
130 if (!supportsAsyncAwait())
131 return;
132 const fs = require('fs');
133 const path = require('path');
134 if (!fs.existsSync(path.join(__dirname, 'utils', 'protocol-types-generator')))
135 return;
136 if (!updated && fs.existsSync(path.join(__dirname, 'lib', 'protocol.d.ts')))
137 return;
138 return require('./utils/protocol-types-generator');
139}