package-config.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. /** Gets the version of the specified package by looking at the package.json in the given tree. */
  35. function getPackageVersionFromPackageJson(tree, name) {
  36. if (!tree.exists('package.json')) {
  37. return null;
  38. }
  39. const packageJson = JSON.parse(tree.read('package.json').toString('utf8'));
  40. if (packageJson.dependencies && packageJson.dependencies[name]) {
  41. return packageJson.dependencies[name];
  42. }
  43. return null;
  44. }
  45. exports.getPackageVersionFromPackageJson = getPackageVersionFromPackageJson;
  46. //# sourceMappingURL=package-config.js.map