utils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @fileoverview Provides utilities for setting up environments.
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. "use strict";
  9. var path = require("path");
  10. var helpers = require("../helpers");
  11. var globals = require("../globals");
  12. /**
  13. * Obtains the globals for a list of files.
  14. *
  15. * @param {Array.<String>} files
  16. * The array of files to get globals for. The paths are relative to the topsrcdir.
  17. * @returns {Object}
  18. * Returns an object with keys of the global names and values of if they are
  19. * writable or not.
  20. */
  21. function getGlobalsForScripts(environmentName, files, extraDefinitions) {
  22. let fileGlobals = extraDefinitions;
  23. const root = helpers.rootDir;
  24. for (const file of files) {
  25. const fileName = path.join(root, file);
  26. try {
  27. fileGlobals = fileGlobals.concat(globals.getGlobalsForFile(fileName));
  28. } catch (e) {
  29. console.error(`Could not load globals from file ${fileName}: ${e}`);
  30. console.error(
  31. `You may need to update the mappings for the ${environmentName} environment`
  32. );
  33. throw new Error(`Could not load globals from file ${fileName}: ${e}`);
  34. }
  35. }
  36. var globalObjects = {};
  37. for (let global of fileGlobals) {
  38. globalObjects[global.name] = global.writable;
  39. }
  40. return globalObjects;
  41. }
  42. module.exports = {
  43. getScriptGlobals(
  44. environmentName,
  45. files,
  46. extraDefinitions = [],
  47. extraEnv = {}
  48. ) {
  49. if (helpers.isMozillaCentralBased()) {
  50. return {
  51. globals: getGlobalsForScripts(environmentName, files, extraDefinitions),
  52. ...extraEnv,
  53. };
  54. }
  55. return helpers.getSavedEnvironmentItems(environmentName);
  56. },
  57. };