copy-workbox-libraries.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. /*
  3. Copyright 2018 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. const fse = require('fs-extra');
  9. const upath = require('upath');
  10. const errors = require('./errors'); // Used to filter the libraries to copy based on our package.json dependencies.
  11. const WORKBOX_PREFIX = 'workbox-'; // The directory within each package containing the final bundles.
  12. const BUILD_DIR = 'build';
  13. /**
  14. * This copies over a set of runtime libraries used by Workbox into a
  15. * local directory, which should be deployed alongside your service worker file.
  16. *
  17. * As an alternative to deploying these local copies, you could instead use
  18. * Workbox from its official CDN URL.
  19. *
  20. * This method is exposed for the benefit of developers using
  21. * [injectManifest()]{@link module:workbox-build.injectManifest} who would
  22. * prefer not to use the CDN copies of Workbox. Developers using
  23. * [generateSW()]{@link module:workbox-build.generateSW} don't need to
  24. * explicitly call this method.
  25. *
  26. * @param {string} destDirectory The path to the parent directory under which
  27. * the new directory of libraries will be created.
  28. * @return {Promise<string>} The name of the newly created directory.
  29. *
  30. * @alias module:workbox-build.copyWorkboxLibraries
  31. */
  32. module.exports = async destDirectory => {
  33. const thisPkg = require('../../package.json'); // Use the version string from workbox-build in the name of the parent
  34. // directory. This should be safe, because lerna will bump workbox-build's
  35. // pkg.version whenever one of the dependent libraries gets bumped, and we
  36. // care about versioning the dependent libraries.
  37. const workboxDirectoryName = `workbox-v${thisPkg.version}`;
  38. const workboxDirectoryPath = upath.join(destDirectory, workboxDirectoryName);
  39. await fse.ensureDir(workboxDirectoryPath);
  40. const copyPromises = [];
  41. const librariesToCopy = Object.keys(thisPkg.dependencies).filter(dependency => dependency.startsWith(WORKBOX_PREFIX));
  42. for (const library of librariesToCopy) {
  43. // Get the path to the package on the user's filesystem by require-ing
  44. // the package's `package.json` file via the node resolution algorithm.
  45. const libraryPath = upath.dirname(require.resolve(`${library}/package.json`));
  46. const buildPath = upath.join(libraryPath, BUILD_DIR); // fse.copy() copies all the files in a directory, not the directory itself.
  47. // See https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md#copysrc-dest-options-callback
  48. copyPromises.push(fse.copy(buildPath, workboxDirectoryPath));
  49. }
  50. try {
  51. await Promise.all(copyPromises);
  52. return workboxDirectoryName;
  53. } catch (error) {
  54. throw Error(`${errors['unable-to-copy-workbox-libraries']} ${error}`);
  55. }
  56. };