index.cjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. 'use strict';
  2. const fs = require('node:fs');
  3. const node_module = require('node:module');
  4. const path = require('node:path');
  5. const process = require('node:process');
  6. const fsPromises = require('node:fs/promises');
  7. const node_url = require('node:url');
  8. const mlly = require('mlly');
  9. const macro = require('quansync/macro');
  10. var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
  11. function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
  12. const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
  13. const path__default = /*#__PURE__*/_interopDefaultCompat(path);
  14. const process__default = /*#__PURE__*/_interopDefaultCompat(process);
  15. const fsPromises__default = /*#__PURE__*/_interopDefaultCompat(fsPromises);
  16. const toPath = urlOrPath => urlOrPath instanceof URL ? node_url.fileURLToPath(urlOrPath) : urlOrPath;
  17. async function findUp$1(name, {
  18. cwd = process__default.cwd(),
  19. type = 'file',
  20. stopAt,
  21. } = {}) {
  22. let directory = path__default.resolve(toPath(cwd) ?? '');
  23. const {root} = path__default.parse(directory);
  24. stopAt = path__default.resolve(directory, toPath(stopAt ?? root));
  25. const isAbsoluteName = path__default.isAbsolute(name);
  26. while (directory) {
  27. const filePath = isAbsoluteName ? name : path__default.join(directory, name);
  28. try {
  29. const stats = await fsPromises__default.stat(filePath); // eslint-disable-line no-await-in-loop
  30. if ((type === 'file' && stats.isFile()) || (type === 'directory' && stats.isDirectory())) {
  31. return filePath;
  32. }
  33. } catch {}
  34. if (directory === stopAt || directory === root) {
  35. break;
  36. }
  37. directory = path__default.dirname(directory);
  38. }
  39. }
  40. function findUpSync(name, {
  41. cwd = process__default.cwd(),
  42. type = 'file',
  43. stopAt,
  44. } = {}) {
  45. let directory = path__default.resolve(toPath(cwd) ?? '');
  46. const {root} = path__default.parse(directory);
  47. stopAt = path__default.resolve(directory, toPath(stopAt) ?? root);
  48. const isAbsoluteName = path__default.isAbsolute(name);
  49. while (directory) {
  50. const filePath = isAbsoluteName ? name : path__default.join(directory, name);
  51. try {
  52. const stats = fs__default.statSync(filePath, {throwIfNoEntry: false});
  53. if ((type === 'file' && stats?.isFile()) || (type === 'directory' && stats?.isDirectory())) {
  54. return filePath;
  55. }
  56. } catch {}
  57. if (directory === stopAt || directory === root) {
  58. break;
  59. }
  60. directory = path__default.dirname(directory);
  61. }
  62. }
  63. function _resolve(path$1, options = {}) {
  64. if (options.platform === "auto" || !options.platform)
  65. options.platform = process__default.platform === "win32" ? "win32" : "posix";
  66. if (process__default.versions.pnp) {
  67. const paths = options.paths || [];
  68. if (paths.length === 0)
  69. paths.push(process__default.cwd());
  70. const targetRequire = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
  71. try {
  72. return targetRequire.resolve(path$1, { paths });
  73. } catch {
  74. }
  75. }
  76. const modulePath = mlly.resolvePathSync(path$1, {
  77. url: options.paths
  78. });
  79. if (options.platform === "win32")
  80. return path.win32.normalize(modulePath);
  81. return modulePath;
  82. }
  83. function resolveModule(name, options = {}) {
  84. try {
  85. return _resolve(name, options);
  86. } catch {
  87. return void 0;
  88. }
  89. }
  90. async function importModule(path) {
  91. const i = await import(path);
  92. if (i)
  93. return mlly.interopDefault(i);
  94. return i;
  95. }
  96. function isPackageExists(name, options = {}) {
  97. return !!resolvePackage(name, options);
  98. }
  99. function getPackageJsonPath(name, options = {}) {
  100. const entry = resolvePackage(name, options);
  101. if (!entry)
  102. return;
  103. return searchPackageJSON(entry);
  104. }
  105. const readFile = macro.quansync({
  106. async: (id) => fs__default.promises.readFile(id, "utf8"),
  107. sync: (id) => fs__default.readFileSync(id, "utf8")
  108. });
  109. const getPackageInfo = macro.quansync(function* (name, options = {}) {
  110. const packageJsonPath = getPackageJsonPath(name, options);
  111. if (!packageJsonPath)
  112. return;
  113. const packageJson = JSON.parse(yield readFile(packageJsonPath));
  114. return {
  115. name,
  116. version: packageJson.version,
  117. rootPath: path.dirname(packageJsonPath),
  118. packageJsonPath,
  119. packageJson
  120. };
  121. });
  122. const getPackageInfoSync = getPackageInfo.sync;
  123. function resolvePackage(name, options = {}) {
  124. try {
  125. return _resolve(`${name}/package.json`, options);
  126. } catch {
  127. }
  128. try {
  129. return _resolve(name, options);
  130. } catch (e) {
  131. if (e.code !== "MODULE_NOT_FOUND" && e.code !== "ERR_MODULE_NOT_FOUND")
  132. console.error(e);
  133. return false;
  134. }
  135. }
  136. function searchPackageJSON(dir) {
  137. let packageJsonPath;
  138. while (true) {
  139. if (!dir)
  140. return;
  141. const newDir = path.dirname(dir);
  142. if (newDir === dir)
  143. return;
  144. dir = newDir;
  145. packageJsonPath = path.join(dir, "package.json");
  146. if (fs__default.existsSync(packageJsonPath))
  147. break;
  148. }
  149. return packageJsonPath;
  150. }
  151. const findUp = macro.quansync({
  152. sync: findUpSync,
  153. async: findUp$1
  154. });
  155. const loadPackageJSON = macro.quansync(function* (cwd = process__default.cwd()) {
  156. const path = yield findUp("package.json", { cwd });
  157. if (!path || !fs__default.existsSync(path))
  158. return null;
  159. return JSON.parse(yield readFile(path));
  160. });
  161. const loadPackageJSONSync = loadPackageJSON.sync;
  162. const isPackageListed = macro.quansync(function* (name, cwd) {
  163. const pkg = (yield loadPackageJSON(cwd)) || {};
  164. return name in (pkg.dependencies || {}) || name in (pkg.devDependencies || {});
  165. });
  166. const isPackageListedSync = isPackageListed.sync;
  167. exports.getPackageInfo = getPackageInfo;
  168. exports.getPackageInfoSync = getPackageInfoSync;
  169. exports.importModule = importModule;
  170. exports.isPackageExists = isPackageExists;
  171. exports.isPackageListed = isPackageListed;
  172. exports.isPackageListedSync = isPackageListedSync;
  173. exports.loadPackageJSON = loadPackageJSON;
  174. exports.loadPackageJSONSync = loadPackageJSONSync;
  175. exports.resolveModule = resolveModule;