index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
  8. var __export = (target, all) => {
  9. __markAsModule(target);
  10. for (var name in all)
  11. __defProp(target, name, { get: all[name], enumerable: true });
  12. };
  13. var __reExport = (target, module2, desc) => {
  14. if (module2 && typeof module2 === "object" || typeof module2 === "function") {
  15. for (let key of __getOwnPropNames(module2))
  16. if (!__hasOwnProp.call(target, key) && key !== "default")
  17. __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  18. }
  19. return target;
  20. };
  21. var __toModule = (module2) => {
  22. return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
  23. };
  24. // src/index.ts
  25. __export(exports, {
  26. clearCache: () => clearCache,
  27. detect: () => detect,
  28. getNpmVersion: () => getNpmVersion
  29. });
  30. var import_fs = __toModule(require("fs"));
  31. var import_path = __toModule(require("path"));
  32. var import_execa = __toModule(require("execa"));
  33. async function pathExists(p) {
  34. try {
  35. await import_fs.promises.access(p);
  36. return true;
  37. } catch {
  38. return false;
  39. }
  40. }
  41. var cache = new Map();
  42. function hasGlobalInstallation(pm) {
  43. const key = `has_global_${pm}`;
  44. if (cache.has(key)) {
  45. return Promise.resolve(cache.get(key));
  46. }
  47. return (0, import_execa.default)(pm, ["--version"]).then((res) => {
  48. return /^\d+.\d+.\d+$/.test(res.stdout);
  49. }).then((value) => {
  50. cache.set(key, value);
  51. return value;
  52. });
  53. }
  54. function getTypeofLockFile(cwd = ".") {
  55. const key = `lockfile_${cwd}`;
  56. if (cache.has(key)) {
  57. return Promise.resolve(cache.get(key));
  58. }
  59. return Promise.all([
  60. pathExists((0, import_path.resolve)(cwd, "yarn.lock")),
  61. pathExists((0, import_path.resolve)(cwd, "package-lock.json")),
  62. pathExists((0, import_path.resolve)(cwd, "pnpm-lock.yaml"))
  63. ]).then(([isYarn, isNpm, isPnpm]) => {
  64. let value = null;
  65. if (isYarn) {
  66. value = "yarn";
  67. } else if (isPnpm) {
  68. value = "pnpm";
  69. } else if (isNpm) {
  70. value = "npm";
  71. }
  72. cache.set(key, value);
  73. return value;
  74. });
  75. }
  76. var detect = async ({ cwd } = {}) => {
  77. const type = await getTypeofLockFile(cwd);
  78. if (type) {
  79. return type;
  80. }
  81. const [hasYarn, hasPnpm] = await Promise.all([
  82. hasGlobalInstallation("yarn"),
  83. hasGlobalInstallation("pnpm")
  84. ]);
  85. if (hasYarn) {
  86. return "yarn";
  87. }
  88. if (hasPnpm) {
  89. return "pnpm";
  90. }
  91. return "npm";
  92. };
  93. function getNpmVersion(pm) {
  94. return (0, import_execa.default)(pm || "npm", ["--version"]).then((res) => res.stdout);
  95. }
  96. function clearCache() {
  97. return cache.clear();
  98. }
  99. // Annotate the CommonJS export names for ESM import in node:
  100. 0 && (module.exports = {
  101. clearCache,
  102. detect,
  103. getNpmVersion
  104. });