get-package-version.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPackageVersion = getPackageVersion;
  6. var _fs = require("fs");
  7. var _findUp = _interopRequireDefault(require("next/dist/compiled/find-up"));
  8. var _json5 = _interopRequireDefault(require("next/dist/compiled/json5"));
  9. var path = _interopRequireWildcard(require("path"));
  10. function _interopRequireDefault(obj) {
  11. return obj && obj.__esModule ? obj : {
  12. default: obj
  13. };
  14. }
  15. function _getRequireWildcardCache() {
  16. if (typeof WeakMap !== "function") return null;
  17. var cache = new WeakMap();
  18. _getRequireWildcardCache = function() {
  19. return cache;
  20. };
  21. return cache;
  22. }
  23. function _interopRequireWildcard(obj) {
  24. if (obj && obj.__esModule) {
  25. return obj;
  26. }
  27. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  28. return {
  29. default: obj
  30. };
  31. }
  32. var cache = _getRequireWildcardCache();
  33. if (cache && cache.has(obj)) {
  34. return cache.get(obj);
  35. }
  36. var newObj = {};
  37. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  38. for(var key in obj){
  39. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  40. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  41. if (desc && (desc.get || desc.set)) {
  42. Object.defineProperty(newObj, key, desc);
  43. } else {
  44. newObj[key] = obj[key];
  45. }
  46. }
  47. }
  48. newObj.default = obj;
  49. if (cache) {
  50. cache.set(obj, newObj);
  51. }
  52. return newObj;
  53. }
  54. let cachedDeps;
  55. function getDependencies({ cwd }) {
  56. if (cachedDeps) {
  57. return cachedDeps;
  58. }
  59. return cachedDeps = (async ()=>{
  60. const configurationPath = await (0, _findUp).default("package.json", {
  61. cwd
  62. });
  63. if (!configurationPath) {
  64. return {
  65. dependencies: {},
  66. devDependencies: {}
  67. };
  68. }
  69. const content = await _fs.promises.readFile(configurationPath, "utf-8");
  70. const packageJson = _json5.default.parse(content);
  71. const { dependencies ={} , devDependencies ={} } = packageJson || {};
  72. return {
  73. dependencies,
  74. devDependencies
  75. };
  76. })();
  77. }
  78. async function getPackageVersion({ cwd , name }) {
  79. const { dependencies , devDependencies } = await getDependencies({
  80. cwd
  81. });
  82. if (!(dependencies[name] || devDependencies[name])) {
  83. return null;
  84. }
  85. const cwd2 = cwd.endsWith(path.posix.sep) || cwd.endsWith(path.win32.sep) ? cwd : `${cwd}/`;
  86. try {
  87. const targetPath = require.resolve(`${name}/package.json`, {
  88. paths: [
  89. cwd2
  90. ]
  91. });
  92. const targetContent = await _fs.promises.readFile(targetPath, "utf-8");
  93. var _version;
  94. return (_version = _json5.default.parse(targetContent).version) != null ? _version : null;
  95. } catch {
  96. return null;
  97. }
  98. }
  99. //# sourceMappingURL=get-package-version.js.map