UNPKG

1.74 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
17const Downloader = require('./utils/ChromiumDownloader');
18const revision = require('./package').puppeteer.chromium_revision;
19const ProgressBar = require('progress');
20
21// Do nothing if the revision is already downloaded.
22if (Downloader.revisionInfo(Downloader.currentPlatform(), revision))
23 return;
24
25let allRevisions = Downloader.downloadedRevisions();
26Downloader.downloadRevision(Downloader.currentPlatform(), revision, onProgress)
27 // Remove previous chromium revisions.
28 .then(() => Promise.all(allRevisions.map(({platform, revision}) => Downloader.removeRevision(platform, revision))))
29 .catch(error => console.error('Download failed: ' + error.message));
30
31let progressBar = null;
32function onProgress(bytesTotal, delta) {
33 if (!progressBar) {
34 progressBar = new ProgressBar(`Downloading Chromium r${revision} - ${toMegabytes(bytesTotal)} [:bar] :percent :etas `, {
35 complete: '=',
36 incomplete: ' ',
37 width: 20,
38 total: bytesTotal,
39 });
40 }
41 progressBar.tick(delta);
42}
43
44function toMegabytes(bytes) {
45 let mb = bytes / 1024 / 1024;
46 return (Math.round(mb * 10) / 10) + ' Mb';
47}
48