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