UNPKG

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