copy-workbox-libraries.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  4. /*
  5. Copyright 2018 Google LLC
  6. Use of this source code is governed by an MIT-style
  7. license that can be found in the LICENSE file or at
  8. https://opensource.org/licenses/MIT.
  9. */
  10. const fse = require('fs-extra');
  11. const path = require('path');
  12. const errors = require('./errors'); // Used to filter the libraries to copy based on our package.json dependencies.
  13. const WORKBOX_PREFIX = 'workbox-';
  14. /**
  15. * This copies over a set of runtime libraries used by Workbox into a
  16. * local directory, which should be deployed alongside your service worker file.
  17. *
  18. * As an alternative to deploying these local copies, you could instead use
  19. * Workbox from its official CDN URL.
  20. *
  21. * This method is exposed for the benefit of developers using
  22. * [injectManifest()]{@link module:workbox-build.injectManifest} who would
  23. * prefer not to use the CDN copies of Workbox. Developers using
  24. * [generateSW()]{@link module:workbox-build.generateSW} don't need to
  25. * explicitly call this method, as it's called automatically when
  26. * `importWorkboxFrom` is set to `local`.
  27. *
  28. * @param {string} destDirectory The path to the parent directory under which
  29. * the new directory of libraries will be created.
  30. * @return {Promise<string>} The name of the newly created directory.
  31. *
  32. * @alias module:workbox-build.copyWorkboxLibraries
  33. */
  34. module.exports =
  35. /*#__PURE__*/
  36. function () {
  37. var _ref = (0, _asyncToGenerator2.default)(function* (destDirectory) {
  38. const thisPkg = require('../../package.json'); // Use the version string from workbox-build in the name of the parent
  39. // directory. This should be safe, because lerna will bump workbox-build's
  40. // pkg.version whenever one of the dependent libraries gets bumped, and we
  41. // care about versioning the dependent libraries.
  42. const workboxDirectoryName = `workbox-v${thisPkg.version}`;
  43. const workboxDirectoryPath = path.join(destDirectory, workboxDirectoryName);
  44. yield fse.ensureDir(workboxDirectoryPath);
  45. const copyPromises = [];
  46. const librariesToCopy = Object.keys(thisPkg.dependencies).filter(dependency => dependency.startsWith(WORKBOX_PREFIX));
  47. var _iteratorNormalCompletion = true;
  48. var _didIteratorError = false;
  49. var _iteratorError = undefined;
  50. try {
  51. for (var _iterator = librariesToCopy[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  52. const library = _step.value;
  53. const mainFilePath = require.resolve(library);
  54. const srcPath = path.dirname(mainFilePath); // fse.copy() copies all the files in a directory, not the directory itself.
  55. // See https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md#copysrc-dest-options-callback
  56. copyPromises.push(fse.copy(srcPath, workboxDirectoryPath));
  57. }
  58. } catch (err) {
  59. _didIteratorError = true;
  60. _iteratorError = err;
  61. } finally {
  62. try {
  63. if (!_iteratorNormalCompletion && _iterator.return != null) {
  64. _iterator.return();
  65. }
  66. } finally {
  67. if (_didIteratorError) {
  68. throw _iteratorError;
  69. }
  70. }
  71. }
  72. try {
  73. yield Promise.all(copyPromises);
  74. return workboxDirectoryName;
  75. } catch (error) {
  76. throw Error(`${errors['unable-to-copy-workbox-libraries']} ${error}`);
  77. }
  78. });
  79. return function (_x) {
  80. return _ref.apply(this, arguments);
  81. };
  82. }();