get-file-manifest-entries.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. /*
  3. Copyright 2021 Google LLC
  4. Use of this source code is governed by an MIT-style
  5. license that can be found in the LICENSE file or at
  6. https://opensource.org/licenses/MIT.
  7. */
  8. var __importDefault = (this && this.__importDefault) || function (mod) {
  9. return (mod && mod.__esModule) ? mod : { "default": mod };
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.getFileManifestEntries = void 0;
  13. const assert_1 = __importDefault(require("assert"));
  14. const errors_1 = require("./errors");
  15. const get_composite_details_1 = require("./get-composite-details");
  16. const get_file_details_1 = require("./get-file-details");
  17. const get_string_details_1 = require("./get-string-details");
  18. const transform_manifest_1 = require("./transform-manifest");
  19. async function getFileManifestEntries({ additionalManifestEntries, dontCacheBustURLsMatching, globDirectory, globFollow, globIgnores, globPatterns = [], globStrict, manifestTransforms, maximumFileSizeToCacheInBytes, modifyURLPrefix, templatedURLs, }) {
  20. const warnings = [];
  21. const allFileDetails = new Map();
  22. try {
  23. for (const globPattern of globPatterns) {
  24. const { globbedFileDetails, warning } = (0, get_file_details_1.getFileDetails)({
  25. globDirectory,
  26. globFollow,
  27. globIgnores,
  28. globPattern,
  29. globStrict,
  30. });
  31. if (warning) {
  32. warnings.push(warning);
  33. }
  34. for (const details of globbedFileDetails) {
  35. if (details && !allFileDetails.has(details.file)) {
  36. allFileDetails.set(details.file, details);
  37. }
  38. }
  39. }
  40. }
  41. catch (error) {
  42. // If there's an exception thrown while globbing, then report
  43. // it back as a warning, and don't consider it fatal.
  44. if (error instanceof Error && error.message) {
  45. warnings.push(error.message);
  46. }
  47. }
  48. if (templatedURLs) {
  49. for (const url of Object.keys(templatedURLs)) {
  50. (0, assert_1.default)(!allFileDetails.has(url), errors_1.errors['templated-url-matches-glob']);
  51. const dependencies = templatedURLs[url];
  52. if (Array.isArray(dependencies)) {
  53. const details = dependencies.reduce((previous, globPattern) => {
  54. try {
  55. const { globbedFileDetails, warning } = (0, get_file_details_1.getFileDetails)({
  56. globDirectory,
  57. globFollow,
  58. globIgnores,
  59. globPattern,
  60. globStrict,
  61. });
  62. if (warning) {
  63. warnings.push(warning);
  64. }
  65. return previous.concat(globbedFileDetails);
  66. }
  67. catch (error) {
  68. const debugObj = {};
  69. debugObj[url] = dependencies;
  70. throw new Error(`${errors_1.errors['bad-template-urls-asset']} ` +
  71. `'${globPattern}' from '${JSON.stringify(debugObj)}':\n` +
  72. `${error instanceof Error ? error.toString() : ''}`);
  73. }
  74. }, []);
  75. if (details.length === 0) {
  76. throw new Error(`${errors_1.errors['bad-template-urls-asset']} The glob ` +
  77. `pattern '${dependencies.toString()}' did not match anything.`);
  78. }
  79. allFileDetails.set(url, (0, get_composite_details_1.getCompositeDetails)(url, details));
  80. }
  81. else if (typeof dependencies === 'string') {
  82. allFileDetails.set(url, (0, get_string_details_1.getStringDetails)(url, dependencies));
  83. }
  84. }
  85. }
  86. const transformedManifest = await (0, transform_manifest_1.transformManifest)({
  87. additionalManifestEntries,
  88. dontCacheBustURLsMatching,
  89. manifestTransforms,
  90. maximumFileSizeToCacheInBytes,
  91. modifyURLPrefix,
  92. fileDetails: Array.from(allFileDetails.values()),
  93. });
  94. transformedManifest.warnings.push(...warnings);
  95. return transformedManifest;
  96. }
  97. exports.getFileManifestEntries = getFileManifestEntries;