package-config.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.io/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. /**
  11. * Sorts the keys of the given object.
  12. * @returns A new object instance with sorted keys
  13. */
  14. function sortObjectByKeys(obj) {
  15. return Object.keys(obj).sort().reduce((result, key) => (result[key] = obj[key]) && result, {});
  16. }
  17. /** Adds a package to the package.json in the given host tree. */
  18. function addPackageToPackageJson(host, pkg, version) {
  19. if (host.exists('package.json')) {
  20. const sourceText = host.read('package.json').toString('utf-8');
  21. const json = JSON.parse(sourceText);
  22. if (!json.dependencies) {
  23. json.dependencies = {};
  24. }
  25. if (!json.dependencies[pkg]) {
  26. json.dependencies[pkg] = version;
  27. json.dependencies = sortObjectByKeys(json.dependencies);
  28. }
  29. host.overwrite('package.json', JSON.stringify(json, null, 2));
  30. }
  31. return host;
  32. }
  33. exports.addPackageToPackageJson = addPackageToPackageJson;
  34. //# sourceMappingURL=package-config.js.map