1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | if (require('./package.json').name === 'puppeteer-core')
|
19 | return;
|
20 |
|
21 | buildNode6IfNecessary();
|
22 |
|
23 | if (process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD) {
|
24 | console.log('**INFO** Skipping Chromium download. "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" environment variable was found.');
|
25 | return;
|
26 | }
|
27 | if (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 |
|
32 | const downloadHost = process.env.PUPPETEER_DOWNLOAD_HOST || process.env.npm_config_puppeteer_download_host;
|
33 |
|
34 | const puppeteer = require('./index');
|
35 | const browserFetcher = puppeteer.createBrowserFetcher({ host: downloadHost });
|
36 |
|
37 | const revision = process.env.PUPPETEER_CHROMIUM_REVISION || process.env.npm_config_puppeteer_chromium_revision
|
38 | || require('./package.json').puppeteer.chromium_revision;
|
39 |
|
40 | const revisionInfo = browserFetcher.revisionInfo(revision);
|
41 |
|
42 |
|
43 | if (revisionInfo.local) {
|
44 | generateProtocolTypesIfNecessary(false );
|
45 | return;
|
46 | }
|
47 |
|
48 |
|
49 | const NPM_HTTPS_PROXY = process.env.npm_config_https_proxy || process.env.npm_config_proxy;
|
50 | const NPM_HTTP_PROXY = process.env.npm_config_http_proxy || process.env.npm_config_proxy;
|
51 | const NPM_NO_PROXY = process.env.npm_config_no_proxy;
|
52 |
|
53 | if (NPM_HTTPS_PROXY)
|
54 | process.env.HTTPS_PROXY = NPM_HTTPS_PROXY;
|
55 | if (NPM_HTTP_PROXY)
|
56 | process.env.HTTP_PROXY = NPM_HTTP_PROXY;
|
57 | if (NPM_NO_PROXY)
|
58 | process.env.NO_PROXY = NPM_NO_PROXY;
|
59 |
|
60 | browserFetcher.download(revisionInfo.revision, onProgress)
|
61 | .then(() => browserFetcher.localRevisions())
|
62 | .then(onSuccess)
|
63 | .catch(onError);
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function onSuccess(localRevisions) {
|
70 | console.log('Chromium downloaded to ' + revisionInfo.folderPath);
|
71 | localRevisions = localRevisions.filter(revision => revision !== revisionInfo.revision);
|
72 |
|
73 | const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision));
|
74 | return Promise.all([...cleanupOldVersions, generateProtocolTypesIfNecessary(true )]);
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | function 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 |
|
86 | let progressBar = null;
|
87 | let lastDownloadedBytes = 0;
|
88 | function 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 |
|
103 | function toMegabytes(bytes) {
|
104 | const mb = bytes / 1024 / 1024;
|
105 | return `${Math.round(mb * 10) / 10} Mb`;
|
106 | }
|
107 |
|
108 | function buildNode6IfNecessary() {
|
109 | const fs = require('fs');
|
110 | const path = require('path');
|
111 |
|
112 |
|
113 |
|
114 | if (!fs.existsSync(path.join('utils', 'node6-transform')))
|
115 | return;
|
116 |
|
117 | if (supportsAsyncAwait())
|
118 | return;
|
119 |
|
120 | console.log('Building Puppeteer for Node 6');
|
121 | require(path.join(__dirname, 'utils', 'node6-transform'));
|
122 | }
|
123 |
|
124 | function supportsAsyncAwait() {
|
125 | try {
|
126 | new Function('async function test(){await 1}');
|
127 | } catch (error) {
|
128 | return false;
|
129 | }
|
130 | return true;
|
131 | }
|
132 |
|
133 | function 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 | }
|