package-config.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. /**
  3. * Use of this source code is governed by an MIT-style license that can be
  4. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  5. */
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.addPackageToPackageJson = addPackageToPackageJson;
  8. exports.getPackageVersionFromPackageJson = getPackageVersionFromPackageJson;
  9. /**
  10. * Sorts the keys of the given object.
  11. * @returns A new object instance with sorted keys
  12. */
  13. function sortObjectByKeys(obj) {
  14. return Object.keys(obj)
  15. .sort()
  16. .reduce((result, key) => {
  17. result[key] = obj[key];
  18. return result;
  19. }, {});
  20. }
  21. /** Adds a package to the package.json in the given host tree. */
  22. function addPackageToPackageJson(host, pkg, version) {
  23. if (host.exists('package.json')) {
  24. const sourceText = host.read('package.json').toString('utf-8');
  25. const json = JSON.parse(sourceText);
  26. if (!json.dependencies) {
  27. json.dependencies = {};
  28. }
  29. if (!json.dependencies[pkg]) {
  30. json.dependencies[pkg] = version;
  31. json.dependencies = sortObjectByKeys(json.dependencies);
  32. }
  33. host.overwrite('package.json', JSON.stringify(json, null, 2));
  34. }
  35. return host;
  36. }
  37. /** Gets the version of the specified package by looking at the package.json in the given tree. */
  38. function getPackageVersionFromPackageJson(tree, name) {
  39. if (!tree.exists('package.json')) {
  40. return null;
  41. }
  42. const packageJson = JSON.parse(tree.read('package.json').toString('utf8'));
  43. if (packageJson.dependencies && packageJson.dependencies[name]) {
  44. return packageJson.dependencies[name];
  45. }
  46. return null;
  47. }
  48. //# sourceMappingURL=package-config.js.map