install.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * This file is part of public API.
  18. *
  19. * By default, the `puppeteer` package runs this script during the installation
  20. * process unless one of the env flags is provided.
  21. * `puppeteer-core` package doesn't include this step at all. However, it's
  22. * still possible to install Chromium using this script when necessary.
  23. */
  24. if (process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD) {
  25. logPolitely('**INFO** Skipping Chromium download. "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" environment variable was found.');
  26. return;
  27. }
  28. if (process.env.NPM_CONFIG_PUPPETEER_SKIP_CHROMIUM_DOWNLOAD || process.env.npm_config_puppeteer_skip_chromium_download) {
  29. logPolitely('**INFO** Skipping Chromium download. "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" was set in npm config.');
  30. return;
  31. }
  32. if (process.env.NPM_PACKAGE_CONFIG_PUPPETEER_SKIP_CHROMIUM_DOWNLOAD || process.env.npm_package_config_puppeteer_skip_chromium_download) {
  33. logPolitely('**INFO** Skipping Chromium download. "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" was set in project config.');
  34. return;
  35. }
  36. const downloadHost = process.env.PUPPETEER_DOWNLOAD_HOST || process.env.npm_config_puppeteer_download_host || process.env.npm_package_config_puppeteer_download_host;
  37. const puppeteer = require('./index');
  38. const browserFetcher = puppeteer.createBrowserFetcher({ host: downloadHost });
  39. const revision = process.env.PUPPETEER_CHROMIUM_REVISION || process.env.npm_config_puppeteer_chromium_revision || process.env.npm_package_config_puppeteer_chromium_revision
  40. || require('./package.json').puppeteer.chromium_revision;
  41. const revisionInfo = browserFetcher.revisionInfo(revision);
  42. // Do nothing if the revision is already downloaded.
  43. if (revisionInfo.local) {
  44. generateProtocolTypesIfNecessary(false /* updated */);
  45. return;
  46. }
  47. // Override current environment proxy settings with npm configuration, if any.
  48. const NPM_HTTPS_PROXY = process.env.npm_config_https_proxy || process.env.npm_config_proxy;
  49. const NPM_HTTP_PROXY = process.env.npm_config_http_proxy || process.env.npm_config_proxy;
  50. const NPM_NO_PROXY = process.env.npm_config_no_proxy;
  51. if (NPM_HTTPS_PROXY)
  52. process.env.HTTPS_PROXY = NPM_HTTPS_PROXY;
  53. if (NPM_HTTP_PROXY)
  54. process.env.HTTP_PROXY = NPM_HTTP_PROXY;
  55. if (NPM_NO_PROXY)
  56. process.env.NO_PROXY = NPM_NO_PROXY;
  57. browserFetcher.download(revisionInfo.revision, onProgress)
  58. .then(() => browserFetcher.localRevisions())
  59. .then(onSuccess)
  60. .catch(onError);
  61. /**
  62. * @param {!Array<string>}
  63. * @return {!Promise}
  64. */
  65. function onSuccess(localRevisions) {
  66. logPolitely('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. * @param {!Error} error
  74. */
  75. function onError(error) {
  76. console.error(`ERROR: Failed to download Chromium r${revision}! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.`);
  77. console.error(error);
  78. process.exit(1);
  79. }
  80. let progressBar = null;
  81. let lastDownloadedBytes = 0;
  82. function onProgress(downloadedBytes, totalBytes) {
  83. if (!progressBar) {
  84. const ProgressBar = require('progress');
  85. progressBar = new ProgressBar(`Downloading Chromium r${revision} - ${toMegabytes(totalBytes)} [:bar] :percent :etas `, {
  86. complete: '=',
  87. incomplete: ' ',
  88. width: 20,
  89. total: totalBytes,
  90. });
  91. }
  92. const delta = downloadedBytes - lastDownloadedBytes;
  93. lastDownloadedBytes = downloadedBytes;
  94. progressBar.tick(delta);
  95. }
  96. function toMegabytes(bytes) {
  97. const mb = bytes / 1024 / 1024;
  98. return `${Math.round(mb * 10) / 10} Mb`;
  99. }
  100. function generateProtocolTypesIfNecessary(updated) {
  101. const fs = require('fs');
  102. const path = require('path');
  103. if (!fs.existsSync(path.join(__dirname, 'utils', 'protocol-types-generator')))
  104. return;
  105. if (!updated && fs.existsSync(path.join(__dirname, 'lib', 'protocol.d.ts')))
  106. return;
  107. return require('./utils/protocol-types-generator');
  108. }
  109. function logPolitely(toBeLogged) {
  110. const logLevel = process.env.npm_config_loglevel;
  111. const logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel) > -1;
  112. if (!logLevelDisplay)
  113. console.log(toBeLogged);
  114. }